3 namespace Drupal\redirect\Plugin\Field\FieldWidget;
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Field\WidgetBase;
9 use Drupal\Core\Form\FormStateInterface;
10 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
13 * Plugin implementation of the 'link' widget for the redirect module.
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.
19 * id = "redirect_source",
20 * label = @Translation("Redirect source"),
25 * "placeholder_url" = "",
26 * "placeholder_title" = ""
30 class RedirectSourceWidget extends WidgetBase {
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);
40 $element['path'] = array(
41 '#type' => 'textfield',
42 '#title' => $this->t('Path'),
43 '#placeholder' => $this->getSetting('placeholder_url'),
44 '#default_value' => $default_url_value,
46 '#required' => $element['#required'],
47 '#field_prefix' => Url::fromRoute('<front>', array(), array('absolute' => TRUE))->toString(),
48 '#attributes' => array('data-disable-refocus' => 'true'),
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>',
58 $source_path = $form_state->getValue(array('redirect_source', 0, 'path'));
60 $source_path = trim($source_path);
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.
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>';
70 catch (ResourceNotFoundException $e) {
71 // Do nothing, expected behaviour.
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;
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>';
88 $element['path']['#ajax'] = array(
89 'callback' => 'redirect_source_link_get_status_messages',
90 'wrapper' => 'redirect-link-status',
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'];