Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
62 / 62
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
NameListFormatListBuilder
100.00% covered (success)
100.00%
62 / 62
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 createInstance
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 buildHeader
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 buildRow
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
4
 examples
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\name\ListBuilder;
6
7use Drupal\Component\Render\FormattableMarkup;
8use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
9use Drupal\Core\Entity\EntityInterface;
10use Drupal\Core\Entity\EntityStorageInterface;
11use Drupal\Core\Entity\EntityTypeInterface;
12use Drupal\name\Entity\NameListFormat;
13use Drupal\name\Service\GeneratorInterface;
14use Drupal\name\Service\NameFormatParserInterface;
15use Drupal\name\Service\NameFormatterInterface;
16use Symfony\Component\DependencyInjection\ContainerInterface;
17
18/**
19 * The name list builder.
20 */
21class NameListFormatListBuilder extends ConfigEntityListBuilder {
22
23  /**
24   * The name formatter.
25   *
26   * @var \Drupal\name\Service\NameFormatterInterface
27   */
28  protected $formatter;
29
30  /**
31   * The name format parser.
32   *
33   * @var \Drupal\name\Service\NameFormatParserInterface
34   */
35  protected $parser;
36
37  /**
38   * The name generator.
39   *
40   * @var \Drupal\name\Service\GeneratorInterface
41   */
42  protected $generator;
43
44  /**
45   * {@inheritdoc}
46   */
47  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
48    return new static(
49      $entity_type,
50      $container->get('entity_type.manager')->getStorage($entity_type->id()),
51      $container->get('name.formatter'),
52      $container->get('name.format_parser'),
53      $container->get('name.generator')
54    );
55  }
56
57  /**
58   * Constructs a new EntityListBuilder object.
59   *
60   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
61   *   The entity type definition.
62   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
63   *   The entity storage class.
64   * @param \Drupal\name\Service\NameFormatterInterface $formatter
65   *   The name formatter.
66   * @param \Drupal\name\Service\NameFormatParserInterface $parser
67   *   The name format parser.
68   * @param \Drupal\name\Service\GeneratorInterface $generator
69   *   The name generator service.
70   */
71  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, NameFormatterInterface $formatter, NameFormatParserInterface $parser, GeneratorInterface $generator) {
72    parent::__construct($entity_type, $storage);
73    $this->parser = $parser;
74    $this->formatter = $formatter;
75    $this->generator = $generator;
76  }
77
78  /**
79   * {@inheritdoc}
80   */
81  public function buildHeader() {
82    $row = [];
83    $row['label'] = $this->t('Label');
84    $row['id'] = $this->t('Machine name');
85    $row['settings'] = $this->t('Settings');
86    $row['examples'] = $this->t('Examples');
87    $row['operations'] = $this->t('Operations');
88    return $row;
89  }
90
91  /**
92   * {@inheritdoc}
93   */
94  public function buildRow(EntityInterface $entity) {
95    if (!$entity instanceof NameListFormat) {
96      throw new \LogicException(sprintf(
97        'Expected %s in %s::buildRow(), got %s.',
98        NameListFormat::class,
99        static::class,
100        get_debug_type($entity),
101      ));
102    }
103
104    $row = [];
105    $row['label'] = $entity->label();
106    $row['id'] = $entity->id();
107
108    $settings = $entity->listSettings();
109
110    $and_options = $this->formatter->getLastDelimiterTypes();
111    $and_delimiter = $and_options[$settings['and']] ?? $this->t('-- invalid option --');
112
113    $and_behavior_options = $this->formatter->getLastDelimiterBehaviors(FALSE);
114    $and_behavior = $and_behavior_options[$settings['delimiter_precedes_last']] ?? $this->t('-- invalid option --');
115    $behavior = $this->t('Show all names.');
116    if ($settings['el_al_min']) {
117      $behavior = $this->t('Reduce after @max items and show @min items followed by <em>el al</em>.', [
118        '@max' => $settings['el_al_min'],
119        '@min' => $settings['el_al_first'],
120      ]);
121    }
122
123    $summary = [
124      $behavior,
125      $this->t('Delimiters: "@delimiter" and @last', [
126        '@delimiter' => $settings['delimiter'],
127        '@last' => $and_delimiter,
128      ]),
129      $this->t('Last delimiter: @delimiter', ['@delimiter' => $and_behavior]),
130    ];
131    if ($entity->isLocked()) {
132      $summary[] = t('Default format (locked)');
133    }
134    $row['settings'] = new FormattableMarkup(implode('<br>', $summary), []);
135
136    // Add a few examples.
137    $row['examples'] = $this->examples($entity);
138
139    $operations = $this->buildOperations($entity);
140    $row['operations']['data'] = $operations;
141    return $row;
142  }
143
144  /**
145   * Provides some example based on names with various components set.
146   *
147   * @param \Drupal\name\Entity\NameListFormat $entity
148   *   The name format entity.
149   *
150   * @return \Drupal\Component\Render\FormattableMarkup
151   *   The example names with formatting applied.
152   */
153  protected function examples(NameListFormat $entity) {
154    $examples = [];
155    foreach ([1, 2, 3, 4] as $num) {
156      $names = $this->generator->generateSampleNames($num);
157      $examples[] = $this->t('(@num) %list', [
158        '@num' => $num,
159        '%list' => $this->formatter->formatList($names, 'family', $entity->id()),
160      ]);
161    }
162    return new FormattableMarkup(implode('<br>', $examples), []);
163  }
164
165}