You are here

function synonyms_term_synonyms_context in Synonyms 7

Discover if this argument gives us the term we crave.

1 string reference to 'synonyms_term_synonyms_context'
term_synonyms.inc in plugins/arguments/term_synonyms.inc

File

plugins/arguments/term_synonyms.inc, line 24

Code

function synonyms_term_synonyms_context($arg = NULL, $conf = NULL, $empty = FALSE) {

  // If unset it wants a generic, unfilled context.
  if ($empty) {
    return ctools_context_create_empty('entity:taxonomy_term');
  }
  $conf['vids'] = is_array($conf['vids']) ? array_filter($conf['vids']) : array();
  if (is_object($arg)) {
    $term = $arg;
    if (!empty($conf['vids']) && empty($conf['vids'][$term->vid])) {
      return NULL;
    }
  }
  else {
    if ($conf['transform']) {
      $tids = db_select('taxonomy_term_data', 't')
        ->fields('t', array(
        'tid',
      ))
        ->where("REPLACE(t.name, ' ', '-') = :argument", array(
        ':argument' => $arg,
      ));
      if (!empty($conf['vids'])) {
        $tids
          ->condition('t.vid', $conf['vids']);
      }
      $tids = $tids
        ->execute()
        ->fetchCol();
      $terms = taxonomy_term_load_multiple($tids);
    }
    else {
      $terms = taxonomy_get_term_by_name($arg);
    }
    if (!empty($conf['vids'])) {
      foreach ($terms as $k => $term) {
        if (!isset($conf['vids'][$term->vid])) {
          unset($terms[$k]);
        }
      }
    }
    if (empty($terms)) {

      // We couldn't find the term by name, so we will look it up now by
      // synonyms.
      $vocabularies = taxonomy_vocabulary_load_multiple(empty($conf['vids']) ? FALSE : $conf['vids']);
      foreach ($vocabularies as $vocabulary) {
        $condition = db_and();
        if ($conf['transform']) {
          $condition
            ->where("REPLACE(" . AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER . ", ' ', '-') = :argument", array(
            ':argument' => $arg,
          ));
        }
        else {
          $condition
            ->condition(AbstractSynonymsBehavior::COLUMN_SYNONYM_PLACEHOLDER, $arg);
        }
        $rows = synonyms_synonyms_find($condition, 'taxonomy_term', $vocabulary->machine_name);
        if (!empty($rows)) {

          // We have found a match, no need to search further.
          $terms[] = taxonomy_term_load($rows[0]->entity_id);
          break;
        }
      }
    }
    if (empty($terms)) {
      return NULL;
    }
    $term = array_shift($terms);
  }
  $context = ctools_context_create('entity:taxonomy_term', $term);
  $context->original_argument = $arg;
  return $context;
}