diff --git a/core/modules/path/lib/Drupal/path/Plugin/Field/FieldType/PathItem.php b/core/modules/path/lib/Drupal/path/Plugin/Field/FieldType/PathItem.php index 601bc0b..0fc9272 100644 --- a/core/modules/path/lib/Drupal/path/Plugin/Field/FieldType/PathItem.php +++ b/core/modules/path/lib/Drupal/path/Plugin/Field/FieldType/PathItem.php @@ -32,7 +32,7 @@ class PathItem extends FieldItemBase { static $propertyDefinitions; /** - * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions(). + * {@inheritdoc} */ public function getPropertyDefinitions() { if (!isset(static::$propertyDefinitions)) { @@ -45,4 +45,57 @@ public function getPropertyDefinitions() { return static::$propertyDefinitions; } + /** + * {@inheritdoc} + */ + public function preSave() { + $this->alias = trim($this->alias); + } + + /** + * {@inheritdoc} + */ + public function insert() { + if ($this->alias) { + $entity = $this->getEntity(); + + // Ensure fields for programmatic executions. + $uri = $entity->uri(); + $langcode = $entity->language()->id; + + if ($path = \Drupal::service('path.crud')->save($uri['path'], $this->alias, $langcode)) { + $this->pid = $path['pid']; + } + } + } + + /** + * {@inheritdoc} + */ + public function update() { + // Delete old alias if user erased it. + if ($this->pid && !$this->alias) { + \Drupal::service('path.crud')->delete(array('pid' => $this->pid)); + } + // Only save a non-empty alias. + elseif ($this->alias) { + $entity = $this->getEntity(); + + // Ensure fields for programmatic executions. + $uri = $entity->uri(); + $langcode = $entity->language()->id; + + \Drupal::service('path.crud')->save($uri['path'], $this->alias, $langcode, $this->pid); + } + } + + /** + * {@inheritdoc} + */ + public function delete() { + // Delete all aliases associated with this entity. + $uri = $this->getEntity()->uri(); + \Drupal::service('path.crud')->delete(array('source' => $uri['path'])); + } + } diff --git a/core/modules/path/path.module b/core/modules/path/path.module index 8e96610..5c1049e 100644 --- a/core/modules/path/path.module +++ b/core/modules/path/path.module @@ -217,56 +217,6 @@ function path_entity_field_info($entity_type) { } /** - * Implements hook_entity_insert(). - * - * @todo: Move this to methods on the FieldItem class. - */ -function path_entity_insert(EntityInterface $entity) { - if ($entity instanceof ContentEntityInterface && $entity->hasField('path')) { - $entity->path->alias = trim($entity->path->alias); - // Only save a non-empty alias. - if (!empty($entity->path->alias)) { - // Ensure fields for programmatic executions. - $uri = $entity->uri(); - $langcode = $entity->language()->id; - \Drupal::service('path.crud')->save($uri['path'], $entity->path->alias, $langcode); - } - } -} - -/** - * Implements hook_entity_update(). - */ -function path_entity_update(EntityInterface $entity) { - if ($entity instanceof ContentEntityInterface && $entity->hasField('path')) { - $entity->path->alias = trim($entity->path->alias); - // Delete old alias if user erased it. - if ($entity->path->pid && !$entity->path->alias) { - \Drupal::service('path.crud')->delete(array('pid' => $entity->path->pid)); - } - // Only save a non-empty alias. - if ($entity->path->alias) { - $pid = $entity->path->pid; - // Ensure fields for programmatic executions. - $uri = $entity->uri(); - $langcode = $entity->language()->id; - \Drupal::service('path.crud')->save($uri['path'], $entity->path->alias, $langcode, $pid); - } - } -} - -/** - * Implements hook_entity_predelete(). - */ -function path_entity_predelete(EntityInterface $entity) { - if ($entity instanceof ContentEntityInterface && $entity->hasField('path')) { - // Delete all aliases associated with this term. - $uri = $entity->uri(); - \Drupal::service('path.crud')->delete(array('source' => $uri['path'])); - } -} - -/** * Implements hook_library_info(). */ function path_library_info() {