You are here

function views_bulk_operations_content_taxonomy_action in Views Bulk Operations (VBO) 6

File

actions/content_taxonomy.action.inc, line 13

Code

function views_bulk_operations_content_taxonomy_action(&$node, $context) {
  switch ($context['do']) {
    case TAXONOMY_ACTION_REPLACE:
      foreach ($context['fields'] as $field => $terms) {
        $node->{$field} = array();
      }

    // Wanted fallback on next case, replace: no terms + the new ones (no break; please)
    case TAXONOMY_ACTION_ADD:
      foreach ($context['fields'] as $field => $terms) {
        foreach ($terms as $value) {
          if (!is_array($node->{$field})) {
            $node->{$field} = array();
          }

          // If the term ID is already in the array, it does not make sense to re-add
          foreach ($node->{$field} as $already) {
            if ($already['value'] == $value['value']) {

              // Continues at next new term
              continue 2;
            }
          }
          $node->{$field}[] = $value;
        }
      }
      break;
    case TAXONOMY_ACTION_DELETE:
      foreach ($context['fields'] as $field => $terms) {
        if (!is_array($node->{$field})) {
          continue;
        }
        if (empty($node->{$field})) {
          continue;
        }
        foreach ($terms as $term) {
          foreach ($node->{$field} as $k => $already) {
            if ($already['value'] == $term['value']) {
              unset($node->{$field}[$k]);
            }
          }

          // Reset numerical IDs to make it continous
          $node->{$field} = array_values($node->{$field});
        }
      }
      break;
  }
}