Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / redirect / src / Plugin / Field / FieldWidget / RedirectSourceWidget.php
1 <?php
2
3 namespace Drupal\redirect\Plugin\Field\FieldWidget;
4
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\WidgetBase;
8 use Drupal\Core\Url;
9 use Drupal\Core\Form\FormStateInterface;
10 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
11
12 /**
13  * Plugin implementation of the 'link' widget for the redirect module.
14  *
15  * Note that this field is meant only for the source field of the redirect
16  * entity as it drops validation for non existing paths.
17  *
18  * @FieldWidget(
19  *   id = "redirect_source",
20  *   label = @Translation("Redirect source"),
21  *   field_types = {
22  *     "link"
23  *   },
24  *   settings = {
25  *     "placeholder_url" = "",
26  *     "placeholder_title" = ""
27  *   }
28  * )
29  */
30 class RedirectSourceWidget extends WidgetBase {
31
32   /**
33    * {@inheritdoc}
34    */
35   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
36     $default_url_value = $items[$delta]->path;
37     if ($items[$delta]->query) {
38       $default_url_value .= '?' . http_build_query($items[$delta]->query);
39     }
40     $element['path'] = array(
41       '#type' => 'textfield',
42       '#title' => $this->t('Path'),
43       '#placeholder' => $this->getSetting('placeholder_url'),
44       '#default_value' => $default_url_value,
45       '#maxlength' => 2048,
46       '#required' => $element['#required'],
47       '#field_prefix' => Url::fromRoute('<front>', array(), array('absolute' => TRUE))->toString(),
48       '#attributes' => array('data-disable-refocus' => 'true'),
49     );
50
51     // If creating new URL add checks.
52     if ($items->getEntity()->isNew()) {
53       $element['status_box'] = array(
54         '#prefix' => '<div id="redirect-link-status">',
55         '#suffix' => '</div>',
56       );
57
58       $source_path = $form_state->getValue(array('redirect_source', 0, 'path'));
59       if ($source_path) {
60         $source_path = trim($source_path);
61
62         // Warning about creating a redirect from a valid path.
63         // @todo - Hmm... exception driven logic. Find a better way how to
64         //   determine if we have a valid path.
65         try {
66           \Drupal::service('router')->match('/' . $form_state->getValue(array('redirect_source', 0, 'path')));
67           $element['status_box'][]['#markup'] = '<div class="messages messages--warning">' . $this->t('The source path %path is likely a valid path. It is preferred to <a href="@url-alias">create URL aliases</a> for existing paths rather than redirects.',
68               array('%path' => $source_path, '@url-alias' => Url::fromRoute('path.admin_add')->toString())) . '</div>';
69         }
70         catch (ResourceNotFoundException $e) {
71           // Do nothing, expected behaviour.
72         }
73
74         // Warning about the path being already redirected.
75         $parsed_url = UrlHelper::parse($source_path);
76         $path = isset($parsed_url['path']) ? $parsed_url['path'] : NULL;
77         if (!empty($path)) {
78           /** @var \Drupal\redirect\RedirectRepository $repository */
79           $repository = \Drupal::service('redirect.repository');
80           $redirects = $repository->findBySourcePath($path);
81           if (!empty($redirects)) {
82             $redirect = array_shift($redirects);
83             $element['status_box'][]['#markup'] = '<div class="messages messages--warning">' . $this->t('The base source path %source is already being redirected. Do you want to <a href="@edit-page">edit the existing redirect</a>?', array('%source' => $source_path, '@edit-page' => $redirect->url('edit-form'))) . '</div>';
84           }
85         }
86       }
87
88       $element['path']['#ajax'] = array(
89         'callback' => 'redirect_source_link_get_status_messages',
90         'wrapper' => 'redirect-link-status',
91       );
92     }
93
94     return $element;
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
101     $values = parent::massageFormValues($values, $form, $form_state);
102     // It is likely that the url provided for this field is not existing and
103     // so the logic in the parent method did not set any defaults. Just run
104     // through all url values and add defaults.
105     foreach ($values as &$value) {
106       if (!empty($value['path'])) {
107         // In case we have query process the url.
108         if (strpos($value['path'], '?') !== FALSE) {
109           $url = UrlHelper::parse($value['path']);
110           $value['path'] = $url['path'];
111           $value['query'] = $url['query'];
112         }
113       }
114     }
115     return $values;
116   }
117 }