Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / pathauto / src / PathautoWidget.php
1 <?php
2
3 namespace Drupal\pathauto;
4
5 use Drupal\Core\Field\FieldItemListInterface;
6 use Drupal\path\Plugin\Field\FieldWidget\PathWidget;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\Url;
9
10 /**
11  * Extends the core path widget.
12  */
13 class PathautoWidget extends PathWidget {
14
15   /**
16    * {@inheritdoc}
17    */
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();
21
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
25     /*
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'),
34       ),
35       '#access' => \Drupal::currentUser()->hasPermission('create url aliases') || \Drupal::currentUser()->hasPermission('administer url aliases'),
36       '#weight' => 30,
37       '#tree' => TRUE,
38       '#element_validate' => array('path_form_element_validate'),
39     );*/
40
41     $pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
42     if (empty($pattern)) {
43       return $element;
44     }
45
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()]);
48     }
49     else {
50       $description = $this->t('Uncheck this to create a custom alias below.');
51     }
52
53     $element['pathauto'] = array(
54       '#type' => 'checkbox',
55       '#title' => $this->t('Generate automatic URL alias'),
56       '#default_value' => $entity->path->pathauto,
57       '#description' => $description,
58       '#weight' => -1,
59     );
60
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);
64
65     // Override path.module's vertical tabs summary.
66     $element['alias']['#attached']['library'] = ['pathauto/widget'];
67
68     return $element;
69   }
70
71 }