Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 57
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PatternPresetForm
0.00% covered (danger)
0.00%
0 / 57
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 1
 form
0.00% covered (danger)
0.00%
0 / 40
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
2
 save
0.00% covered (danger)
0.00%
0 / 11
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
20
 copyFormValuesToEntity
0.00% covered (danger)
0.00%
0 / 6
n/a
0 / 0
n/a
0 / 0
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\display_builder\Form;
6
7use Drupal\Component\Serialization\Exception\InvalidDataTypeException;
8use Drupal\Component\Serialization\Yaml;
9use Drupal\Core\Entity\EntityForm;
10use Drupal\Core\Entity\EntityInterface;
11use Drupal\Core\Form\FormStateInterface;
12use Drupal\display_builder\Entity\PatternPreset;
13
14/**
15 * Pattern preset form.
16 */
17final class PatternPresetForm extends EntityForm {
18
19  /**
20   * {@inheritdoc}
21   */
22  public function form(array $form, FormStateInterface $form_state): array {
23    $form = parent::form($form, $form_state);
24
25    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
26    $entity = $this->entity;
27
28    $form['label'] = [
29      '#type' => 'textfield',
30      '#title' => $this->t('Label'),
31      '#maxlength' => 255,
32      '#default_value' => $entity->label(),
33      '#required' => TRUE,
34    ];
35
36    $form['id'] = [
37      '#type' => 'machine_name',
38      '#default_value' => $entity->id(),
39      '#machine_name' => [
40        'exists' => [PatternPreset::class, 'load'],
41      ],
42      '#disabled' => !$entity->isNew(),
43    ];
44
45    $form['description'] = [
46      '#type' => 'textarea',
47      '#title' => $this->t('Description'),
48      '#default_value' => $entity->get('description'),
49      '#rows' => 2,
50    ];
51
52    $form['group'] = [
53      '#type' => 'textfield',
54      '#title' => $this->t('Group'),
55      '#default_value' => $entity->get('group'),
56    ];
57
58    $form['sources'] = [
59      '#type' => 'textarea',
60      '#title' => $this->t('Sources'),
61      '#default_value' => Yaml::encode($entity->get('sources') ?? []),
62      '#rows' => 16,
63    ];
64
65    $form['status'] = [
66      '#type' => 'checkbox',
67      '#title' => $this->t('Enabled'),
68      '#default_value' => $entity->status(),
69    ];
70
71    return $form;
72  }
73
74  /**
75   * {@inheritdoc}
76   */
77  public function save(array $form, FormStateInterface $form_state): int {
78    $result = parent::save($form, $form_state);
79    $message_args = ['%label' => $this->entity->label()];
80    $this->messenger()->addStatus(
81      match ($result) {
82        SAVED_NEW => $this->t('Created new preset %label.', $message_args),
83        SAVED_UPDATED => $this->t('Updated preset %label.', $message_args),
84        default => '',
85      }
86    );
87    $form_state->setRedirectUrl($this->entity->toUrl('collection'));
88
89    return $result;
90  }
91
92  /**
93   * {@inheritdoc}
94   */
95  protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state): void {
96    $sources = $form_state->getValue('sources');
97
98    try {
99      $sources = \is_string($sources) ? Yaml::decode($sources) : $sources;
100    }
101    catch (InvalidDataTypeException $e) {
102      // We do it here instead of FormInterface::validateForm() because it is
103      // the earliest call of EntityInterface::set().
104      $form_state->setErrorByName('sources', $this->t('The import failed with the following message: %message', ['%message' => $e->getMessage()]));
105    }
106    $form_state->setValue('sources', $sources);
107
108    parent::copyFormValuesToEntity($entity, $form, $form_state);
109  }
110
111}

Branches

Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once. Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement always has an else as part of its logical flow even if you didn't write one.