You are here

function metatag_metatags_save in Metatag 7

Save an entity's tags.

Parameters

string $entity_type: The entity type to load.

int $entity_id: The entity's primary ID.

int $revision_id: The entity's revision ID.

array $metatags: All of the tag information, keyed by the language code. Most meta tags use the 'value' element, so the structure should look like:

array(
  LANGUAGE_NONE => array(
    'title' => array(
      'value' => "This node's title!",
    ),
    'og:title' => array(
      'value' => "This node's title for Open Graph!",
    ),
    'og:image' => array(
      'value' => "[node:field_thumbnail]",
    ),
  ),
);

string|null $bundle: The bundle of the entity that is being saved. Optional.

8 calls to metatag_metatags_save()
metatag_bulk_revert_batch_operation in ./metatag.admin.inc
Batch callback: delete custom metatags for selected bundles.
metatag_commerce_product_form_submit in ./metatag.module
Form API submission callback for Commerce product.
metatag_devel_node_insert in metatag_devel/metatag_devel.module
Implements hook_node_insert().
metatag_entity_insert in ./metatag.module
Implements hook_entity_insert().
metatag_entity_update in ./metatag.module
Implements hook_entity_update().

... See full list

File

./metatag.module, line 736
Primary hook implementations for Metatag.

Code

function metatag_metatags_save($entity_type, $entity_id, $revision_id, array $metatags, $bundle = NULL) {

  // Check that $entity_id is numeric because of Entity API and string IDs.
  if (!is_numeric($entity_id)) {
    return;
  }

  // Don't do anything if the entity type is not supported.
  if (!metatag_entity_supports_metatags($entity_type)) {
    return;
  }

  // Verify the entity bundle is supported, if not available just check the
  // entity type.
  if (!empty($bundle)) {
    if (!metatag_entity_supports_metatags($entity_type, $bundle)) {
      return;
    }
  }
  else {
    if (!metatag_entity_supports_metatags($entity_type)) {
      return;
    }
  }

  // The revision_id must be a numeric value; some entities use NULL for the
  // revision so change that to a zero.
  if (is_null($revision_id)) {
    $revision_id = 0;
  }

  // Handle scenarios where the metatags are completely empty, this will have
  // the effect of erasing the meta tags for those this entity.
  if (empty($metatags)) {
    $metatags = array();

    // Add an empty array record for each language.
    $languages = db_query("SELECT language\n        FROM {metatag}\n        WHERE (entity_type = :type)\n        AND (entity_id = :id)\n        AND (revision_id = :revision)", array(
      ':type' => $entity_type,
      ':id' => $entity_id,
      ':revision' => $revision_id,
    ))
      ->fetchCol();
    foreach ($languages as $oldlang) {
      $metatags[$oldlang] = array();
    }
  }

  // Update each of the per-language metatag configurations in turn.
  foreach ($metatags as $langcode => $new_metatags) {

    // Allow other modules to alter the meta tags prior to saving using
    // hook_metatag_presave().
    foreach (module_implements('metatag_presave') as $module) {
      $function = "{$module}_metatag_presave";
      $function($new_metatags, $entity_type, $entity_id, $revision_id, $langcode);
    }

    // If the data array is empty, there is no data to actually save, so just
    // delete the record from the database.
    if (empty($new_metatags)) {
      db_delete('metatag')
        ->condition('entity_type', $entity_type)
        ->condition('entity_id', $entity_id)
        ->condition('revision_id', $revision_id)
        ->condition('language', $langcode)
        ->execute();
    }
    else {
      db_merge('metatag')
        ->key(array(
        'entity_type' => $entity_type,
        'entity_id' => $entity_id,
        'language' => $langcode,
        'revision_id' => $revision_id,
      ))
        ->fields(array(
        'data' => serialize($new_metatags),
      ))
        ->execute();
    }
  }

  // Clear cached data.
  metatag_metatags_cache_clear($entity_type, $entity_id);

  // Clear the entity cache.
  entity_get_controller($entity_type)
    ->resetCache(array(
    $entity_id,
  ));
}