PathWidget.php 3.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php

/**
 * @file
 * Contains \Drupal\path\Plugin\Field\FieldWidget\PathWidget.
 */

namespace Drupal\path\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
12
use Drupal\Core\Form\FormStateInterface;
13
use Drupal\Core\Language\LanguageInterface;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
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}
   */
32
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
33 34 35
    $entity = $items->getEntity();
    $path = array();
    if (!$entity->isNew()) {
36
      $conditions = array('source' => $entity->urlInfo()->getInternalPath());
37
      if ($items->getLangcode() != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
38 39 40 41 42 43 44 45 46
        $conditions['langcode'] = $items->getLangcode();
      }
      $path = \Drupal::service('path.alias_storage')->load($conditions);
      if ($path === FALSE) {
        $path = array();
      }
    }
    $path += array(
      'pid' => NULL,
47
      'source' => !$entity->isNew() ? $entity->urlInfo()->getInternalPath() : NULL,
48 49 50 51 52 53 54 55 56 57 58 59 60
      '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,
61
      '#description' => $this->t('The alternative URL for this content. Use a relative path. For example, enter "about" for the about page.'),
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    );
    $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.
83
   * @param \Drupal\Core\Form\FormStateInterface $form_state
84 85
   *   The form state.
   */
86
  public static function validateFormElement(array &$element, FormStateInterface $form_state) {
87 88
    // Trim the submitted value of whitespace and slashes.
    $alias = trim(trim($element['alias']['#value']), " \\/");
89
    if (!empty($alias)) {
90
      $form_state->setValueForElement($element['alias'], $alias);
91 92 93 94

      // 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) {
95
        $form_state->setError($element, t('The alias is already in use.'));
96 97 98 99 100 101 102
      }
    }
  }

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

}