Commit 40681d96 authored by catch's avatar catch

Issue #1980822 by Berdir, sun, vladan.me, twistor: Support any entity with path.module.

parent 8b40554c
......@@ -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()));
}
}
......@@ -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().
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment