PathautoWidget.php in Pathauto 8
File
src/PathautoWidget.php
View source
<?php
namespace Drupal\pathauto;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\path\Plugin\Field\FieldWidget\PathWidget;
class PathautoWidget extends PathWidget {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$entity = $items
->getEntity();
$pattern = \Drupal::service('pathauto.generator')
->getPatternByEntity($entity);
if (empty($pattern)) {
$element['pathauto'] = [
'#type' => 'value',
'#value' => PathautoState::SKIP,
];
return $element;
}
if (\Drupal::currentUser()
->hasPermission('administer pathauto')) {
$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(),
]);
}
else {
$description = $this
->t('Uncheck this to create a custom alias below.');
}
$element['pathauto'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Generate automatic URL alias'),
'#default_value' => $entity->path->pathauto,
'#description' => $description,
'#weight' => -1,
];
$element['alias']['#states']['disabled']['input[name="path[' . $delta . '][pathauto]"]'] = [
'checked' => TRUE,
];
$element['alias']['#attached']['library'] = [
'pathauto/widget',
];
return $element;
}
}