You are here

public function PanelizerEntityDefault::hook_entity_load in Panelizer 7.3

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

Overrides PanelizerEntityInterface::hook_entity_load

File

plugins/entity/PanelizerEntityDefault.class.php, line 1412
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) {

    // Don't bother if somehow we've already loaded and are asked to
    // load again.
    if (!empty($entity->panelizer)) {
      continue;
    }
    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 (empty($ids)) {
    return;
  }

  // Load all the panelizers associated with the list of entities.
  if ($this->supports_revisions) {
    $result = db_query("SELECT * FROM {panelizer_entity} WHERE entity_type = :entity_type AND revision_id IN (:vids)", array(
      ':entity_type' => $this->entity_type,
      ':vids' => $vids,
    ));
  }
  else {
    $result = db_query("SELECT * FROM {panelizer_entity} WHERE entity_type = :entity_type AND entity_id IN (:ids)", array(
      ':entity_type' => $this->entity_type,
      ':ids' => $ids,
    ));
  }
  $panelizers = array();
  while ($panelizer = $result
    ->fetchObject()) {
    $panelizers[$panelizer->entity_id][$panelizer->view_mode] = $panelizer;
  }
  $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;
    }

    // Check for each view mode.
    foreach ($this->plugin['view modes'] as $view_mode => $view_mode_info) {

      // Skip disabled view modes.
      $check_needed = array_key_exists($view_mode, $this->plugin['bundles'][$bundles[$entity_id]]['view modes']);
      $view_mode_disabled = empty($this->plugin['bundles'][$bundles[$entity_id]]['view modes'][$view_mode]['status']);
      if ($check_needed === FALSE || $view_mode_disabled) {
        continue;
      }

      // Load the default display for this entity bundle / view_mode.
      $name = $this
        ->get_default_display_name($bundles[$entity_id], $view_mode);

      // If no panelizer was loaded for the view mode, queue up defaults.
      if (empty($panelizers[$entity_id][$view_mode]) && $this
        ->has_default_panel($bundles[$entity_id] . '.' . $view_mode)) {
        $defaults[$name] = $name;
      }
      else {
        if (!empty($panelizers[$entity_id][$view_mode])) {
          $entity->panelizer[$view_mode] = ctools_export_unpack_object('panelizer_entity', $panelizers[$entity_id][$view_mode]);

          // If somehow we have no name AND no did, fill in the default.
          // This can happen if use of defaults has switched around maybe?
          if (empty($entity->panelizer[$view_mode]->did) && empty($entity->panelizer[$view_mode]->name)) {
            if ($this
              ->has_default_panel($bundles[$entity_id] . '.' . $view_mode)) {
              $entity->panelizer[$view_mode]->name = $name;
            }
            else {

              // With no default, did or name, this doesn't actually exist.
              unset($entity->panelizer[$view_mode]);
              list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
              db_delete('panelizer_entity')
                ->condition('entity_type', $this->entity_type)
                ->condition('entity_id', $entity_id)
                ->condition('revision_id', $revision_id)
                ->condition('view_mode', $view_mode)
                ->execute();
              continue;
            }
          }

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

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

  // if any panelizers were loaded, get their attached displays.
  if (!empty($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;
    }

    // Reload these.
    list(, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);

    // Check for each view mode.
    foreach ($this->plugin['view modes'] as $view_mode => $view_mode_info) {
      if (empty($entity->panelizer[$view_mode])) {

        // Load the configured default display.
        $default_value = $this
          ->get_default_display_name($bundle, $view_mode);
        if (!empty($panelizer_defaults[$default_value])) {
          $entity->panelizer[$view_mode] = clone $panelizer_defaults[$default_value];

          // Make sure this entity can't write to the default display.
          $entity->panelizer[$view_mode]->did = NULL;
          $entity->panelizer[$view_mode]->entity_id = 0;
          $entity->panelizer[$view_mode]->revision_id = 0;
        }
      }
      elseif (empty($entity->panelizer[$view_mode]->display) || empty($entity->panelizer[$view_mode]->did)) {
        if (!empty($entity->panelizer[$view_mode]->did)) {
          if (empty($displays[$entity->panelizer[$view_mode]->did])) {

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

            // Reload the settings from the default configuration.
            $entity->panelizer[$view_mode] = clone $panelizer_defaults[$entity->panelizer[$view_mode]->name];
            $entity->panelizer[$view_mode]->did = NULL;
            $entity->panelizer[$view_mode]->entity_id = $entity_id;
            $entity->panelizer[$view_mode]->revision_id = $revision_id;
          }
        }
      }
    }
  }
}