You are here

private function HierarchyManager::hierarchyGetParentSelector in Entity Reference Hierarchy 8

Build a list of parent tiles to be displayed as part of a dropdown selector in hierarchyNodeParentFormItems. First we grab a list of allowed parents using the hierarchyParentOptions method. Then we format each title by iteratively calling hierarchyParentOptionTitle. Finally we build the actual form element to be supplied to hierarchyNodeParentFormItems.

Parameters

string $child_type: The child node type.

$parent: The parent item being set on the form.

int/null $exclude: Node ID of the parent that should be excluded from the list.

Return value

array The form array to be consumed by hierarchyNodeParentFormItems

See also

hierarchyNodeParentFormItems

hierarchyParentOptions

hierarchyParentOptionTitle

2 calls to HierarchyManager::hierarchyGetParentSelector()
HierarchyManager::hierarchyGetNodeTypeSettingsForm in src/HierarchyManager.php
Create the hierarchy type settings form, and load any default values using the configuration management settings.
HierarchyManager::hierarchyNodeParentFormItems in src/HierarchyManager.php
Build the parent form item for a given parent node object. This function is called iteratively in addHierarchyFormElement method to build a dropdown list of parents. We add a visible title (node title), the hierarchy id (hid), a parent node id (pid),…

File

src/HierarchyManager.php, line 353
Contains \Drupal\entity_hierarchy\HierarchyManager.

Class

HierarchyManager
Defines a hierarchy manager.

Namespace

Drupal\entity_hierarchy

Code

private function hierarchyGetParentSelector($child_type, $parent, $exclude = NULL) {

  // Allow other modules to create the pulldown first.
  // Modules implementing this hook, should return the form element inside an array with a numeric index.
  // This prevents module_invoke_all from merging the outputs to make an invalid form array.
  $out = \Drupal::moduleHandler()
    ->invokeAll('entity_hierarchy_get_parent_selector', array(
    $child_type,
    $parent,
    $exclude,
  ));
  if ($out) {

    // Return the last element defined (any others are thrown away);
    return end($out);
  }
  $default_value = $parent;

  // If no other modules defined the pulldown, then define it here.
  $options = array(
    0 => '-- ' . t('NONE') . ' --',
  );
  $items = $this
    ->hierarchyParentOptions($child_type, $exclude);
  foreach ($items as $key => $item) {
    if (is_object($item)) {
      $options[$key] = $this
        ->hierarchyParentOptionTitle($item);
    }
  }

  // Make sure the current value is enabled so items can be re-saved.
  if ($default_value && isset($items[$default_value])) {
    $items[$default_value]->disabled = 0;
  }
  $out = array(
    '#type' => 'select',
    '#title' => t('Parent Node'),
    '#default_value' => $default_value,
    '#attributes' => array(
      'class' => array(
        'entity_hierarchy-parent-selector',
      ),
    ),
    '#options' => $options,
    '#items' => $items,
  );
  return $out;
}