Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
65.00% covered (warning)
65.00%
13 / 20
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EntityHooks
65.00% covered (warning)
65.00%
13 / 20
66.67% covered (warning)
66.67%
2 / 3
22.40
0.00% covered (danger)
0.00%
0 / 1
 entityBundleInfoAlter
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 inlineEntityFormTableFieldsAlter
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
6
 entityView
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace Drupal\crm\Hook;
4
5use Drupal\Core\Entity\EntityInterface;
6use Drupal\Core\Hook\Attribute\Hook;
7use Drupal\crm\Entity\ContactDetail\AddressContactDetail;
8use Drupal\crm\Entity\ContactDetail\EmailContactDetail;
9use Drupal\crm\Entity\ContactDetail\TelephoneContactDetail;
10
11/**
12 * Hooks relating to entities.
13 */
14class 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
69}