You are here

function yoast_seo_entity_update in Real-time SEO for Drupal 7

Implements hook_entity_update().

File

./yoast_seo.module, line 705
Primary hook implementations for Yoast SEO for Drupal module.

Code

function yoast_seo_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 (!yoast_seo_entity_supports_yoast_seo($entity_type, $bundle)) {
    return;
  }
  $revision_id = intval($revision_id);
  if (isset($entity->yoast_seo)) {

    // 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 = entity_language($entity_type, (object) $entity);
    if (empty($new_language)) {
      $new_language = LANGUAGE_NONE;
    }

    // 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('yoast_seo')
            ->condition('entity_type', $entity_type)
            ->condition('entity_id', $entity_id)
            ->condition('revision_id', $revision_id)
            ->condition('language', $old_language)
            ->execute();

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

    // Save the record.
    $old_vid = isset($entity->old_vid) ? $entity->old_vid : NULL;
    yoast_seo_configuration_save($entity_type, $entity_id, $revision_id, $entity->yoast_seo, $new_language, $old_vid);
  }
}