You are here

function taxonomy_patterns_validate in Patterns 7

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

File

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

Code

function taxonomy_patterns_validate($action, $tag, &$data) {
  $status = PATTERNS_SUCCESS;
  $msg = '';
  if ($tag == 'term') {
    if (!$data['vid'] && !empty($data['vocabulary'])) {
      $vocabs = taxonomy_get_vocabularies();
      foreach ($vocabs as $vid => $vocab) {
        if (drupal_strtolower($vocab->name) == drupal_strtolower($data['vocabulary'])) {
          $data['vid'] = $vid;
          break;
        }
      }
    }

    // we can't proceed without valid vocabulary ID
    if (empty($data['vid'])) {
      $status = PATTERNS_ERR;
      $msg = t("Vocabulary %vocab doesn't exist.", array(
        '%vocab' => $data['vocabulary'],
      ));
    }
  }
  else {
    if ($tag == 'vocabulary') {
      $keys = patterns_utils_key_exists(array(
        'name',
        'machine_name',
      ), $data);
      $msg = t("The following mandatory keys were missing for tag %tag: ", array(
        '%tag' => $tag,
      ));
      foreach ($keys as $key => $exist) {
        if (!$exist) {
          $status = PATTERNS_ERR;
          $msg .= $key . ', ';
        }
      }
      $msg = substr($msg, 0, -2);
    }
    else {
      $status = PATTERNS_ERR;
      $msg = t("Unknown tag: %tag.", array(
        '%tag' => $tag,
      ));
    }
  }
  return patterns_results($status, $msg);
}