Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
48 / 48 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| NameFormatListBuilder | |
100.00% |
48 / 48 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
| createInstance | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| buildHeader | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| buildRow | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
| examples | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 | |||
| render | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\name\ListBuilder; |
| 6 | |
| 7 | use Drupal\Component\Render\FormattableMarkup; |
| 8 | use Drupal\Core\Config\Entity\ConfigEntityListBuilder; |
| 9 | use Drupal\Core\Entity\EntityInterface; |
| 10 | use Drupal\Core\Entity\EntityStorageInterface; |
| 11 | use Drupal\Core\Entity\EntityTypeInterface; |
| 12 | use Drupal\name\Entity\NameFormat; |
| 13 | use Drupal\name\Service\GeneratorInterface; |
| 14 | use Drupal\name\Service\NameFormatParserInterface; |
| 15 | use Drupal\name\Service\NameFormatterInterface; |
| 16 | use Drupal\name\Utility\NameFormatHelp; |
| 17 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 18 | |
| 19 | /** |
| 20 | * Name format list builder for the admin page. |
| 21 | */ |
| 22 | class NameFormatListBuilder extends ConfigEntityListBuilder { |
| 23 | |
| 24 | /** |
| 25 | * The name formatter. |
| 26 | * |
| 27 | * @var \Drupal\name\Service\NameFormatterInterface |
| 28 | */ |
| 29 | protected $formatter; |
| 30 | |
| 31 | /** |
| 32 | * The name format parser. |
| 33 | * |
| 34 | * @var \Drupal\name\Service\NameFormatParserInterface |
| 35 | */ |
| 36 | protected $parser; |
| 37 | |
| 38 | /** |
| 39 | * The name generator. |
| 40 | * |
| 41 | * @var \Drupal\name\Service\GeneratorInterface |
| 42 | */ |
| 43 | protected $generator; |
| 44 | |
| 45 | /** |
| 46 | * The names that were used to generate the list. |
| 47 | * |
| 48 | * @var array |
| 49 | */ |
| 50 | protected $names; |
| 51 | |
| 52 | /** |
| 53 | * {@inheritdoc} |
| 54 | */ |
| 55 | public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) { |
| 56 | return new static( |
| 57 | $entity_type, |
| 58 | $container->get('entity_type.manager')->getStorage($entity_type->id()), |
| 59 | $container->get('name.format_parser'), |
| 60 | $container->get('name.generator'), |
| 61 | $container->get('name.formatter') |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Constructs a new EntityListBuilder object. |
| 67 | * |
| 68 | * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type |
| 69 | * The entity type definition. |
| 70 | * @param \Drupal\Core\Entity\EntityStorageInterface $storage |
| 71 | * The entity storage class. |
| 72 | * @param \Drupal\name\Service\NameFormatParserInterface $parser |
| 73 | * The name format parser. |
| 74 | * @param \Drupal\name\Service\GeneratorInterface $generator |
| 75 | * The name generator. |
| 76 | * @param \Drupal\name\Service\NameFormatterInterface|null $formatter |
| 77 | * The name formatter, or NULL to load from the global container (backward |
| 78 | * compatibility with legacy four-argument construction). Prefer |
| 79 | * injection via createInstance(). |
| 80 | */ |
| 81 | public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, NameFormatParserInterface $parser, GeneratorInterface $generator, ?NameFormatterInterface $formatter = NULL) { |
| 82 | parent::__construct($entity_type, $storage); |
| 83 | // @phpstan-ignore-next-line |
| 84 | $this->formatter = $formatter ?? \Drupal::service('name.formatter'); |
| 85 | $this->parser = $parser; |
| 86 | $this->generator = $generator; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * {@inheritdoc} |
| 91 | */ |
| 92 | public function buildHeader() { |
| 93 | $row = []; |
| 94 | $row['label'] = $this->t('Label'); |
| 95 | $row['id'] = $this->t('Machine name'); |
| 96 | $row['format'] = $this->t('Format'); |
| 97 | $row['examples'] = $this->t('Examples'); |
| 98 | $row['operations'] = $this->t('Operations'); |
| 99 | return $row; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * {@inheritdoc} |
| 104 | */ |
| 105 | public function buildRow(EntityInterface $entity) { |
| 106 | if (!$entity instanceof NameFormat) { |
| 107 | throw new \LogicException(sprintf( |
| 108 | 'Expected %s in %s::buildRow(), got %s.', |
| 109 | NameFormat::class, |
| 110 | static::class, |
| 111 | get_debug_type($entity), |
| 112 | )); |
| 113 | } |
| 114 | |
| 115 | $row = []; |
| 116 | $row['label'] = $entity->label(); |
| 117 | $row['id'] = $entity->id(); |
| 118 | $row['format'] = $entity->get('pattern'); |
| 119 | $row['examples'] = $this->examples($entity); |
| 120 | $operations = $this->buildOperations($entity); |
| 121 | $row['operations']['data'] = $operations; |
| 122 | return $row; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Provides some example based on names with various components set. |
| 127 | * |
| 128 | * @param \Drupal\name\Entity\NameFormat $entity |
| 129 | * The name format entity. |
| 130 | * |
| 131 | * @return \Drupal\Component\Render\FormattableMarkup |
| 132 | * The example names with formatting applied. |
| 133 | */ |
| 134 | protected function examples(NameFormat $entity) { |
| 135 | $this->names = $this->generator->loadSampleValues(4); |
| 136 | $examples = []; |
| 137 | foreach ($this->names as $index => $example_name) { |
| 138 | $formatted = $this->formatter->format($example_name, $entity->id()); |
| 139 | if (!strlen((string) $formatted)) { |
| 140 | $formatted = $this->t('<<@empty>>', ['@empty' => $this->t('empty')]); |
| 141 | } |
| 142 | $examples[] = $this->t('(@num) %name', [ |
| 143 | '@num' => $index + 1, |
| 144 | '%name' => $formatted, |
| 145 | ]); |
| 146 | } |
| 147 | return new FormattableMarkup(implode('<br>', $examples), []); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * {@inheritdoc} |
| 152 | */ |
| 153 | public function render() { |
| 154 | return [ |
| 155 | 'list' => parent::render(), |
| 156 | 'help' => NameFormatHelp::renderableTokenHelp(), |
| 157 | ]; |
| 158 | } |
| 159 | |
| 160 | } |