diff --git a/modules/zencrm_entities/src/Entity/Person.php b/modules/zencrm_entities/src/Entity/Person.php index fd38dee..55d6236 100644 --- a/modules/zencrm_entities/src/Entity/Person.php +++ b/modules/zencrm_entities/src/Entity/Person.php @@ -49,6 +49,7 @@ use Drupal\user\UserInterface; * "uid" = "user_id", * "langcode" = "langcode", * "status" = "status", + * "label" = "full_name", * }, * links = { * "canonical" = "/zencrm/person/{person}", @@ -192,6 +193,16 @@ class Person extends RevisionableContentEntityBase implements PersonInterface { ->setSetting('handler', 'default') ->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') ->setLabel(t('First Name')) ->setDescription(t('First Name.')) diff --git a/modules/zencrm_entities/zencrm_entities.module b/modules/zencrm_entities/zencrm_entities.module index 985f2fd..42ba0cc 100644 --- a/modules/zencrm_entities/zencrm_entities.module +++ b/modules/zencrm_entities/zencrm_entities.module @@ -7,6 +7,25 @@ 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(). */