function entity_hierarchy_node_prepare_form in Entity Reference Hierarchy 8
Implements We are loading the hierarchy parents for a given node id, and adding it to the node object for later processing in HierarchyManagerInterface::addHierarchyFormElement, which is called in entity_hierarchy_form_node_form_alter().
See also
hook_node_prepare_form().
entity_hierarchy_form_node_form_alter
HierarchyManagerInterface::addHierarchyFormElement
HierarchyManagerInterface::hierarchyDefaultRecord
File
- ./
entity_hierarchy.module, line 213 - A module to make nodes hierarchical.
Code
function entity_hierarchy_node_prepare_form(NodeInterface $node) {
/** @var \Drupal\entity_hierarchy\HierarchyManager $hierarchy_manager */
$hierarchy_manager = \Drupal::service('entity_hierarchy.manager');
/** @var \Drupal\entity_hierarchy\HierarchyOutlineStorage $hierarchy_storage */
$hierarchy_storage = \Drupal::service('entity_hierarchy.outline_storage');
// Load the parents if that hasn't been done before.
$nid = $node
->id();
if (!isset($node->entity_hierarchy_parents) && !empty($nid)) {
$node->entity_hierarchy_parents = $hierarchy_storage
->hierarchyGetParents($node
->id());
}
// Cannot use module_invoke_all because it doesn't support references.
foreach (\Drupal::moduleHandler()
->getImplementations('entity_hierarchy_default_parents') as $module) {
$function = $module . '_entity_hierarchy_default_parents';
$function($node);
}
if ($hierarchy_manager
->hierarchyCanBeChild($node) || $hierarchy_manager
->hierarchyCanBeParent($node)) {
if (!isset($node->entity_hierarchy_parents) || empty($node->entity_hierarchy_parents)) {
// Create a default entity_hierarchy object.
$nid = empty($nid) ? null : $node
->id();
$parent = $hierarchy_manager
->hierarchyDefaultRecord($nid, 0);
// Set the type default if there is one.
if (empty($nid)) {
$config = \Drupal::config('entity_hierarchy.settings');
$default = $config
->get('nh_defaultparent_' . $node
->getType());
// Get the parent node id from passed in from the get params.
$pnid = !empty($_GET['parent']) ? (int) $_GET['parent'] : $default;
// Get the parent from the get string. User must have update perms for parent unless it is the default.
$account = \Drupal::currentUser();
if ($pnid && ($parent_node = Node::load($pnid))) {
if ($hierarchy_manager
->hierarchyCanBeParent($node) && ($account
->hasPermission('create child of any parent') || $node
->access("update") || $parent_node
->id() == $default)) {
$parent->pnid = $pnid;
}
}
}
$node->entity_hierarchy_parents[] = $parent;
}
}
}