Twig 拡張機能を作成できます。
class TranslatableTypeExtension extends AbstractTypeExtension
{
/**
* @var ObjectManager
*/
private $om;
/**
* @var TranslatableListener
*/
private $listener;
/**
* @param ObjectManager $om
*/
public function __construct(ObjectManager $om, TranslatableListener $listener )
{
$this->om = $om;
$this->listener = $listener;
}
private function isTranslatableField($object, $name)
{
$config = $this->listener->getConfiguration($this->om, get_class($object));
if (isset($config['fields']) && in_array($name, $config['fields']) )
return true;
return false;
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
if ( $form->getParent() == null )
return;
if ( is_object($form->getParent()->getData())) {
if ( $this->isTranslatableField($form->getParent()->getData(), $form->getName()) )
$view->vars['field_translatable'] = true;
}
}
/**
* Returns the name of the type being extended.
*
* @return string The name of the type being extended
*/
public function getExtendedType()
{
return 'field';
}
}
この拡張機能を次のようにロードします。
my_extension.translatable_type_extension:
class: Acme\DemoBundle\Form\Extension\TranslatableTypeExtension
arguments: ["@doctrine.orm.entity_manager", "@gedmo.listener.translatable"]
tags:
- { name: form.type_extension, alias: field }
小枝テンプレートでは、次のようなものを使用できます。
{% if field_translatable is defined and field_translatable %} Translatable field {% endif %}