Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
47 / 47
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
FormattedNameTokenInfoRegistrar
100.00% covered (success)
100.00%
47 / 47
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 register
100.00% covered (success)
100.00%
47 / 47
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\name\Utility;
6
7use Drupal\name\Service\NameFormatterInterface;
8
9/**
10 * Registers formatted name token types and browser entries in token info.
11 *
12 * @internal
13 */
14final class FormattedNameTokenInfoRegistrar {
15
16  /**
17   * Demo components for token browser descriptions.
18   *
19   * @var array<string, string>
20   */
21  private const DEMO_COMPONENTS = [
22    'title'        => 'Mr.',
23    'given'        => 'John',
24    'middle'       => 'Q.',
25    'family'       => 'Public',
26    'generational' => 'Sr.',
27    'credentials'  => 'PhD',
28  ];
29
30  /**
31   * Registers formatted token chain and browser entries for one name field.
32   *
33   * @param array $info
34   *   Token info array from hook_token_info_alter(), altered in place.
35   * @param string $entity_type_id
36   *   Entity type id (e.g. node).
37   * @param string $field_name
38   *   Field machine name.
39   * @param string $label
40   *   Human-readable field label for descriptions.
41   * @param array $formats
42   *   Loaded name format config entities keyed by id.
43   * @param \Drupal\name\Service\NameFormatterInterface $name_formatter
44   *   The name formatter service.
45   * @param string|null $field_sub_type
46   *   Token sub-type id when Token chained the field, or NULL.
47   */
48  public static function register(
49    array &$info,
50    string $entity_type_id,
51    string $field_name,
52    string $label,
53    array $formats,
54    NameFormatterInterface $name_formatter,
55    ?string $field_sub_type,
56  ): void {
57    $pointer                    = FormattedNameTokenParser::formattedPointer($field_name);
58    $chain_type                 = FormattedNameTokenParser::formattedChainType(
59      $entity_type_id,
60      $field_name,
61    );
62    $info['types'][$chain_type] = [
63      'name'        => t('Formatted name'),
64      'description' => t(
65        'Full name for @field using a saved name format. First value only; other deltas: [entity:field:delta:formatted:format].',
66        ['@field' => $label],
67      ),
68      'needs-data'  => $entity_type_id,
69    ];
70    foreach ($formats as $format) {
71      $demo_formatted = (string) $name_formatter->format(
72        self::DEMO_COMPONENTS,
73        $format->id(),
74      );
75      $info['tokens'][$chain_type][$format->id()] = [
76        'name'        => $format->label(),
77        'description' => t(
78          '@field using the @format name format (@demo).',
79          [
80            '@field'  => $label,
81            '@format' => $format->label(),
82            '@demo'   => $demo_formatted,
83          ],
84        ),
85      ];
86    }
87
88    $formatted_parent = [
89      'name'        => t('Formatted'),
90      'description' => t(
91        'Full name text using a name format (first value only). For other deltas use [entity:field:delta:formatted:format].',
92      ),
93      'type'        => $chain_type,
94    ];
95    if ($field_sub_type !== NULL) {
96      $info['tokens'][$field_sub_type]['formatted'] = $formatted_parent;
97      return;
98    }
99    $info['tokens'][$entity_type_id][$pointer] = [
100      'name'        => t('Formatted: @field', ['@field' => $label]),
101      'description' => t(
102        'Structured name for @field using a name format (expand to pick a format).',
103        ['@field' => $label],
104      ),
105      'type'        => $chain_type,
106    ];
107  }
108
109}