You are here

function taxonomy_deploy_check_taxonomy in Deploy - Content Staging 6

Manage the dependencies for an array of taxonomy terms.

Parameters

$taxonomy: Array of taxonomy terms, as is included with a node in node_load. You can also pass a single tid here, and the function will manually get the term and put it into an array of the appropriate format.

1 call to taxonomy_deploy_check_taxonomy()
taxonomy_deploy_node_deploy_check in modules/taxonomy_deploy/taxonomy_deploy.module
Implementation of hook_node_deploy_check().

File

modules/taxonomy_deploy/taxonomy_deploy.module, line 50
Deployment API which enables modules to deploy items between servers.

Code

function taxonomy_deploy_check_taxonomy($taxonomy) {

  // If the param is not an array, assume it is a tid. Get the
  // term and put it in an array such that the rest of the function
  // can work as normal.
  if (!is_array($taxonomy)) {
    $term = taxonomy_get_term($taxonomy);
    $taxonomy = array();
    $taxonomy[] = $term;
  }

  // Retrieve the remote server information.
  $url = variable_get('deploy_server_url', '');
  $pid = variable_get('deploy_pid', 0);

  // As we get vids from the remote server, cache them here for future
  // use to reduce roundtrips.
  //
  // @todo
  //   Make this static so that it will maintain through the recursive function calls?
  //   Also, should probably cache the terms as well since its quite possible to be
  //   checking a single term multiple times because of use in parents / related terms.
  $vocabularies = array();

  // Every item in a node's taxonomy array is a term array.
  foreach ($taxonomy as $term) {

    // Convert to array if its not already
    $term = (array) $term;

    // We keep a unique list of all the vids we encounter, so we can
    // handle them next. Terms must be handled before vocabularies in order
    // to make sure that vocabularies always get deployed before their terms.
    if (!in_array($term['vid'], $vocabularies)) {
      $vocabularies[] = $term['vid'];
    }

    // If this term is already in the deployment plan then either
    // a) it was added by the user and will get checked down the line or
    // b) it was added through dependency checks and its already been
    // dealt with. So we just move on in this case.
    if (!deploy_item_is_in_plan($pid, 'taxonomy_term', $term['tid'])) {

      // Does this term exist on the remote server?
      $remote_key = deploy_get_remote_key(deploy_uuid_get_term_uuid($term['tid']), 'term_data');

      // If not we're going to add it to the deployment plan, with a weight
      // of min(weight) - 1.
      if (!$remote_key) {

        // Note that when adding a taxonomy term or vocabulary to a plan, we save 'taxonomy_term'
        // or 'taxonomy_vocabulary' in the $module parameter, despite the fact that this is all
        // coming from the 'taxonomy' module. It just makes sense since they are both dealt with
        // completely differently, and it prevents me from doing some hackery like storing a
        // serialized array in the $data field.
        deploy_add_to_plan($pid, 'taxonomy_term', 'taxonomy term: ' . $term['name'], $term['tid'], deploy_get_min_weight($pid) - 1, DEPLOY_TAXONOMY_TERM_GROUP_WEIGHT);

        // Having added this term to the plan, we now need to dependency check it
        // term's parents. This will continue recursively until we get to a root term.
        taxonomy_deploy_check_taxonomy(taxonomy_get_parents($term['tid']));

        // And its related terms. Same deal.
        taxonomy_deploy_check_taxonomy(taxonomy_get_related($term['tid']));
      }
    }
  }

  // Mow we need to do the same thing for the vocabularies.
  foreach ($vocabularies as $vid) {
    if (!deploy_item_is_in_plan($pid, 'taxonomy_vocabulary', $vid)) {

      // Does this vocabulary exist on the remote server?
      $remote_key = deploy_get_remote_key(deploy_uuid_get_vocabulary_uuid($vid), 'vocabulary');

      // If not we're going to add it to the deployment plan, with a weight
      // of min(weight) - 1.
      if (!$remote_key) {

        // I hate doing this load here but i have to bite the bullet so that the
        // deployment listing makes sense.
        $vocabulary = taxonomy_vocabulary_load($vid);
        deploy_add_to_plan($pid, 'taxonomy_vocabulary', 'taxonomy vocabulary: ' . $vocabulary->name, $vid, deploy_get_min_weight($pid) - 1, DEPLOY_TAXONOMY_VOCABULARY_GROUP_WEIGHT);
      }
    }
  }
}