You are here

function og_vocab_remove_referenced_items_queue_worker in OG Vocabulary 7

Queue worker; Process a queue item.

When a OG vocabulary is being deleted we need to remove the reference between the nodes to the terms.

1 call to og_vocab_remove_referenced_items_queue_worker()
OgVocabUnbindFromContentType::testUnbindContentType in ./og_vocab.test
When a vocabulary is related to a bundle and it's term are reference to the group content, un-bind it from the bundle the terms should not be reference any more to the node.
1 string reference to 'og_vocab_remove_referenced_items_queue_worker'
og_vocab_cron_queue_info in ./og_vocab.module
Implements hook_cron_queue_info().

File

./og_vocab.module, line 1537
Give each group its own system controlled vocabularies.

Code

function og_vocab_remove_referenced_items_queue_worker($data, $end_time = FALSE) {
  extract($data);

  // Get range of the group content.
  // We don't use entityFieldQuery() as we want to join to the entity base
  // table, to make sure we process only entities from the correct bundle.
  $entity_info = entity_get_info($entity_type);
  $query = db_select('og_membership', 'ogm');
  $query
    ->fields('ogm', array(
    'id',
    'entity_type',
    'etid',
  ));
  $query
    ->condition('ogm.group_type', $group_type)
    ->condition('ogm.gid', $gid)
    ->condition('ogm.entity_type', $entity_type)
    ->condition('ogm.id', $last_id, '>')
    ->orderBy('ogm.id');
  $base_table = $entity_info['base table'];
  $bundle_key = $entity_info['entity keys']['bundle'];
  $id_key = $entity_info['entity keys']['id'];
  $alias = $query
    ->innerJoin($base_table, NULL, "ogm.etid = {$base_table}.{$id_key}");
  $result = $query
    ->condition("{$alias}.{$bundle_key}", $bundle)
    ->range(0, 100)
    ->execute()
    ->fetchAllAssoc('id');
  if (empty($result)) {

    // No more group-content for this entity, so we can delete the task item.
    return;
  }
  foreach ($result as $row) {
    $last_id = $row->id;
    $wrapper = entity_metadata_wrapper($row->entity_type, $row->etid);
    if (!($terms = $wrapper->{$field_name}
      ->value())) {

      // Field doesn't have any term, so we can skip.
      continue;
    }

    // Array with the original and new term IDs.
    $original_tids = array();
    $new_tids = array();
    foreach ($terms as $term) {
      $original_tids[] = $term->tid;
      if ($term->vid != $vid) {
        $new_tids[] = $term->tid;
      }
    }
    if ($original_tids != $new_tids) {

      // Some terms were removed so re-save the entity.
      $wrapper->{$field_name}
        ->set($new_tids);
      $wrapper
        ->save();
    }
  }

  // Update the item with the last OG-membership ID.
  $data = array(
    'vid' => $vid,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'field_name' => $field_name,
    'group_type' => $group_type,
    'gid' => $gid,
    'last_id' => $last_id,
  );
  $queue = DrupalQueue::get('og_vocab');
  return $queue
    ->createItem($data);
}