PathautoWidget.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace Drupal\pathauto;
  3. use Drupal\Core\Field\FieldItemListInterface;
  4. use Drupal\path\Plugin\Field\FieldWidget\PathWidget;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\Url;
  7. /**
  8. * Extends the core path widget.
  9. */
  10. class PathautoWidget extends PathWidget {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  15. $element = parent::formElement($items, $delta, $element, $form, $form_state);
  16. $entity = $items->getEntity();
  17. // Taxonomy terms do not have an actual fieldset for path settings.
  18. // Merge in the defaults.
  19. // @todo Impossible to do this in widget, use another solution
  20. /*
  21. $form['path'] += array(
  22. '#type' => 'fieldset',
  23. '#title' => $this->t('URL path settings'),
  24. '#collapsible' => TRUE,
  25. '#collapsed' => empty($form['path']['alias']),
  26. '#group' => 'additional_settings',
  27. '#attributes' => array(
  28. 'class' => array('path-form'),
  29. ),
  30. '#access' => \Drupal::currentUser()->hasPermission('create url aliases') || \Drupal::currentUser()->hasPermission('administer url aliases'),
  31. '#weight' => 30,
  32. '#tree' => TRUE,
  33. '#element_validate' => array('path_form_element_validate'),
  34. );*/
  35. $pattern = \Drupal::service('pathauto.generator')->getPatternByEntity($entity);
  36. if (empty($pattern)) {
  37. return $element;
  38. }
  39. if (\Drupal::currentUser()->hasPermission('administer pathauto')) {
  40. $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()]);
  41. }
  42. else {
  43. $description = $this->t('Uncheck this to create a custom alias below.');
  44. }
  45. $element['pathauto'] = array(
  46. '#type' => 'checkbox',
  47. '#title' => $this->t('Generate automatic URL alias'),
  48. '#default_value' => $entity->path->pathauto,
  49. '#description' => $description,
  50. '#weight' => -1,
  51. );
  52. // Add JavaScript that will disable the path textfield when the automatic
  53. // alias checkbox is checked.
  54. $element['alias']['#states']['disabled']['input[name="path[' . $delta . '][pathauto]"]'] = array('checked' => TRUE);
  55. // Override path.module's vertical tabs summary.
  56. $element['alias']['#attached']['library'] = ['pathauto/widget'];
  57. return $element;
  58. }
  59. }