You are here

function metatag_entity_update in Metatag 7

Implements hook_entity_update().

File

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

Code

function metatag_entity_update($entity, $entity_type) {
  list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);

  // If this entity object isn't allowed meta tags, don't continue.
  if (!metatag_entity_supports_metatags($entity_type, $bundle)) {
    return;
  }
  $revision_id = intval($revision_id);
  if (isset($entity->metatags)) {

    // Determine the entity's new language. This will always be accurate as the
    // language value will already have been updated by the time this function
    // executes, and it will be loaded for the correct edit process.
    $new_language = metatag_entity_get_language($entity_type, $entity);

    // If applicable, determine the entity's original language. This cannot be
    // obtained via the normal API as that data will already have been updated,
    // instead check to see if the entity has an old-fasioned 'language' value.
    if (isset($entity->original) && isset($entity->language) && isset($entity->original->language)) {
      $old_language = $entity->original->language;

      // If the language has changed then additional checking needs to be done.
      // Need to compare against the entity's raw language value as they will
      // be different when updating a translated entity, versus an untranslated
      // entity or a source entity for translation, and give a false positive.
      if ($new_language == $entity->language && $new_language != $old_language) {

        // If this entity is not translated, or if it is translated but the
        // translation was previously created, then some language cleanup needs
        // to be done.
        if (!isset($entity->translation) || isset($entity->translation) && !empty($entity->translation['created'])) {

          // Delete the old language record. This will not affect old revisions.
          db_delete('metatag')
            ->condition('entity_type', $entity_type)
            ->condition('entity_id', $entity_id)
            ->condition('revision_id', $revision_id)
            ->condition('language', $old_language)
            ->execute();

          // Swap out the metatag values for the two languages.
          if (isset($entity->metatags[$old_language])) {
            $entity->metatags[$new_language] = $entity->metatags[$old_language];
            unset($entity->metatags[$old_language]);
          }
        }
      }
    }

    // Support for Workbench Moderation.
    if ($entity_type == 'node' && _metatag_isdefaultrevision($entity)) {
      return;
    }

    // Save the record.
    metatag_metatags_save($entity_type, $entity_id, $revision_id, $entity->metatags, $bundle);
  }
  else {

    // Still ensure the meta tag output is cached.
    metatag_metatags_cache_clear($entity_type, $entity_id);
  }
}