Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 5
CRAP
n/a
0 / 0
template_preprocess_crm_contact
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
template_preprocess_crm_relationship
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
crm_form_block_form_alter
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
crm_form_crm_contact_type_edit_form_alter
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
6
crm_form_crm_contact_type_edit_form_submit
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * @file
5 * Provides a contact entity type.
6 */
7
8use Drupal\Core\Render\Element;
9use Drupal\Core\Form\FormStateInterface;
10use Drupal\name\Entity\NameFormat;
11
12/**
13 * Prepares variables for contact templates.
14 *
15 * Default template: crm-contact.html.twig.
16 *
17 * @param array $variables
18 *   An associative array containing:
19 *   - elements: An associative array containing the contact information and any
20 *     fields attached to the entity.
21 *   - attributes: HTML attributes for the containing element.
22 */
23function template_preprocess_crm_contact(array &$variables) {
24  $variables['view_mode'] = $variables['elements']['#view_mode'];
25  foreach (Element::children($variables['elements']) as $key) {
26    $variables['content'][$key] = $variables['elements'][$key];
27  }
28}
29
30/**
31 * Prepares variables for crm relationship templates.
32 *
33 * Default template: crm-relationship.html.twig.
34 *
35 * @param array $variables
36 *   An associative array containing:
37 *   - elements: An associative array containing the relationship and any
38 *     fields attached to the entity.
39 *   - attributes: HTML attributes for the containing element.
40 */
41function template_preprocess_crm_relationship(array &$variables) {
42  $variables['view_mode'] = $variables['elements']['#view_mode'];
43  foreach (Element::children($variables['elements']) as $key) {
44    $variables['content'][$key] = $variables['elements'][$key];
45  }
46}
47
48/**
49 * Implements hook_form_FORM_ID_alter().
50 */
51function crm_form_block_form_alter(&$form, FormStateInterface $form_state) {
52  $form['#attached']['library'][] = 'crm/crm.block';
53}
54
55/**
56 * Implements hook_form_FORM_ID_alter().
57 */
58function crm_form_crm_contact_type_edit_form_alter(&$form, FormStateInterface $form_state) {
59  $entity = $form_state->getFormObject()->getEntity();
60  if ($entity->id() != 'person') {
61    return;
62  }
63
64  $format_options = NameFormat::loadMultiple();
65  $format_options = array_map(function ($format) {
66    return $format->label();
67  }, $format_options);
68
69  $form['name_format'] = [
70    '#type' => 'select',
71    '#title' => t('Name Format'),
72    '#description' => t('Select the format for displaying names for this contact type.'),
73    '#options' => $format_options,
74    '#default_value' => $entity->getThirdPartySetting('crm', 'name_format', 'default'),
75  ];
76  $form['actions']['submit']['#submit'][] = 'crm_form_crm_contact_type_edit_form_submit';
77}
78
79/**
80 * Submit handler for the contact type edit form.
81 */
82function crm_form_crm_contact_type_edit_form_submit(array &$form, FormStateInterface $form_state) {
83  $name_format = $form_state->getValue('name_format');
84  // Third party settings.
85  $entity = $form_state->getFormObject()->getEntity();
86  $entity->setThirdPartySetting('crm', 'name_format', $name_format);
87  $entity->save();
88}