7

In my project, I have pages and elements linked to them. An element can be linked to only one page.

I have a specific form to choose a field mask and many fields. I would like to have a list of elements linked to a page instead of embedded forms. Is it possible to open a modal dialog to have the edit form in it, like when you add an element?

Is sonata_type_collection the most suitable for that?

PageAdmin

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper) {
    $availableApiRoutes = [];
    foreach ($this->getConfigurationPool()->getContainer()->get('router')->getRouteCollection()->all() as $name => $route) {
        $route = $route->compile();
        if (( strstr($name, "api_") === FALSE ) &&
                ( strstr($name, "admin") === FALSE ) &&
                ( strstr($name, "ajax") === FALSE ) &&
                ( strstr($name, "fos") === FALSE ) &&
                ( strstr($name, "sonata") === FALSE ) &&
                ( strstr($name, "add") === FALSE ) &&
                ( strstr($name, "edit") === FALSE ) &&
                ( strstr($name, "payment") === FALSE ) &&
                ( strstr($name, "suppr") === FALSE ) &&
                ( substr($name, 0, 1) !== "_" )) {
            $availableApiRoutes[$name] = $name;
        }
    }
    $formMapper
            ->add('route', FormType\ChoiceType::class , array(
                'choices' => $availableApiRoutes,
            ))
            ->add('element', 'sonata_type_collection', array(), array('edit' => 'inline', 'inline'=>'table'))
    ;
}

And the element form :

ElementAdmin

/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper) {
    $formMapper
            ->add('page', 'sonata_type_model_hidden')
            ->add('id')
            ->add('title')
            ->add('type', 'sonata_type_choice_field_mask', array(
                'choices' => array(
                    'texte' => 'texte',
                    'image' => 'image',
                    'gallery' => 'gallerie'
                ),
                'map' => array(
                    'texte' => array('texte'),
                    'image' => array('image'),
                    'gallery' => array('gallery'),
                ),
                'empty_value' => 'Choose an option',
                'required' => true
            ))
            ->add('texte', CKEditorType::class, array(
                'config' => array(
                    'uiColor' => '#ffffff'
                )
            ))
            ->add('image', 'sonata_type_model_list', array(), array('link_parameters' => array('context' => 'cms')))
            ->add('gallery', 'sonata_type_model_list', array(), array(
                'edit' => 'inline',
                'inline' => 'table',
                'link_parameters' => array(
                    'context' => 'cms',
                    'provider' => 'sonata.media.provider.image'
                )
            ))
    ;
}
Opux
  • 702
  • 1
  • 10
  • 30
Christophe Ferreboeuf
  • 1,048
  • 14
  • 26

0 Answers0