3 namespace Drupal\pathauto;
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\path\Plugin\Field\FieldWidget\PathWidget;
7 use Drupal\Core\Form\FormStateInterface;
11 * Extends the core path widget.
13 class PathautoWidget extends PathWidget {
18 public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
19 $element = parent::formElement($items, $delta, $element, $form, $form_state);
20 $entity = $items->getEntity();
22 // Taxonomy terms do not have an actual fieldset for path settings.
23 // Merge in the defaults.
24 // @todo Impossible to do this in widget, use another solution
26 $form['path'] += array(
27 '#type' => 'fieldset',
28 '#title' => $this->t('URL path settings'),
29 '#collapsible' => TRUE,
30 '#collapsed' => empty($form['path']['alias']),
31 '#group' => 'additional_settings',
32 '#attributes' => array(
33 'class' => array('path-form'),
35 '#access' => \Drupal::currentUser()->hasPermission('create url aliases') || \Drupal::currentUser()->hasPermission('administer url aliases'),
38 '#element_validate' => array('path_form_element_validate'),
41 $pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
42 if (empty($pattern)) {
46 if (\Drupal::currentUser()->hasPermission('administer pathauto')) {
47 $description = $this->t('Uncheck this to create a custom alias below. <a href="@admin_link">Configure URL alias patterns.</a>', ['@admin_link' => Url::fromRoute('entity.pathauto_pattern.collection')->toString()]);
50 $description = $this->t('Uncheck this to create a custom alias below.');
53 $element['pathauto'] = array(
54 '#type' => 'checkbox',
55 '#title' => $this->t('Generate automatic URL alias'),
56 '#default_value' => $entity->path->pathauto,
57 '#description' => $description,
61 // Add JavaScript that will disable the path textfield when the automatic
62 // alias checkbox is checked.
63 $element['alias']['#states']['disabled']['input[name="path[' . $delta . '][pathauto]"]'] = array('checked' => TRUE);
65 // Override path.module's vertical tabs summary.
66 $element['alias']['#attached']['library'] = ['pathauto/widget'];