You are here

class ChildEntityWarning in Entity Reference Hierarchy 3.x

Same name and namespace in other branches
  1. 8.2 src/Information/ChildEntityWarning.php \Drupal\entity_hierarchy\Information\ChildEntityWarning

Defines a value object for a child entity warning.

Hierarchy

Expanded class hierarchy of ChildEntityWarning

See also

entity_hierarchy_form_alter()

File

src/Information/ChildEntityWarning.php, line 14

Namespace

Drupal\entity_hierarchy\Information
View 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

Namesort descending Modifiers Type Description Overrides
ChildEntityWarning::$cache protected property Cache metadata.
ChildEntityWarning::$parent protected property Node if parent exists.
ChildEntityWarning::$relatedEntities protected property Related entities.
ChildEntityWarning::getList public function Gets render array for child entity list.
ChildEntityWarning::getWarning public function Gets warning message for deleting a parent.
ChildEntityWarning::__construct public function Constructs a new ChildEntityWarning object.