Search code examples
symfonysonata-admin

Sonata 2 display 2 fields in the same column


I display the list form of Sonata. I have 2 fields : firstname, lastname. I want to display 2 fields in the same column.

Currently, I'm doing

$listMapper->add('firstname', 'text', array('label' => 'First Name'))
           ->add('lastname', 'text', array('label' => 'Last Name'));

How can I combine 2 fields in one without to change the Entity


Solution

  • This is how i do it:

    Say firstname and lastname are properties of User. In your entity class User, just add:

    /**
     * @return string
     */
    public function getFullname()
    {
        return sprintf("%s %s", $this->getFirstname(), $this->getLastname());
    }
    

    Then in your admin class:

    /**
     * {@inheritdoc}
     */
    protected function configureListFields(ListMapper $listMapper)
    {
      $listMapper
      ...
         ->add('fullname', null, array('label' => 'Full Name'))
    }