Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
39 / 39 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| NameFieldBuilder | |
100.00% |
39 / 39 |
|
100.00% |
6 / 6 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| create | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| build | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
5 | |||
| buildConfigurationForm | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| submitConfigurationForm | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| defaultConfiguration | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\name\Plugin\diff\Field; |
| 6 | |
| 7 | use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 8 | use Drupal\Core\Field\FieldItemListInterface; |
| 9 | use Drupal\Core\Form\FormStateInterface; |
| 10 | use Drupal\diff\DiffEntityParser; |
| 11 | use Drupal\diff\FieldDiffBuilderBase; |
| 12 | use Drupal\name\Service\FormatOptionInterface; |
| 13 | use Drupal\name\Service\NameFormatterInterface; |
| 14 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 15 | |
| 16 | /** |
| 17 | * Plugin to diff text fields. |
| 18 | * |
| 19 | * @FieldDiffBuilder( |
| 20 | * id = "name_field_diff_builder", |
| 21 | * label = @Translation("Name Field"), |
| 22 | * field_types = { |
| 23 | * "name" |
| 24 | * }, |
| 25 | * ) |
| 26 | */ |
| 27 | class NameFieldBuilder extends FieldDiffBuilderBase { |
| 28 | |
| 29 | /** |
| 30 | * The name formatter. |
| 31 | */ |
| 32 | protected NameFormatterInterface $formatter; |
| 33 | |
| 34 | /** |
| 35 | * The name format options service. |
| 36 | */ |
| 37 | protected ?FormatOptionInterface $formatOptions; |
| 38 | |
| 39 | /** |
| 40 | * Constructs a Name Field diff builder instance. |
| 41 | * |
| 42 | * @param array $configuration |
| 43 | * A configuration array containing information about the plugin instance. |
| 44 | * @param string $plugin_id |
| 45 | * The plugin_id for the plugin instance. |
| 46 | * @param mixed $plugin_definition |
| 47 | * The plugin implementation definition. |
| 48 | * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager |
| 49 | * The entity type manager. |
| 50 | * @param \Drupal\diff\DiffEntityParser $entity_parser |
| 51 | * The entity parser. |
| 52 | * @param \Drupal\name\Service\NameFormatterInterface $formatter |
| 53 | * The name formatter. |
| 54 | * @param \Drupal\name\Service\FormatOptionInterface|null $format_options |
| 55 | * The name format options service. |
| 56 | */ |
| 57 | public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, DiffEntityParser $entity_parser, NameFormatterInterface $formatter, ?FormatOptionInterface $format_options) { |
| 58 | parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_parser); |
| 59 | $this->formatter = $formatter; |
| 60 | $this->formatOptions = $format_options; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * {@inheritdoc} |
| 65 | */ |
| 66 | public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
| 67 | return new static( |
| 68 | $configuration, |
| 69 | $plugin_id, |
| 70 | $plugin_definition, |
| 71 | $container->get('entity_type.manager'), |
| 72 | $container->get('diff.entity_parser'), |
| 73 | $container->get('name.formatter'), |
| 74 | $container->get('name.format_options', ContainerInterface::NULL_ON_INVALID_REFERENCE) |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * {@inheritdoc} |
| 80 | */ |
| 81 | public function build(FieldItemListInterface $field_items): array { |
| 82 | $result = []; |
| 83 | if ($this->configuration['compare_format']) { |
| 84 | foreach ($field_items as $item) { |
| 85 | $result[] = (string) $this->formatter->format($item->filteredArray(), $this->configuration['compare_format']); |
| 86 | } |
| 87 | return $result; |
| 88 | } |
| 89 | |
| 90 | foreach ($field_items as $item) { |
| 91 | $output = []; |
| 92 | $values = $item->toArray(); |
| 93 | foreach ($item->activeComponents() as $key => $label) { |
| 94 | $output[] = "$label: $values[$key]"; |
| 95 | } |
| 96 | $result[] = implode("\n", $output); |
| 97 | } |
| 98 | return $result; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * {@inheritdoc} |
| 103 | */ |
| 104 | public function buildConfigurationForm(array $form, FormStateInterface $form_state): array { |
| 105 | $form['compare_format'] = [ |
| 106 | '#type' => 'select', |
| 107 | '#title' => $this->t('Name format'), |
| 108 | '#default_value' => $this->configuration['compare_format'], |
| 109 | '#options' => $this->formatOptions?->getCustomFormatOptions() ?? [], |
| 110 | '#empty_option' => $this->t('-- components --'), |
| 111 | ]; |
| 112 | |
| 113 | return parent::buildConfigurationForm($form, $form_state); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * {@inheritdoc} |
| 118 | */ |
| 119 | public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void { |
| 120 | $this->configuration['compare_format'] = $form_state->getValue('compare_format'); |
| 121 | |
| 122 | parent::submitConfigurationForm($form, $form_state); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * {@inheritdoc} |
| 127 | */ |
| 128 | public function defaultConfiguration(): array { |
| 129 | $default_settings = [ |
| 130 | 'compare_format' => '', |
| 131 | ]; |
| 132 | $default_settings += parent::defaultConfiguration(); |
| 133 | |
| 134 | return $default_settings; |
| 135 | } |
| 136 | |
| 137 | } |