To turn Symfony forms into lists, I created a new type of type ListType, which accepts an arbitrary type of nested collection. This way I can create lists with different columns. It will look something like this:
class ListType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { if (empty($options['collection_type']) || !$options['collection_type'] instanceof AbstractType) { throw new \InvalidArgumentException(); } $builder->add('rows', 'collection', array('type' => $options['collection_type'])); $builder->add('save', 'submit', array('label' => 'Save')); } ... }
I will use the $ options array to provide or enable and disable other functions, such as a search filter and pagination.
To have a convenient interface for displaying lists, for example:
{{ list(list) }}
I created a Twig extension:
class ListExtension extends \Twig_Extension { public function getFunctions() { return array( new \Twig_SimpleFunction('list', array($this, 'listFunction'), array('is_safe' => array('html'), 'needs_environment' => true)) ); } public function listFunction(\Twig_Environment $env, FormView $form) { return $env->resolveTemplate($this->defaultTemplate)->renderBlock('list', array('form' => $form)); } ... }
It displays a βlistβ of Twig block. I will also add other subblocks equally. This extension provides greater feed than mold blocks.
I registered the list extension as a service:
# app/config/services.yml services: my.twig.list.extension: class: MyBundle\Twig\Extension\ListExtension arguments: ["::my_theme.html.twig"] public: false tags: - { name: twig.extension }
Now all I have to do is create a form of my ListType type and pass it an array of string entities.
Thanks to Elias Van Ootegem for pointing me in the right direction.