You are here

function synonyms_select_taxonomy_term_sort_name_options_recursive in Synonyms 7

Supportive function to build taxonomy term options array sorted by name.

The function starts from the 0-depth level and starts to recursively build the options and to sort the labels on each level, then it merges the bottom to up all the levels maintaining correct order within the final options array.

Parameters

object $vocabulary: Within which vocabulary to execute the function. Supply here the fully loaded taxonomy vocabulary object

int $parent: Only children of this term will be included in the output. You can supply 0 which means to include all the terms from the vocabulary

int $depth: Used for internal purposes. Clients of this function should supply here 0, unless they know what they are doing. It is used internally to keep track of the nesting level

Return value

array Array of options that can be inserted directly into 'select' form element. The options will be sorted by name (term or synonym), respecting the hierarchy restrictions

2 calls to synonyms_select_taxonomy_term_sort_name_options_recursive()
synonyms_field_widget_form in ./synonyms.module
Implements hook_field_widget_form().
synonyms_views_handler_filter_term_tid::value_form in views/synonyms_views_handler_filter_term_tid.inc
Options form subform for setting options.

File

./synonyms.module, line 1381
Provide synonyms feature for Drupal entities.

Code

function synonyms_select_taxonomy_term_sort_name_options_recursive($vocabulary, $parent = 0, $depth = 0) {

  // We statically cache behavior implementations in order to not DDOS the data
  // base.
  $behavior_implementations =& drupal_static(__FUNCTION__, array());
  $bundle = field_extract_bundle('taxonomy_term', $vocabulary);
  if (!isset($behavior_implementations[$bundle])) {
    $behavior_implementations[$bundle] = synonyms_behavior_get('select', 'taxonomy_term', $bundle, TRUE);
  }
  $options = array();
  if ($terms = taxonomy_get_tree($vocabulary->vid, $parent, 1, TRUE)) {
    $options = array();
    foreach ($terms as $term) {
      $term->depth = $depth;
      $options[] = synonyms_select_option_entity($term, 'taxonomy_term', NULL, NULL, array(
        'depth',
      ));
      foreach ($behavior_implementations[$bundle] as $implementation) {
        foreach ($implementation['object']
          ->extractSynonyms($term) as $synonym) {
          $options[] = synonyms_select_option_entity($term, 'taxonomy_term', $synonym, $implementation, array(
            'depth',
          ));
        }
      }
    }
    usort($options, 'synonyms_select_sort_name');

    // Now recursively go one level nested into each of the terms that we have
    // on this level.
    $options_copy = $options;
    $i = 0;
    foreach ($options_copy as $v) {
      $i++;
      $tid = array_keys($v->option);
      $tid = $tid[0];
      if (is_numeric($tid)) {
        $nested_options = synonyms_select_taxonomy_term_sort_name_options_recursive($vocabulary, $tid, $depth + 1);
        $options = array_merge(array_slice($options, 0, $i), $nested_options, array_slice($options, $i));
      }
    }
  }
  return $options;
}