You are here

function metatag_entity_insert in Metatag 7

Implements hook_entity_insert().

File

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

Code

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

    // Verify that this entity type / bundle is supported.
    if (!metatag_entity_supports_metatags($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->metatags[$langcode]) && isset($entity->metatags[LANGUAGE_NONE])) {
      $entity->metatags[$langcode] = $entity->metatags[LANGUAGE_NONE];
      unset($entity->metatags[LANGUAGE_NONE]);
    }

    // Support for Workbench Moderation.
    if ($entity_type == 'node' && _metatag_isdefaultrevision($entity)) {
      return;
    }
    metatag_metatags_save($entity_type, $entity_id, $revision_id, $entity->metatags, $bundle);
  }
}