Hi,

I am not sure how to submit a patch so I just paste the code in here. The functionality is simple introduced into current code as it does not chage anything, just paste it into .module file:

/**
 * Implements hook_node_operations().
 */
function metatag_node_operations() {
  $operations['metatag_update_node_metatags'] = array(
    'label' => t('Update node meta tags'),
    'callback' => 'metatag_node_update_metatags_multiple',
    'callback arguments' => array(),
  );
  return $operations;
}

/**
 * Update the meta tags for multiple nodes.
 * @param array $nids
 *   An array of node IDs.
 */
function metatag_node_update_metatags_multiple(array $nids) {

  $nodes = node_load_multiple($nids);

  // We use batch processing to prevent timeout when updating a large number
  // of nodes.
  if (count($nodes) > 10) {
    $batch = array(
      'operations' => array(
        array('_metatag_node_update_metatags_process', array($nodes))
      ),
      'finished' => '_metatag_node_update_metatags_finished',
      'title' => t('Processing'),
      'error_message' => t('The update has encountered an error.'),
    );
    batch_set($batch);
  }
  else {
    foreach ($nodes as $node) {
      metatag_entity_update($node, 'node');
    }
    drupal_set_message(t('The update has been performed.'));
  }
}

/**
 * Node mass update of meta tags.
 */
function _metatag_node_update_metatags_process($nodes, $updates, &$context) {
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($nodes);
    $context['sandbox']['nodes'] = $nodes;
  }

  // Process nodes by groups of 5.
  $count = min(5, count($context['sandbox']['nodes']));
  for ($i = 1; $i <= $count; $i++) {
    // For each nid, load the node, reset the values, and save it.
    $node = array_shift($context['sandbox']['nodes']);
    metatag_entity_update($node, 'node');

    // Store result for post-processing in the finished callback.
    $context['results'][] = l($node->title, 'node/' . $node->nid);

    // Update our progress information.
    $context['sandbox']['progress']++;
  }

  // Inform the batch engine that we are not finished,
  // and provide an estimation of the completion level we reached.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}

/**
 * Node mass update of meta tags 'finished' callback.
 */
function _metatag_node_update_metatags_finished($success, $results, $operations) {
  if ($success) {
    drupal_set_message(t('The update has been performed.'));
  }
  else {
    drupal_set_message(t('An error occurred and processing did not complete.'), 'error');
    $message = format_plural(count($results), '1 item successfully processed:', '@count items successfully processed:');
    $message .= theme('item_list', array('items' => $results));
    drupal_set_message($message);
  }
}

Comments

dave reid’s picture

Version: 7.x-1.0-alpha3 » 7.x-1.x-dev
Category: task » feature
Status: Needs review » Postponed (maintainer needs more info)

I'm not sure what this actually accomplishes? Looks like it just does metatag_entity_update($node, 'node'); which will basically be a no-op.

blueminds’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

The idea was to do a mass update of nodes to generate metatags. It was handy for me when I installed metatag module on a site where content was already created and I needed to generate metatags for each node. However this is one time operation, so it might be not a good idea to have it in node operations or as a UI tool at all.

SebastienT’s picture

Is this code working?

I am looking for something similar... Don't want to edit my 2000+ nodes one by one

damienmckenna’s picture

For anyone else who finds this, because Metatag uses tokens the per-page tags should be automatically generated based on the tokens, e.g. [node:title] fills in the node's title, etc. There is no need to do this.