ChildEntityWarning.php in Entity Reference Hierarchy 8.2
File
src/Information/ChildEntityWarning.php
View source
<?php
namespace Drupal\entity_hierarchy\Information;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use PNX\NestedSet\Node;
class ChildEntityWarning {
protected $relatedEntities;
protected $cache;
protected $parent;
public function __construct(\SplObjectStorage $relatedEntities, RefinableCacheableDependencyInterface $cache, Node $parent = NULL) {
$this->relatedEntities = $relatedEntities;
$this->cache = $cache;
$this->parent = $parent;
}
public function getList() {
$child_labels = [];
$build = [
'#theme' => 'item_list',
];
foreach ($this->relatedEntities as $node) {
if (!$this->relatedEntities
->contains($node) || $node == $this->parent) {
continue;
}
$child_labels[] = $this->relatedEntities
->offsetGet($node)
->label();
}
$build['#items'] = array_unique($child_labels);
$this->cache
->applyTo($build);
return $build;
}
public function getWarning() {
if ($this->parent) {
return new PluralTranslatableMarkup($this->relatedEntities
->count() - 1, 'This Test entity has 1 child, deleting this item will change its parent to be @parent.', 'This Test entity has @count children, deleting this item will change their parent to be @parent.', [
'@parent' => $this->relatedEntities
->offsetGet($this->parent)
->label(),
]);
}
return new PluralTranslatableMarkup($this->relatedEntities
->count(), 'This Test entity has 1 child, deleting this item will move that item to the root of the hierarchy.', 'This Test entity has @count children, deleting this item will move those items to the root of the hierarchy.');
}
}