PathautoItem.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Drupal\pathauto;
  3. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  4. use Drupal\Core\TypedData\DataDefinition;
  5. use Drupal\path\Plugin\Field\FieldType\PathItem;
  6. /**
  7. * Extends the default PathItem implementation to generate aliases.
  8. */
  9. class PathautoItem extends PathItem {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
  14. $properties = parent::propertyDefinitions($field_definition);
  15. $properties['pathauto'] = DataDefinition::create('integer')
  16. ->setLabel(t('Pathauto state'))
  17. ->setDescription(t('Whether an automated alias should be created or not.'))
  18. ->setComputed(TRUE)
  19. ->setClass('\Drupal\pathauto\PathautoState');
  20. return $properties;
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function postSave($update) {
  26. // Only allow the parent implementation to act if pathauto will not create
  27. // an alias.
  28. if ($this->pathauto == PathautoState::SKIP) {
  29. parent::postSave($update);
  30. }
  31. $this->get('pathauto')->persist();
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function isEmpty() {
  37. // Make sure that the pathauto state flag does not get lost if just that is
  38. // changed.
  39. return !$this->alias && !$this->get('pathauto')->hasValue();
  40. }
  41. }