Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
44 / 44 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| NameFormatForm | |
100.00% |
44 / 44 |
|
100.00% |
7 / 7 |
8 | |
100.00% |
1 / 1 |
| create | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| exists | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| form | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
1 | |||
| actions | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| save | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Drupal\name\Form; |
| 6 | |
| 7 | use Drupal\Core\Entity\EntityForm; |
| 8 | use Drupal\Core\Form\FormStateInterface; |
| 9 | use Drupal\name\Entity\NameFormat; |
| 10 | use Drupal\name\Service\NameFormatParserInterface; |
| 11 | use Drupal\name\Utility\NameFormatHelp; |
| 12 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 13 | |
| 14 | /** |
| 15 | * Provides a base form controller for date formats. |
| 16 | */ |
| 17 | class NameFormatForm extends EntityForm { |
| 18 | |
| 19 | /** |
| 20 | * The name format parser for token help. |
| 21 | * |
| 22 | * @var \Drupal\name\Service\NameFormatParserInterface |
| 23 | */ |
| 24 | protected $parser; |
| 25 | |
| 26 | /** |
| 27 | * {@inheritdoc} |
| 28 | */ |
| 29 | public static function create(ContainerInterface $container) { |
| 30 | return new static( |
| 31 | $container->get('name.format_parser') |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Constructs a new NameListFormatForm object. |
| 37 | * |
| 38 | * @param \Drupal\name\Service\NameFormatParserInterface $parser |
| 39 | * The name format parser. |
| 40 | */ |
| 41 | public function __construct(NameFormatParserInterface $parser) { |
| 42 | $this->parser = $parser; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * {@inheritdoc} |
| 47 | */ |
| 48 | public function exists($entity_id, array $element, FormStateInterface $form_state) { |
| 49 | return NameFormat::load($entity_id); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * {@inheritdoc} |
| 54 | */ |
| 55 | public function form(array $form, FormStateInterface $form_state) { |
| 56 | $element = parent::form($form, $form_state); |
| 57 | |
| 58 | $element['label'] = [ |
| 59 | '#type' => 'textfield', |
| 60 | '#title' => $this->t('Name'), |
| 61 | '#default_value' => $this->entity->label(), |
| 62 | '#maxlength' => 255, |
| 63 | '#required' => TRUE, |
| 64 | ]; |
| 65 | |
| 66 | $element['id'] = [ |
| 67 | '#type' => 'machine_name', |
| 68 | '#title' => $this->t('Machine-readable name'), |
| 69 | '#description' => $this->t('A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores.'), |
| 70 | '#disabled' => !$this->entity->isNew(), |
| 71 | '#default_value' => $this->entity->id(), |
| 72 | '#machine_name' => [ |
| 73 | 'exists' => [$this, 'exists'], |
| 74 | ], |
| 75 | ]; |
| 76 | |
| 77 | $element['pattern'] = [ |
| 78 | '#type' => 'textfield', |
| 79 | '#title' => $this->t('Format'), |
| 80 | '#default_value' => $this->entity->get('pattern'), |
| 81 | '#maxlength' => 255, |
| 82 | '#required' => TRUE, |
| 83 | ]; |
| 84 | |
| 85 | $element['help'] = NameFormatHelp::renderableTokenHelp(); |
| 86 | |
| 87 | return $element; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * {@inheritdoc} |
| 92 | */ |
| 93 | protected function actions(array $form, FormStateInterface $form_state) { |
| 94 | $actions = parent::actions($form, $form_state); |
| 95 | $actions['submit']['#value'] = $this->t('Save format'); |
| 96 | return $actions; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * {@inheritdoc} |
| 101 | */ |
| 102 | public function delete(array $form, FormStateInterface $form_state) { |
| 103 | $form_state->setRedirect('entity.name_format.delete_form', [ |
| 104 | 'name_format' => $this->entity->id(), |
| 105 | ]); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * {@inheritdoc} |
| 110 | */ |
| 111 | public function save(array $form, FormStateInterface $form_state) { |
| 112 | $form_state->setRedirect('name.name_format_list'); |
| 113 | if ($this->entity->isNew()) { |
| 114 | $this->messenger()->addMessage($this->t('Name format %label added.', ['%label' => $this->entity->label()])); |
| 115 | return $this->entity->save(); |
| 116 | } |
| 117 | |
| 118 | $this->messenger()->addMessage($this->t('Name format %label has been updated.', ['%label' => $this->entity->label()])); |
| 119 | |
| 120 | return $this->entity->save(); |
| 121 | } |
| 122 | |
| 123 | } |