You are here

class ChildEntityWarningBuilder in Entity Reference Hierarchy 8.2

Same name and namespace in other branches
  1. 3.x src/Information/ChildEntityWarningBuilder.php \Drupal\entity_hierarchy\Information\ChildEntityWarningBuilder

Defines a class for building a list of child entity warnings.

Hierarchy

Expanded class hierarchy of ChildEntityWarningBuilder

1 file declares its use of ChildEntityWarningBuilder
entity_hierarchy.module in ./entity_hierarchy.module
A module to make entities hierarchical.

File

src/Information/ChildEntityWarningBuilder.php, line 16

Namespace

Drupal\entity_hierarchy\Information
View source
class ChildEntityWarningBuilder implements ContainerInjectionInterface {

  /**
   * Parent candidate info.
   *
   * @var \Drupal\entity_hierarchy\Information\ParentCandidateInterface
   */
  protected $parentCandidate;

  /**
   * Tree node mapper.
   *
   * @var \Drupal\entity_hierarchy\Storage\EntityTreeNodeMapperInterface
   */
  protected $treeNodeMapper;

  /**
   * Storage factory.
   *
   * @var \Drupal\entity_hierarchy\Storage\NestedSetStorageFactory
   */
  protected $nestedSetStorageFactory;

  /**
   * Node key factory.
   *
   * @var \Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory
   */
  protected $nodeKeyFactory;

  /**
   * Constructs a new ChildEntityWarningBuilder object.
   *
   * @param \Drupal\entity_hierarchy\Information\ParentCandidateInterface $parentCandidate
   *   Parent candidate service.
   * @param \Drupal\entity_hierarchy\Storage\EntityTreeNodeMapperInterface $treeNodeMapper
   *   Tree node mapper.
   * @param \Drupal\entity_hierarchy\Storage\NestedSetStorageFactory $nestedSetStorageFactory
   *   Storage factory.
   * @param \Drupal\entity_hierarchy\Storage\NestedSetNodeKeyFactory $nodeKeyFactory
   *   Key factory.
   */
  public function __construct(ParentCandidateInterface $parentCandidate, EntityTreeNodeMapperInterface $treeNodeMapper, NestedSetStorageFactory $nestedSetStorageFactory, NestedSetNodeKeyFactory $nodeKeyFactory) {
    $this->parentCandidate = $parentCandidate;
    $this->treeNodeMapper = $treeNodeMapper;
    $this->nestedSetStorageFactory = $nestedSetStorageFactory;
    $this->nodeKeyFactory = $nodeKeyFactory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_hierarchy.information.parent_candidate'), $container
      ->get('entity_hierarchy.entity_tree_node_mapper'), $container
      ->get('entity_hierarchy.nested_set_storage_factory'), $container
      ->get('entity_hierarchy.nested_set_node_factory'));
  }

  /**
   * Gets warning about child entities before deleting a parent.
   *
   * @param \Drupal\Core\Entity\ContentEntityInterface $parent
   *   Parent to be deleted.
   *
   * @return \Drupal\entity_hierarchy\Information\ChildEntityWarning[]
   *   Array of warning value objects.
   */
  public function buildChildEntityWarnings(ContentEntityInterface $parent) {
    $return = [];
    if ($fields = $this->parentCandidate
      ->getCandidateFields($parent)) {
      $cache = new CacheableMetadata();
      foreach ($fields as $field_name) {

        /** @var \PNX\NestedSet\NestedSetInterface $storage */
        $storage = $this->nestedSetStorageFactory
          ->get($field_name, $parent
          ->getEntityTypeId());
        $nodeKey = $this->nodeKeyFactory
          ->fromEntity($parent);
        $children = $storage
          ->findChildren($nodeKey);
        if ($parent_node = $storage
          ->findParent($nodeKey)) {
          $children[] = $parent_node;
        }
        $entities = $this->treeNodeMapper
          ->loadAndAccessCheckEntitysForTreeNodes($parent
          ->getEntityTypeId(), $children, $cache);
        $return[] = new ChildEntityWarning($entities, $cache, $parent_node);
      }
    }
    return $return;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ChildEntityWarningBuilder::$nestedSetStorageFactory protected property Storage factory.
ChildEntityWarningBuilder::$nodeKeyFactory protected property Node key factory.
ChildEntityWarningBuilder::$parentCandidate protected property Parent candidate info.
ChildEntityWarningBuilder::$treeNodeMapper protected property Tree node mapper.
ChildEntityWarningBuilder::buildChildEntityWarnings public function Gets warning about child entities before deleting a parent.
ChildEntityWarningBuilder::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ChildEntityWarningBuilder::__construct public function Constructs a new ChildEntityWarningBuilder object.