Added full_name and first_and_last_name fields to the Person entity type.

The full name is now the entity label. The first_and_last_name is so that it can be used in search and autocomplete.
This commit is contained in:
naomi 2018-04-05 23:02:55 +02:00
parent 7ac39fc385
commit 6cf7d5ee6f
2 changed files with 30 additions and 0 deletions

View File

@ -49,6 +49,7 @@ use Drupal\user\UserInterface;
* "uid" = "user_id", * "uid" = "user_id",
* "langcode" = "langcode", * "langcode" = "langcode",
* "status" = "status", * "status" = "status",
* "label" = "full_name",
* }, * },
* links = { * links = {
* "canonical" = "/zencrm/person/{person}", * "canonical" = "/zencrm/person/{person}",
@ -192,6 +193,16 @@ class Person extends RevisionableContentEntityBase implements PersonInterface {
->setSetting('handler', 'default') ->setSetting('handler', 'default')
->setTranslatable(TRUE); ->setTranslatable(TRUE);
$fields['full_name'] = BaseFieldDefinition::create('string')
->setLabel(t('Full Name'))
->setDescription(t('The full name of the person.'))
->setRevisionable(TRUE);
$fields['first_and_last_name'] = BaseFieldDefinition::create('string')
->setLabel(t('First and Last Name'))
->setDescription(t('The first and last name of the person. Used for searching and autocomplete'))
->setRevisionable(TRUE);
$fields['first_name'] = BaseFieldDefinition::create('string') $fields['first_name'] = BaseFieldDefinition::create('string')
->setLabel(t('First Name')) ->setLabel(t('First Name'))
->setDescription(t('First Name.')) ->setDescription(t('First Name.'))

View File

@ -7,6 +7,25 @@
use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_ENTITY_TYPE_presave().
* Computes the full_name field from first_name middle_names and last_name.
* Computes the first_and_last_name field from first_name and last_name.
*/
function zencrm_entities_person_presave($entity) {
$first_name = $entity->first_name->getString();
$middle_names = $entity->middle_names->getString();
$last_name = $entity->last_name->getString();
if ($middle_names) {
$middle_names .= ' ';
}
$first_and_last_name = $first_name . ' ' . $last_name;
$full_name = $first_name . ' ' . $middle_names . $last_name;
$entity->set('full_name', $full_name);
$entity->set('first_and_last_name', $first_and_last_name);
}
/** /**
* Implements hook_help(). * Implements hook_help().
*/ */