You are here

public function PanelizerEntityDefault::hook_entity_insert in Panelizer 7.3

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

Overrides PanelizerEntityInterface::hook_entity_insert

File

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

Class

PanelizerEntityDefault
Base class for the Panelizer Entity plugin.

Code

public function hook_entity_insert($entity) {
  list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);
  if (!$this
    ->is_panelized($bundle)) {
    return;
  }

  // Check to see if this entity is a translation of another entity.
  // If so, use the source entity's panelizer information to clone it, being
  // careful about it being the default display.
  if (isset($entity->translation_source, $entity->translation_source->panelizer)) {
    ctools_include('content');
    foreach ($entity->translation_source->panelizer as $view_mode => $panelizer) {

      // Check to see if this display mode is custom or default.
      $default_display = $this
        ->get_default_display_default_name($bundle, $view_mode);
      if (empty($panelizer) || !empty($panelizer->name) && $panelizer->name == $default_display) {
        continue;
      }

      // If it is custom, clone it deeply.
      $entity->panelizer[$view_mode] = $this
        ->clone_panelizer($panelizer, $entity);
    }
  }

  // If there's no panelizer information on the entity then there is nothing to do.
  if (empty($entity->panelizer)) {
    return;
  }

  // Allow exports or older data to be deployed successfully.
  if (is_object($entity->panelizer)) {
    $entity->panelizer = array(
      'page_manager' => $entity->panelizer,
    );
  }
  foreach ($entity->panelizer as $view_mode => $panelizer) {

    // Don't write out empty records.
    if (empty($panelizer)) {
      continue;
    }

    // Just a safety check to make sure we can't have a missing view mode.
    if (empty($view_mode)) {
      $view_mode = 'page_manager';
    }

    // In certain circumstances $panelizer will be the default's name rather
    // than a full object.
    if (!is_object($panelizer) && is_array($panelizer) && !empty($panelizer['name'])) {
      $panelizer = $this
        ->get_default_panelizer_object($bundle . '.' . $view_mode, $panelizer['name']);
      $panelizer->did = NULL;

      // Ensure original values are maintained.
      $panelizer->entity_id = $entity_id;
      $panelizer->revision_id = $revision_id;
    }

    // If this is a default display, skip saving it.
    $default_display = $this
      ->get_default_display_default_name($bundle, $view_mode);
    if (!empty($panelizer->name) && $panelizer->name == $default_display) {
      continue;
    }

    // On entity insert, we only write the display if it is not a default.
    // That probably means it came from an export or deploy or something
    // along those lines.
    if (empty($panelizer->name) && !empty($panelizer->display)) {

      // Ensure we don't accidentally overwrite existing display
      // data or anything silly like that.
      $panelizer = $this
        ->clone_panelizer($panelizer, $entity);

      // Ensure that Panels storage is set correctly.
      $panelizer->display->storage_type = 'panelizer_entity';
      $panelizer->display->storage_id = implode(':', array(
        $this->entity_type,
        $entity_id,
        $view_mode,
      ));

      // First write the display
      panels_save_display($panelizer->display);

      // Make sure we have the new did.
      $panelizer->did = $panelizer->display->did;
    }
    else {

      // Store $panelizer->name as  it is removed by clone_panelizer().
      $stored_name = $panelizer->name;

      // Clone the $panelizer object.
      $panelizer = $this
        ->clone_panelizer($panelizer, $entity);

      // Restore the original $panelizer->name.
      $panelizer->name = $stored_name;
    }

    // Make sure there is a view mode.
    if (empty($panelizer->view_mode)) {
      $panelizer->view_mode = $view_mode;
    }

    // And write the new record.
    drupal_write_record('panelizer_entity', $panelizer);
  }
}