From 40681d9690d7ea7fc012fd52b71aa2688c3b4820 Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Wed, 19 Feb 2014 12:33:22 +0000
Subject: [PATCH] Issue #1980822 by Berdir, sun, vladan.me, twistor: Support
 any entity with path.module.

---
 .../path/Plugin/Field/FieldType/PathItem.php  | 53 ++++++++++++++++++-
 core/modules/path/path.module                 | 47 ----------------
 2 files changed, 52 insertions(+), 48 deletions(-)

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 6d75cac158de..54411a6bd2e8 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
@@ -33,7 +33,7 @@ class PathItem extends FieldItemBase {
   static $propertyDefinitions;
 
   /**
-   * Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
+   * {@inheritdoc}
    */
   public function getPropertyDefinitions() {
     if (!isset(static::$propertyDefinitions)) {
@@ -53,4 +53,55 @@ public static function schema(FieldDefinitionInterface $field_definition) {
     return array();
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function preSave() {
+    $this->alias = trim($this->alias);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function insert() {
+    if ($this->alias) {
+      $entity = $this->getEntity();
+
+      // Ensure fields for programmatic executions.
+      $langcode = $entity->language()->id;
+
+      if ($path = \Drupal::service('path.crud')->save($entity->getSystemPath(), $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.
+      $langcode = $entity->language()->id;
+
+      \Drupal::service('path.crud')->save($entity->getSystemPath(), $this->alias, $langcode, $this->pid);
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function delete() {
+    // Delete all aliases associated with this entity.
+    $entity = $this->getEntity();
+    \Drupal::service('path.crud')->delete(array('source' => $entity->getSystemPath()));
+  }
+
 }
diff --git a/core/modules/path/path.module b/core/modules/path/path.module
index bd8969fe41bb..6c823ea25175 100644
--- a/core/modules/path/path.module
+++ b/core/modules/path/path.module
@@ -231,53 +231,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.
-      $langcode = $entity->language()->id;
-      \Drupal::service('path.crud')->save($entity->getSystemPath(), $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.
-      $langcode = $entity->language()->id;
-      \Drupal::service('path.crud')->save($entity->getSystemPath(), $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.
-    \Drupal::service('path.crud')->delete(array('source' => $entity->getSystemPath()));
-  }
-}
-
 /**
  * Implements hook_library_info().
  */
-- 
GitLab