You are here

function _taxonomy_patterns_prepare_parent_terms in Patterns 7.2

Same name and namespace in other branches
  1. 7 patterns_components/components/taxonomy.inc \_taxonomy_patterns_prepare_parent_terms()

Checks if the provided array of potential parents is valid, and in case try to correct it.

Parameters

$parents mixed The potential array of parents.:

1 call to _taxonomy_patterns_prepare_parent_terms()
_taxonomy_patterns_prepare_term in patterns_components/components/taxonomy.inc

File

patterns_components/components/taxonomy.inc, line 440
Patterns component for taxonomy vocabularies and terms.

Code

function _taxonomy_patterns_prepare_parent_terms($parents) {
  if (!isset($parents)) {
    return FALSE;
  }
  if (!is_array($parents)) {
    $parents = array(
      $parents,
    );
  }
  $err = FALSE;
  $out = array();

  // Terms can have multiple parents
  foreach ($parents as $p) {
    if (!is_numeric($p)) {

      // Try to find out if a term with exists with the given name
      $ps = taxonomy_get_term_by_name($p);
      if (count($ps) !== 1) {
        $err = TRUE;
        continue;
      }
      $parent = array_pop($ps);
    }
    elseif ($p === 0) {
      $parent = new stdClass();
      $parent->tid = 0;
    }
    else {
      $parent = taxonomy_term_load($p);
      if (!$parent) {
        $err = TRUE;
        continue;
      }
    }
    $out[] = $parent->tid;
  }
  if ($err) {
    drupal_set_message(t('One or more error occurred while detecting parent terms.'), 'warning');
  }
  return $out;
}