PathWidget.php 3.25 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\path\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
7
use Drupal\Core\Form\FormStateInterface;
8
use Drupal\Core\Language\LanguageInterface;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
use Symfony\Component\Validator\ConstraintViolationInterface;

/**
 * Plugin implementation of the 'path' widget.
 *
 * @FieldWidget(
 *   id = "path",
 *   label = @Translation("URL alias"),
 *   field_types = {
 *     "path"
 *   }
 * )
 */
class PathWidget extends WidgetBase {

  /**
   * {@inheritdoc}
   */
27
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
28 29 30
    $entity = $items->getEntity();
    $path = array();
    if (!$entity->isNew()) {
31
      $conditions = array('source' => '/' . $entity->urlInfo()->getInternalPath());
32
      if ($items->getLangcode() != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
33 34 35 36 37 38 39 40 41
        $conditions['langcode'] = $items->getLangcode();
      }
      $path = \Drupal::service('path.alias_storage')->load($conditions);
      if ($path === FALSE) {
        $path = array();
      }
    }
    $path += array(
      'pid' => NULL,
42
      'source' => !$entity->isNew() ? '/' . $entity->urlInfo()->getInternalPath() : NULL,
43 44 45 46 47 48 49 50 51 52 53 54 55
      'alias' => '',
      'langcode' => $items->getLangcode(),
    );

    $element += array(
      '#element_validate' => array(array(get_class($this), 'validateFormElement')),
    );
    $element['alias'] = array(
      '#type' => 'textfield',
      '#title' => $element['#title'],
      '#default_value' => $path['alias'],
      '#required' => $element['#required'],
      '#maxlength' => 255,
56
      '#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "/about" when writing an about page.'),
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    );
    $element['pid'] = array(
      '#type' => 'value',
      '#value' => $path['pid'],
    );
    $element['source'] = array(
      '#type' => 'value',
      '#value' => $path['source'],
    );
    $element['langcode'] = array(
      '#type' => 'value',
      '#value' => $path['langcode'],
    );
    return $element;
  }

  /**
   * Form element validation handler for URL alias form element.
   *
   * @param array $element
   *   The form element.
78
   * @param \Drupal\Core\Form\FormStateInterface $form_state
79 80
   *   The form state.
   */
81
  public static function validateFormElement(array &$element, FormStateInterface $form_state) {
82
    // Trim the submitted value of whitespace and slashes.
83
    $alias = rtrim(trim($element['alias']['#value']), " \\/");
84
    if (!empty($alias)) {
85
      $form_state->setValueForElement($element['alias'], $alias);
86 87 88 89

      // Validate that the submitted alias does not exist yet.
      $is_exists = \Drupal::service('path.alias_storage')->aliasExists($alias, $element['langcode']['#value'], $element['source']['#value']);
      if ($is_exists) {
90
        $form_state->setError($element, t('The alias is already in use.'));
91 92
      }
    }
93 94 95 96

    if ($alias && $alias[0] !== '/') {
      $form_state->setError($element, t('The alias needs to start with a slash.'));
    }
97 98 99 100 101
  }

  /**
   * {@inheritdoc}
   */
102
  public function errorElement(array $element, ConstraintViolationInterface $violation, array $form, FormStateInterface $form_state) {
103 104 105 106
    return $element['alias'];
  }

}