You are here

function _hierarchical_select_apply_entity_settings in Hierarchical Select 7.3

Same name and namespace in other branches
  1. 5.3 hierarchical_select.module \_hierarchical_select_apply_entity_settings()
  2. 6.3 hierarchical_select.module \_hierarchical_select_apply_entity_settings()

Given a level, apply the entity_count and require_entity settings.

Parameters

$level: A level in the hierarchy.

$config: A config array with at least the following settings:

  • module
  • params
  • entity_count
  • require_entity

Return value

The updated level

1 call to _hierarchical_select_apply_entity_settings()
_hierarchical_select_hierarchy_generate in ./hierarchical_select.module
Generate the hierarchy object.

File

./hierarchical_select.module, line 1979
This module defines the "hierarchical_select" form element, which is a greatly enhanced way for letting the user select items in a hierarchy.

Code

function _hierarchical_select_apply_entity_settings($level, $config) {
  if (isset($config['special_items'])) {
    $special_items['exclusive'] = array_keys(array_filter($config['special_items'], '_hierarchical_select_special_item_exclusive'));
    $special_items['none'] = array_keys(array_filter($config['special_items'], '_hierarchical_select_special_item_none'));
  }

  // Only do something when the entity_count or the require_entity (or both)
  // settings are enabled.
  // NOTE: this uses the optional "hierarchical_select_entity_count" hook, so
  // we also check if it's implemented.
  if (isset($config['entity_count']['enabled']) && ($config['entity_count']['enabled'] || $config['entity_count']['require_entity']) && module_hook($config['module'], 'hierarchical_select_entity_count')) {
    foreach ($level as $item => $label) {

      // We don't want to alter internal or special items.
      if (!preg_match('/(none|label_\\d+|create_new_item)/', $item) && !in_array($item, $special_items['exclusive']) && !in_array($item, $special_items['none'])) {

        // Add our entity count settings to the parameters.
        $config['params'] += array(
          'entity_count' => array(
            'settings' => array(
              'count_children' => $config['entity_count']['settings']['count_children'],
              'entity_types' => $config['entity_count']['settings']['entity_types'],
            ),
          ),
        );
        $entity_count = module_invoke($config['module'], 'hierarchical_select_entity_count', $item, $config['params']);

        // When the require_entity setting is enabled and the entity count is
        // zero, then remove the item from the level.
        // When the item is not removed from the level due to the above and
        // the entity_count setting is enabled, update the label of the item
        // to include the entity count.
        if ($config['entity_count']['require_entity'] && $entity_count == 0) {
          unset($level[$item]);
        }
        elseif ($config['entity_count']['enabled']) {
          $level[$item] = "{$label} ({$entity_count})";
        }
      }
    }
  }
  return $level;
}