Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know what options are available as 3rd argument of ListMapper->add() in SonataAdminBundle

How do I know what options are available as 3rd argument of ListMapper->add() in SonataAdminBundle. (Same with DatagridMapper->add() and FormMapper->add()).

You says there is a link with options http://symfony.com/doc/current/bundles/SonataAdminBundle/reference/action_list.html#options

but there are also a few here http://symfony.com/doc/current/bundles/SonataAdminBundle/reference/action_list.html#visual-configuration

How do I know if there are more options available? Perfectly if someone point how to discover that from Sonata code (maybe ListMapper class).

Because f.e. I want to reduce size of text in cell if it's too large, and I don't know if I can use some 3rd argument option or I need to override template.

like image 610
user6827096 Avatar asked Mar 11 '23 18:03

user6827096


2 Answers

The problem is that options are stored in a native PHP array and used "on-the-fly" by templates, DoctrineORM bundle, etc... so there is no simple way to find an exhaustive list of all of them.

However, I found a solution to list almost all options for the ListMapper (some are from the DatagridMapper, but it is really difficult to differenciate them).

Here they are:

_sort_order
admin_code
ajax_hidden
association_mapping
code
editable
field_mapping
field_name
field_options
field_type
format
global_search
header_class
header_style
identifier
label
mapping_type
name
operator_options
operator_type
options
parameters
parent_association_mappings
route
route.name
route.parameters
row_align
sort_field_mapping
sort_parent_association_mappings
sortable
timezone
translation_domain
virtual_field

To get this list, I had the idea to make the function SonataAdminBundle\Admin\BaseFieldDescription::getOptions() returns a custom array object that logs every call to isset, unset, getters and setters (with brackets operator). I also logs SonataAdminBundle\Admin\BaseFieldDescription::getOption($name, $default = null) calls.

Code related:

  • TestBundle\ArrayTest

    namespace TestBundle;
    
    class ArrayTest implements \ArrayAccess
    {
        private $container = array();
        private $previousOffset;
    
        public function __construct($array, $previousOffset = null) {
            $this->container = $array;
            $this->previousOffset = $previousOffset;
        }
    
        public function offsetSet($offset, $value) {
            if (is_null($offset)) {
                $this->container[] = $value;
            } else {
                $this->dump($offset);
                $this->container[$offset] = $value;
            }
        }
    
        public function offsetExists($offset) {
            $this->dump($offset);
            return isset($this->container[$offset]);
        }
    
        public function offsetUnset($offset) {
            $this->dump($offset);
            unset($this->container[$offset]);
        }
    
        public function offsetGet($offset) {
            $this->dump($offset);
            if (isset($this->container[$offset])) {
                if (is_array($this->container[$offset])) {
                    $newOffset = ($this->previousOffset ?: '').$offset.'.';
    
                    if ($newOffset === 'route.parameter.') { // because of Sonata\AdminBundle\Admin\AbstractAdmin::generateObjectUrl()
                        return $this->container[$offset];
                    }
                    return new ArrayTest($this->container[$offset], $newOffset);
                }
    
                return $this->container[$offset];
            }
            return null;
        }
    
        private function dump($offset)
        {
            $offset = ($this->previousOffset ?: '').$offset;
            file_put_contents("/tmp/toto.txt", $offset."\n", FILE_APPEND);
        }
    }
    
  • SonataAdminBundle\Admin\BaseFieldDescription::getOption($name, $default = null)

    public function getOption($name, $default = null)
    {
        file_put_contents("/tmp/toto.txt", $name."\n", FILE_APPEND);
        return isset($this->options[$name]) ? $this->options[$name] : $default;
    }
    
  • SonataAdminBundle\Admin\BaseFieldDescription::getOptions()

    New function's prototype : getOptions($fakeArray = true)

    public function getOptions($fakeArray = true)
    {
        if ($fakeArray) {
            return new \TestBundle\ArrayTest($this->options);
        }
    
        return $this->options;
    }
    
  • Sonata\DoctrineORMAdminBundle\Builder\DatagridBuilder::addFilter(DatagridInterface $datagrid, $type, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)

    Line 129 :

    $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions());
    

    to

    $filter = $this->filterFactory->create($fieldDescription->getName(), $type, $fieldDescription->getOptions(false));
    

Then, display a list on sonata admin, and run cat /tmp/toto.txt | sort -u to get the list above.

To get this list of options, I displayed the admin's list of SonataUserBundle. You may find more options displaying an other admin list (which uses other templates for example).


N.B.: I ran this on a clean install of Symfony 2.8.11, SonataAdminBundle 3.8.0, SonataDoctrineORMAdminBundle 3.1.0 and SonataUserBundle 3.0.1.

like image 133
Jules Lamur Avatar answered May 12 '23 00:05

Jules Lamur


After some search in Sonata Bundle code, not in documentation !
I think you have this options:

  1. type
  2. template
  3. help
  4. placeholder
  5. link_parameters

check in : Sonata\AdminBundle\Admin\BaseFieldDescription::setOptions(array $options)

You can do all at once like this:

  ->add('_action','actions',array(
                                  'actions'=>array(
                                         'view'=>array(),
                                         'edit'=>array(),
                                         'delete'=>array()
                                         )
                                  )
        )

We don't use anymore SonataAdmin, is better with my own controllers that fill desires of the project.

A solution for your needs is to use a template with some twig:

 ->add('first_name', null,array(
       'template'=>'AppBundle:User:_partial_with_some_template.html.twig',)
       )

I hope this will help you ! :)
Have fun !

like image 34
CristiC777 Avatar answered May 11 '23 22:05

CristiC777