You are here

function _hierarchical_select_dropbox_generate in Hierarchical Select 7.3

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

Generate the dropbox object.

Parameters

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

  • module
  • save_lineage
  • params
  • dropbox
    • title

$selection: The selection based on which a dropbox should be generated.

Return value

A dropbox object.

5 calls to _hierarchical_select_dropbox_generate()
form_hierarchical_select_process in ./hierarchical_select.module
Hierarchical select form element type #process callback.
HierarchicalSelectInternals::generate in tests/internals.test
Generate the $hierarchy and $dropbox objects.
hs_taxonomy_field_formatter_prepare_view in modules/hs_taxonomy.module
Implements hook_field_formatter_prepare_view().
theme_hierarchical_select_selection_as_lineages in includes/theme.inc
Themeing function to render a selection (of items) according to a given Hierarchical Select configuration as one or more lineages.
_hs_taxonomy_token_termpath_for_vid in modules/hs_taxonomy.module
Helper function for hs_taxonomy_token_values().

File

./hierarchical_select.module, line 2150
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_dropbox_generate($config, $selection) {
  $dropbox = new stdClass();
  $start = microtime(TRUE);
  $dropbox->title = !empty($config['dropbox']['title']) ? filter_xss_admin($config['dropbox']['title']) : t('All selections');
  $dropbox->lineages = array();
  $dropbox->lineages_selections = array();

  // Clean selection.
  foreach ($selection as $key => $item) {
    if (!module_invoke($config['module'], 'hierarchical_select_valid_item', $item, $config['params'])) {
      unset($selection[$key]);
    }
  }
  if (!empty($selection)) {

    // Store the "save lineage" setting, needed in the rendering layer.
    $dropbox->save_lineage = $config['save_lineage'];
    if ($config['save_lineage']) {
      $dropbox->lineages = _hierarchical_select_dropbox_reconstruct_lineages_save_lineage_enabled($config['module'], $selection, $config['params']);
    }
    else {

      // Retrieve the lineage of each item.
      foreach ($selection as $item) {
        $dropbox->lineages[] = module_invoke($config['module'], 'hierarchical_select_lineage', $item, $config['params']);
      }

      // We will also need the labels of each item in the rendering layer.
      foreach ($dropbox->lineages as $id => $lineage) {
        foreach ($lineage as $level => $item) {
          $dropbox->lineages[$id][$level] = array(
            'value' => $item,
            'label' => module_invoke($config['module'], 'hierarchical_select_item_get_label', $item, $config['params']),
          );
        }
      }
    }

    // Sanitize the labels.
    foreach ($dropbox->lineages as $id => $lineage) {
      foreach ($lineage as $level => $item) {
        $dropbox->lineages[$id][$level]['label'] = check_plain($dropbox->lineages[$id][$level]['label']);
      }
    }
    if (!isset($config['dropbox']['sort']) || $config['dropbox']['sort']) {
      usort($dropbox->lineages, '_hierarchical_select_dropbox_sort');
    }

    // Now store each lineage's selection too. This is needed on the client side
    // to enable the remove button to let the server know which selected items
    // should be removed.
    foreach ($dropbox->lineages as $id => $lineage) {
      if ($config['save_lineage']) {

        // Store the entire lineage.
        $dropbox->lineages_selections[$id] = array_map('_hierarchical_select_dropbox_lineage_item_get_value', $lineage);
      }
      else {

        // Store only the last (aka the deepest) value of the lineage.
        $dropbox->lineages_selections[$id][0] = $lineage[count($lineage) - 1]['value'];
      }
    }
  }

  // Calculate the time it took to build the dropbox object.
  $dropbox->build_time = (microtime(TRUE) - $start) * 1000;
  return $dropbox;
}