Commit dbfe03c0 authored by larowlan's avatar larowlan

Issue #2916300 by amateescu: Use ComputedFieldItemListTrait for the path field type

parent e8b84d93
......@@ -5,12 +5,42 @@
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TypedData\ComputedItemListTrait;
/**
* Represents a configurable entity path field.
*/
class PathFieldItemList extends FieldItemList {
use ComputedItemListTrait;
/**
* {@inheritdoc}
*/
protected function computeValue() {
// Default the langcode to the current language if this is a new entity or
// there is no alias for an existent entity.
// @todo Set the langcode to not specified for untranslatable fields
// in https://www.drupal.org/node/2689459.
$value = ['langcode' => $this->getLangcode()];
$entity = $this->getEntity();
if (!$entity->isNew()) {
// @todo Support loading languge neutral aliases in
// https://www.drupal.org/node/2511968.
$alias = \Drupal::service('path.alias_storage')->load([
'source' => '/' . $entity->toUrl()->getInternalPath(),
'langcode' => $this->getLangcode(),
]);
if ($alias) {
$value = $alias;
}
}
$this->list[0] = $this->createItem(0, $value);
}
/**
* {@inheritdoc}
*/
......@@ -34,42 +64,4 @@ public function delete() {
\Drupal::service('path.alias_storage')->delete($conditions);
}
/**
* {@inheritdoc}
*/
public function getValue($include_computed = FALSE) {
$this->ensureLoaded();
return parent::getValue($include_computed);
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$this->ensureLoaded();
return parent::isEmpty();
}
/**
* {@inheritdoc}
*/
public function getIterator() {
$this->ensureLoaded();
return parent::getIterator();
}
/**
* Automatically create the first item for computed fields.
*
* This ensures that ::getValue() and ::isEmpty() calls will behave like a
* non-computed field.
*
* @todo: Move this to the base class in https://www.drupal.org/node/2392845.
*/
protected function ensureLoaded() {
if (!isset($this->list[0]) && $this->definition->isComputed()) {
$this->list[0] = $this->createItem(0);
}
}
}
......@@ -23,20 +23,6 @@
*/
class PathItem extends FieldItemBase {
/**
* Whether the alias has been loaded from the alias storage service yet.
*
* @var bool
*/
protected $isLoaded = FALSE;
/**
* Whether the alias is currently being set.
*
* @var bool
*/
protected $isLoading = FALSE;
/**
* {@inheritdoc}
*/
......@@ -50,14 +36,6 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
return $properties;
}
/**
* {@inheritdoc}
*/
public function __get($name) {
$this->ensureLoaded();
return parent::__get($name);
}
/**
* {@inheritdoc}
*/
......@@ -65,76 +43,20 @@ public static function schema(FieldStorageDefinitionInterface $field_definition)
return [];
}
/**
* {@inheritdoc}
*/
public function getValue() {
$this->ensureLoaded();
return parent::getValue();
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$this->ensureLoaded();
return parent::isEmpty();
}
/**
* {@inheritdoc}
*/
public function getIterator() {
$this->ensureLoaded();
return parent::getIterator();
return ($this->alias === NULL || $this->alias === '') && ($this->pid === NULL || $this->pid === '') && ($this->langcode === NULL || $this->langcode === '');
}
/**
* {@inheritdoc}
*/
public function preSave() {
$this->alias = trim($this->alias);
}
/**
* {@inheritdoc}
*/
public function __set($name, $value) {
// Also ensure that existing values are loaded when setting a value, this
// ensures that it is possible to set a new value immediately after loading
// an entity.
$this->ensureLoaded();
parent::__set($name, $value);
}
/**
* {@inheritdoc}
*/
public function set($property_name, $value, $notify = TRUE) {
// Also ensure that existing values are loaded when setting a value, this
// ensures that it is possible to set a new value immediately after loading
// an entity.
$this->ensureLoaded();
return parent::set($property_name, $value, $notify);
}
/**
* {@inheritdoc}
*/
public function get($property_name) {
$this->ensureLoaded();
return parent::get($property_name);
}
/**
* {@inheritdoc}
*/
public function setValue($values, $notify = TRUE) {
// Also ensure that existing values are loaded when setting a value, this
// ensures that it is possible to set a new value immediately after loading
// an entity.
$this->ensureLoaded();
return parent::setValue($values, $notify);
if ($this->alias !== NULL) {
$this->alias = trim($this->alias);
}
}
/**
......@@ -178,42 +100,4 @@ public static function mainPropertyName() {
return 'alias';
}
/**
* Ensures the alias properties are loaded if available.
*
* This ensures that the properties will always be loaded and act like
* non-computed fields when calling ::__get() and getValue().
*
* @todo: Determine if this should be moved to the base class in
* https://www.drupal.org/node/2392845.
*/
protected function ensureLoaded() {
// Make sure to avoid a infinite loop if setValue() has be called from this
// block which calls ensureLoaded().
if (!$this->isLoaded && !$this->isLoading) {
$entity = $this->getEntity();
if (!$entity->isNew()) {
// @todo Support loading languge neutral aliases in
// https://www.drupal.org/node/2511968.
$alias = \Drupal::service('path.alias_storage')->load([
'source' => '/' . $entity->toUrl()->getInternalPath(),
'langcode' => $this->getLangcode(),
]);
if ($alias) {
$this->isLoading = TRUE;
$this->setValue($alias);
$this->isLoading = FALSE;
}
else {
// If there is no existing alias, default the langcode to the current
// language.
// @todo Set the langcode to not specified for untranslatable fields
// in https://www.drupal.org/node/2689459.
$this->langcode = $this->getLangcode();
}
}
$this->isLoaded = TRUE;
}
}
}
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