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

8.0.x
Nathaniel Catchpole 2014-02-19 12:33:22 +00:00
parent 8b40554c5a
commit 40681d9690
2 changed files with 52 additions and 48 deletions

View File

@ -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 @@ class PathItem extends FieldItemBase {
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()));
}
}

View File

@ -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().
*/