class ChildEntityWarning in Entity Reference Hierarchy 3.x
Same name and namespace in other branches
- 8.2 src/Information/ChildEntityWarning.php \Drupal\entity_hierarchy\Information\ChildEntityWarning
Defines a value object for a child entity warning.
Hierarchy
- class \Drupal\entity_hierarchy\Information\ChildEntityWarning
Expanded class hierarchy of ChildEntityWarning
See also
File
- src/
Information/ ChildEntityWarning.php, line 14
Namespace
Drupal\entity_hierarchy\InformationView source
class ChildEntityWarning {
/**
* Related entities.
*
* @var \SplObjectStorage
*/
protected $relatedEntities;
/**
* Cache metadata.
*
* @var \Drupal\Core\Cache\RefinableCacheableDependencyInterface
*/
protected $cache;
/**
* Node if parent exists.
*
* @var null|\PNX\NestedSet\Node
*/
protected $parent;
/**
* Constructs a new ChildEntityWarning object.
*
* @param \SplObjectStorage $relatedEntities
* Related entities (children or parents).
* @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $cache
* Cache metadata.
* @param \PNX\NestedSet\Node|null $parent
* (optional) Parent if exists.
*/
public function __construct(\SplObjectStorage $relatedEntities, RefinableCacheableDependencyInterface $cache, Node $parent = NULL) {
$this->relatedEntities = $relatedEntities;
$this->cache = $cache;
$this->parent = $parent;
}
/**
* Gets render array for child entity list.
*
* @return array
* Render array.
*/
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;
}
/**
* Gets warning message for deleting a parent.
*
* @return \Drupal\Core\StringTranslation\PluralTranslatableMarkup
* Warning message.
*/
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.');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ChildEntityWarning:: |
protected | property | Cache metadata. | |
ChildEntityWarning:: |
protected | property | Node if parent exists. | |
ChildEntityWarning:: |
protected | property | Related entities. | |
ChildEntityWarning:: |
public | function | Gets render array for child entity list. | |
ChildEntityWarning:: |
public | function | Gets warning message for deleting a parent. | |
ChildEntityWarning:: |
public | function | Constructs a new ChildEntityWarning object. |