You are here

function metatag_entity_load in Metatag 7

Implements hook_entity_load().

File

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

Code

function metatag_entity_load($entities, $entity_type) {

  // Wrap this in a try-catch block to work around occasions when the schema
  // hasn't been updated yet.
  try {
    if (metatag_entity_supports_metatags($entity_type)) {

      // Get the revision_ids.
      $revision_ids = array();

      // Track the entity IDs for values to load.
      $entity_ids = array();

      // Some entities don't support revisions.
      $supports_revisions = TRUE;

      // Extract the revision ID and verify the entity's bundle is supported.
      foreach ($entities as $key => $entity) {
        list($entity_id, $revision_id, $bundle) = entity_extract_ids($entity_type, $entity);

        // Verify that each entity bundle supports Metatag.
        if (metatag_entity_supports_metatags($entity_type, $bundle)) {
          $entity_ids[] = $entity_id;
          if (!empty($revision_id)) {
            $revision_ids[] = $revision_id;
          }
        }
      }

      // Only proceed if either there were revision IDs identified, or the
      // entity doesn't support revisions anyway.
      if (!empty($entity_ids)) {

        // Load all meta tags for these entities.
        $metatags = metatag_metatags_load_multiple($entity_type, $entity_ids, $revision_ids);

        // Assign the metatag records for the correct revision ID.
        if (!empty($metatags)) {
          foreach ($entities as $entity_id => $entity) {
            list($entity_id, $revision_id) = entity_extract_ids($entity_type, $entity);
            $revision_id = intval($revision_id);
            $entities[$entity_id]->metatags = isset($metatags[$entity_id][$revision_id]) ? $metatags[$entity_id][$revision_id] : array();
          }
        }
      }
    }
  } catch (Exception $e) {
    watchdog('metatag', 'Error loading meta tag data, do the <a href="@update">database updates</a> need to be run? The error occurred when loading record(s) %ids for the %type entity type. The error message was: %error', array(
      '@update' => base_path() . 'update.php',
      '%ids' => implode(', ', array_keys($entities)),
      '%type' => $entity_type,
      '%error' => $e
        ->getMessage(),
    ), WATCHDOG_WARNING);

    // Don't display the same message twice for Drush.
    if (drupal_is_cli()) {
      drupal_set_message(t('Run your updates, like drush updb.'));
    }
    elseif (user_access('access site reports')) {
      drupal_set_message(t('Error loading meta tag data, do the <a href="@update">database updates</a> need to be run?', array(
        '@update' => base_path() . 'update.php',
      )), 'error');
    }
  }
}