You are here

function yoast_seo_entity_load in Real-time SEO for Drupal 7

Implements hook_entity_load().

File

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

Code

function yoast_seo_entity_load($entities, $entity_type) {
  if (yoast_seo_entity_supports_yoast_seo($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 Yoast SEO.
      if (yoast_seo_entity_supports_yoast_seo($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 SEO info for these entities.
      $seo_info = yoast_seo_configuration_load_multiple($entity_type, $entity_ids, $revision_ids);

      // Assign the metatag records for the correct revision ID.
      if (!empty($seo_info)) {
        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]->yoast_seo = isset($seo_info[$entity_id][$revision_id]) ? $seo_info[$entity_id][$revision_id] : array();
        }
      }
    }
  }
}