Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
48.15% |
13 / 27 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| EntityHooks | |
48.15% |
13 / 27 |
|
66.67% |
2 / 3 |
82.48 | |
0.00% |
0 / 1 |
| entityBundleInfoAlter | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| inlineEntityFormTableFieldsAlter | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
6 | |||
| entityView | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
132 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Drupal\crm\Hook; |
| 4 | |
| 5 | use Drupal\Core\Entity\EntityInterface; |
| 6 | use Drupal\Core\Hook\Attribute\Hook; |
| 7 | use Drupal\crm\Entity\ContactDetail\AddressContactDetail; |
| 8 | use Drupal\crm\Entity\ContactDetail\EmailContactDetail; |
| 9 | use Drupal\crm\Entity\ContactDetail\TelephoneContactDetail; |
| 10 | |
| 11 | /** |
| 12 | * Hooks relating to entities. |
| 13 | */ |
| 14 | class EntityHooks { |
| 15 | |
| 16 | /** |
| 17 | * Implements hook_entity_bundle_info_alter(). |
| 18 | */ |
| 19 | #[Hook('entity_bundle_info_alter')] |
| 20 | public function entityBundleInfoAlter(array &$bundles): void { |
| 21 | if (isset($bundles['crm_contact_detail']['address'])) { |
| 22 | $bundles['crm_contact_detail']['address']['class'] = AddressContactDetail::class; |
| 23 | } |
| 24 | if (isset($bundles['crm_contact_detail']['email'])) { |
| 25 | $bundles['crm_contact_detail']['email']['class'] = EmailContactDetail::class; |
| 26 | } |
| 27 | if (isset($bundles['crm_contact_detail']['telephone'])) { |
| 28 | $bundles['crm_contact_detail']['telephone']['class'] = TelephoneContactDetail::class; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Implements hook_inline_entity_form_table_fields_alter(). |
| 34 | */ |
| 35 | #[Hook('inline_entity_form_table_fields_alter')] |
| 36 | public function inlineEntityFormTableFieldsAlter(&$fields, $context) { |
| 37 | if ($context['parent_entity_type'] == 'crm_contact' && $context['entity_type'] == 'crm_contact_detail') { |
| 38 | if ($context['allowed_bundles'] == ['address']) { |
| 39 | $fields['label']['label'] = t('Address (line 1)'); |
| 40 | } |
| 41 | elseif ($context['allowed_bundles'] == ['telephone']) { |
| 42 | $fields['label']['label'] = t('Telephone'); |
| 43 | } |
| 44 | elseif ($context['allowed_bundles'] == ['email']) { |
| 45 | $fields['label']['label'] = t('Email'); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Implements hook_entity_view(). |
| 52 | */ |
| 53 | #[Hook('entity_view')] |
| 54 | public function entityView(array &$build, EntityInterface $entity, $view_mode, $langcode) { |
| 55 | $entity_type = $entity->getEntityTypeId(); |
| 56 | |
| 57 | if ($entity_type === 'crm_relationship') { |
| 58 | /** @var \Drupal\crm\Entity\Relationship $entity */ |
| 59 | $bundle = $entity->get('bundle')->entity; |
| 60 | if (isset($build['contact_a'])) { |
| 61 | $build['contact_a']['#title'] = $bundle->get('label_a'); |
| 62 | } |
| 63 | if (isset($build['contact_b'])) { |
| 64 | $build['contact_b']['#title'] = $bundle->get('label_b'); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if ($entity_type === 'crm_contact') { |
| 69 | /** @var \Drupal\crm\Entity\Contact $entity */ |
| 70 | $contact_type = $entity->get('bundle')->entity; |
| 71 | $date = $contact_type->get('date'); |
| 72 | |
| 73 | // Customize start_date field label and description in display. |
| 74 | if ($date && !empty($date['start_date']['label']) && isset($build['start_date'])) { |
| 75 | $build['start_date']['#title'] = $date['start_date']['label']; |
| 76 | } |
| 77 | |
| 78 | // Customize end_date field label and description in display. |
| 79 | if ($date && !empty($date['end_date']['label']) && isset($build['end_date'])) { |
| 80 | $build['end_date']['#title'] = $date['end_date']['label']; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | } |