Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
75 / 75
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
NameListFormatForm
100.00% covered (success)
100.00%
75 / 75
100.00% covered (success)
100.00%
7 / 7
8
100.00% covered (success)
100.00%
1 / 1
 create
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 exists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 form
100.00% covered (success)
100.00%
58 / 58
100.00% covered (success)
100.00%
1 / 1
1
 actions
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 save
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 delete
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\name\Form;
6
7use Drupal\Core\Entity\EntityForm;
8use Drupal\Core\Form\FormStateInterface;
9use Drupal\name\Entity\NameListFormat;
10use Drupal\name\Service\NameFormatterInterface;
11use Symfony\Component\DependencyInjection\ContainerInterface;
12
13/**
14 * Provides a form controller for name list formats.
15 */
16class NameListFormatForm extends EntityForm {
17
18  /**
19   * The name formatter.
20   *
21   * @var \Drupal\name\Service\NameFormatterInterface
22   */
23  protected $formatter;
24
25  /**
26   * {@inheritdoc}
27   */
28  public static function create(ContainerInterface $container) {
29    return new static(
30      $container->get('name.formatter')
31    );
32  }
33
34  /**
35   * Constructs a new NameListFormatForm object.
36   *
37   * @param \Drupal\name\Service\NameFormatterInterface $formatter
38   *   The name formatter.
39   */
40  public function __construct(NameFormatterInterface $formatter) {
41    $this->formatter = $formatter;
42  }
43
44  /**
45   * {@inheritdoc}
46   */
47  public function exists($entity_id, array $element, FormStateInterface $form_state) {
48    return NameListFormat::load($entity_id);
49  }
50
51  /**
52   * {@inheritdoc}
53   */
54  public function form(array $form, FormStateInterface $form_state) {
55    $element = parent::form($form, $form_state);
56
57    $element['label'] = [
58      '#type' => 'textfield',
59      '#title' => $this->t('Name'),
60      '#default_value' => $this->entity->label(),
61      '#maxlength' => 255,
62      '#required' => TRUE,
63    ];
64
65    $element['id'] = [
66      '#type' => 'machine_name',
67      '#title' => $this->t('Machine-readable name'),
68      '#description' => $this->t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'),
69      '#disabled' => !$this->entity->isNew(),
70      '#default_value' => $this->entity->id(),
71      '#machine_name' => [
72        'exists' => [$this, 'exists'],
73      ],
74    ];
75
76    $element['delimiter'] = [
77      '#type' => 'textfield',
78      '#title' => $this->t('Delimiter'),
79      '#default_value' => $this->entity->delimiter,
80      '#description' => $this->t('This specifies the delimiter between the second to last and the last name.'),
81    ];
82    $element['and'] = [
83      '#type' => 'radios',
84      '#title' => $this->t('Last delimiter type'),
85      '#options' => $this->formatter->getLastDelimiterTypes(),
86      '#default_value' => $this->entity->and,
87      '#description' => $this->t('This specifies the delimiter between the second to last and the last name.'),
88      '#required' => TRUE,
89    ];
90    $element['delimiter_precedes_last'] = [
91      '#type' => 'radios',
92      '#title' => $this->t('Standard delimiter precedes last delimiter'),
93      '#options' => $this->formatter->getLastDelimiterBehaviors(),
94      '#default_value' => $this->entity->delimiter_precedes_last,
95      '#description' => $this->t('This specifies the delimiter between the second to last and the last name. Contextual means that the delimiter is only included for lists with three or more names.'),
96      '#required' => TRUE,
97    ];
98    $options = range(1, 20);
99    $options = array_combine($options, $options);
100    $element['el_al_min'] = [
101      '#type' => 'select',
102      '#title' => $this->t('Reduce list and append <em>el al</em>'),
103      '#options' => [0 => $this->t('Never reduce')] + $options,
104      '#default_value' => $this->entity->el_al_min,
105      '#description' => $this->t('This specifies a limit on the number of names to display. After this limit, names are removed and the abbreviation <em>et al</em> is appended. This Latin abbreviation of <em>et alii</em> means "and others".'),
106      '#required' => TRUE,
107    ];
108    $element['el_al_first'] = [
109      '#type' => 'select',
110      '#title' => $this->t('Number of names to display when using <em>el al</em>'),
111      '#options' => $options,
112      '#default_value' => $this->entity->el_al_first,
113      '#required' => TRUE,
114    ];
115
116    return $element;
117  }
118
119  /**
120   * {@inheritdoc}
121   */
122  protected function actions(array $form, FormStateInterface $form_state) {
123    $actions = parent::actions($form, $form_state);
124    $actions['submit']['#value'] = $this->t('Save list format');
125    return $actions;
126  }
127
128  /**
129   * {@inheritdoc}
130   */
131  public function save(array $form, FormStateInterface $form_state) {
132    $form_state->setRedirect('name.name_list_format_list');
133    if ($this->entity->isNew()) {
134      $this->messenger()->addMessage($this->t('Name list format %label added.', ['%label' => $this->entity->label()]));
135      return $this->entity->save();
136    }
137
138    $this->messenger()->addMessage($this->t('Name list format %label has been updated.', ['%label' => $this->entity->label()]));
139
140    return $this->entity->save();
141  }
142
143  /**
144   * {@inheritdoc}
145   */
146  public function delete(array $form, FormStateInterface $form_state) {
147    $form_state->setRedirect('entity.name_list_format.delete_form', [
148      'name_list_format' => $this->entity->id(),
149    ]);
150  }
151
152}