You are here

function yoast_seo_entity_insert in Real-time SEO for Drupal 7

Implements hook_entity_insert().

File

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

Code

function yoast_seo_entity_insert($entity, $entity_type) {
  if (isset($entity->yoast_seo)) {
    list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);

    // Verify that this entity type / bundle is supported.
    if (!yoast_seo_entity_supports_yoast_seo($entity_type, $bundle)) {
      return;
    }
    $revision_id = intval($revision_id);

    // Determine the entity's language.
    $langcode = entity_language($entity_type, $entity);

    // Unfortunately due to how core works, the normal entity_language()
    // function returns 'und' instead of the node's language during node
    // creation.
    if ((empty($langcode) || $langcode == LANGUAGE_NONE) && !empty($entity->language)) {
      $langcode = $entity->language;
    }

    // If no language was still found, use the 'no language' value.
    if (empty($langcode)) {
      $langcode = LANGUAGE_NONE;
    }

    // Work-around for initial entity creation where a language was selection
    // but where it's different to the form's value.
    if (!isset($entity->yoast_seo[$langcode]) && isset($entity->yoast_seo[LANGUAGE_NONE])) {
      $entity->yoast_seo[$langcode] = $entity->yoast_seo[LANGUAGE_NONE];
      unset($entity->yoast_seo[LANGUAGE_NONE]);
    }
    yoast_seo_configuration_save($entity_type, $entity_id, $revision_id, $entity->yoast_seo, $langcode);
  }
}