How to combine firstName and lastName, then show it with Full name column in Sonata admin

1k views Asked by At

I'm trying to show Full name column with firstName and lastName from entity. How can I do?

Here is my Entity and Admin.php:

class test{

private firstName;

//another properties

private lastName;

}

Admin

protected function configureListFields(ListMapper $listMapper){
$listMapper
  ->add('id',null)
  ->add('Full name'); //I want to show the column like this (Full name = firstName + lastName)
}
1

There are 1 answers

0
Ali Mhanna On

try to use this inside your user Entity:

    /**
 * Get the full username if first and last name are set,
 * the username otherwise
 *
 * @return string
 */
public function getFullUsername(): string
{
    if ($this->firstname && $this->lastname) {
        return $this->firstname . ' ' . $this->lastname;
    }

    return $this->username;
}