user1000652
user1000652

Reputation: 33

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

Upvotes: 1

Views: 2783

Answers (2)

devilcius
devilcius

Reputation: 1884

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'))
}

Upvotes: 5

Richard
Richard

Reputation: 4119

You could possibly use a data transformer.

Or (and this doesn't seem like a good idea to me at all)you could do something like:

  1. Add a non mapped field, call it fullName.
  2. Add a PRE_SUBMIT form event listener which will populate fullName with firstName + LastName
  3. Add a POST_SUBMIT form event listener which will then parse fullName back into lastName and firstName
  4. Persist entity.

Upvotes: 0

Related Questions