13ab1f10767c28f1e4ca26a772f2af5f83ccd8c0
[yaffs-website] / web / core / modules / path / src / Plugin / Field / FieldWidget / PathWidget.php
1 <?php
2
3 namespace Drupal\path\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\Core\Field\WidgetBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Symfony\Component\Validator\ConstraintViolationInterface;
9
10 /**
11  * Plugin implementation of the 'path' widget.
12  *
13  * @FieldWidget(
14  *   id = "path",
15  *   label = @Translation("URL alias"),
16  *   field_types = {
17  *     "path"
18  *   }
19  * )
20  */
21 class PathWidget extends WidgetBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
27     $entity = $items->getEntity();
28
29     $element += [
30       '#element_validate' => [[get_class($this), 'validateFormElement']],
31     ];
32     $element['alias'] = [
33       '#type' => 'textfield',
34       '#title' => $element['#title'],
35       '#default_value' => $items[$delta]->alias,
36       '#required' => $element['#required'],
37       '#maxlength' => 255,
38       '#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "/about" when writing an about page.'),
39     ];
40     $element['pid'] = [
41       '#type' => 'value',
42       '#value' => $items[$delta]->pid,
43     ];
44     $element['source'] = [
45       '#type' => 'value',
46       '#value' => !$entity->isNew() ? '/' . $entity->toUrl()->getInternalPath() : NULL,
47      ];
48     $element['langcode'] = [
49       '#type' => 'value',
50       '#value' => $items[$delta]->langcode,
51     ];
52     return $element;
53   }
54
55   /**
56    * Form element validation handler for URL alias form element.
57    *
58    * @param array $element
59    *   The form element.
60    * @param \Drupal\Core\Form\FormStateInterface $form_state
61    *   The form state.
62    */
63   public static function validateFormElement(array &$element, FormStateInterface $form_state) {
64     // Trim the submitted value of whitespace and slashes.
65     $alias = rtrim(trim($element['alias']['#value']), " \\/");
66     if (!empty($alias)) {
67       $form_state->setValueForElement($element['alias'], $alias);
68
69       // Validate that the submitted alias does not exist yet.
70       $is_exists = \Drupal::service('path.alias_storage')->aliasExists($alias, $element['langcode']['#value'], $element['source']['#value']);
71       if ($is_exists) {
72         $form_state->setError($element['alias'], t('The alias is already in use.'));
73       }
74     }
75
76     if ($alias && $alias[0] !== '/') {
77       $form_state->setError($element['alias'], t('The alias needs to start with a slash.'));
78     }
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
85     return $element['alias'];
86   }
87
88 }