Skip to content
Snippets Groups Projects
Commit 40681d96 authored by catch's avatar catch
Browse files

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

parent 8b40554c
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -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().
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment