You are here

public function PanelizerEntityDefault::hook_entity_load in Panelizer 7.2

Same name and namespace in other branches
  1. 7.3 plugins/entity/PanelizerEntityDefault.class.php \PanelizerEntityDefault::hook_entity_load()

Overrides PanelizerEntityInterface::hook_entity_load

File

plugins/entity/PanelizerEntityDefault.class.php, line 643
Base class for the Panelizer Entity plugin.

Class

PanelizerEntityDefault
Base class for the Panelizer Entity plugin.

Code

public function hook_entity_load(&$entities) {
  ctools_include('export');
  $ids = array();
  $vids = array();
  $bundles = array();
  foreach ($entities as $entity) {
    list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
    if ($this
      ->is_panelized($bundle)) {
      $ids[] = $entity_id;
      if ($this->supports_revisions) {
        $vids[] = $revision_id;
      }
      $bundles[$entity_id] = $bundle;
    }
  }
  if (!$ids) {
    return;
  }

  // Load all the panelizers associated with the list of entities.
  if ($this->supports_revisions) {
    $panelizers = db_query("SELECT * FROM {panelizer_entity} WHERE entity_type = '{$this->entity_type}' AND entity_id IN (:ids) AND revision_id IN (:vids)", array(
      ':ids' => $ids,
      ':vids' => $vids,
    ))
      ->fetchAllAssoc('entity_id');
  }
  else {
    $panelizers = db_query("SELECT * FROM {panelizer_entity} WHERE entity_type = '{$this->entity_type}' AND entity_id IN (:ids)", array(
      ':ids' => $ids,
    ))
      ->fetchAllAssoc('entity_id');
  }
  $defaults = array();
  $dids = array();

  // Go through our entity list and generate a list of defaults and displays
  foreach ($entities as $entity_id => $entity) {

    // Don't bother if somehow we've already loaded and are asked to
    // load again.
    if (!empty($entity->panelizer)) {
      continue;
    }

    // Skip not panelized bundles.
    if (empty($bundles[$entity_id])) {
      continue;
    }

    // If no panelizer was loaded, queue up defaults to load.
    if (empty($panelizers[$entity_id])) {
      if ($this
        ->has_default_panel($bundles[$entity_id])) {
        $name = implode(':', array(
          $this->entity_type,
          $bundles[$entity_id],
          'default',
        ));
        $defaults[$name] = $name;
      }
    }
    else {
      $entity->panelizer = ctools_export_unpack_object('panelizer_entity', $panelizers[$entity_id]);

      // Panelizers that do not have dids are just a selection of defaults
      // that has never actually been modified.
      if (empty($entity->panelizer->did) && !empty($entity->panelizer->name)) {
        $defaults[] = $entity->panelizer->name;
      }
      else {
        $dids[$entity->panelizer->did] = $entity->panelizer->did;
      }
    }
  }

  // Load any defaults we collected.
  if ($defaults) {
    $panelizer_defaults = $this
      ->load_default_panelizer_objects($defaults);
  }

  // if any panelizers were loaded, get their attached displays.
  if ($dids) {
    $displays = panels_load_displays($dids);
  }

  // Now, go back through our entities and assign dids and defaults
  // accordingly.
  foreach ($entities as $entity_id => $entity) {

    // Skip not panelized bundles.
    if (empty($bundles[$entity_id])) {
      continue;
    }
    if (empty($entity->panelizer)) {
      $default_key = implode(':', array(
        $this->entity_type,
        $bundles[$entity_id],
        'default',
      ));
      if (!empty($panelizer_defaults[$default_key])) {
        $entity->panelizer = clone $panelizer_defaults[$default_key];

        // make sure this entity can't write to the default display.
        $entity->panelizer->did = NULL;
      }
    }
    else {
      if (empty($entity->panelizer->display)) {
        if (!empty($entity->panelizer->did)) {
          if (empty($displays[$entity->panelizer->did])) {

            // Somehow the display for this entity has gotten lost?
            $entity->panelizer->did = NULL;
            $entity->panelizer->display = $this
              ->get_default_display();
          }
          else {
            $entity->panelizer->display = $displays[$entity->panelizer->did];
          }
        }
        else {
          if (!empty($panelizer_defaults[$entity->panelizer->name])) {
            $entity->panelizer->display = $panelizer_defaults[$entity->panelizer->name]->display;
          }
        }
      }
    }
  }
}