You are here

function PanelizerEntityDefault::reset_entity_panelizer in Panelizer 7.3

Reset displays so that the defaults can be used instead.

Parameters

object $entity: The entity.

$view_mode: The view mode to delete. If not specified, all view modes will be deleted.

1 call to PanelizerEntityDefault::reset_entity_panelizer()
PanelizerEntityDefault::page_reset in plugins/entity/PanelizerEntityDefault.class.php
Switched page callback to give the settings form.

File

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

Class

PanelizerEntityDefault
Base class for the Panelizer Entity plugin.

Code

function reset_entity_panelizer($entity, $view_mode = NULL) {

  // Only proceed if the view mode was customized for this entity.
  if (empty($entity->panelizer[$view_mode])) {
    drupal_set_message(t('Unable to reset this view mode'));
  }
  else {

    // Build a list of displays to delete.
    $dids = array();

    // Identify this entity's bundle.
    list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity);

    // Add the custom display to the list of displays to delete.
    if (!empty($entity->panelizer[$view_mode]->did)) {
      $dids[] = $entity->panelizer[$view_mode]->did;
    }

    // Update the {panelizer_entity} record.
    $entity->panelizer[$view_mode]->did = NULL;
    $entity->panelizer[$view_mode]->name = NULL;

    // Update the entity.
    $this
      ->entity_save($entity);

    // If a new revision was not created, delete any unused displays.
    if (empty($entity->revision)) {

      // Work out which view modes to use.
      if (!empty($view_mode)) {
        $view_modes = array(
          $view_mode,
        );
      }
      else {
        $entity_info = entity_get_info($this->entity_type);
        $view_modes = array_keys($entity_info['view modes']);
      }

      // Locate all displays associated with the entity.
      $new_dids_query = db_select('panelizer_entity', 'p')
        ->fields('p', array(
        'did',
      ))
        ->condition('entity_type', $this->entity_type)
        ->condition('view_mode', $view_modes, 'IN')
        ->condition('did', '0', '>');

      // Not all entity types support revisions.
      if (!is_null($revision_id)) {
        $new_dids_query
          ->condition('revision_id', $revision_id);
      }
      else {
        $new_dids_query
          ->condition('entity_id', $entity_id);
      }
      $new_dids = $new_dids_query
        ->execute()
        ->fetchCol();
      if (!empty($new_dids)) {
        $dids = array_merge($dids, $new_dids);
      }

      // Delete the display records if they are not still in use.
      foreach (array_unique($dids) as $did) {
        panels_delete_display($did);
      }

      // Delete the {panelizer_entity} records.
      db_delete('panelizer_entity')
        ->condition('entity_type', $this->entity_type)
        ->condition('view_mode', $view_modes, 'IN')
        ->condition('did', $dids, 'IN')
        ->execute();

      // Reset the entity's cache. If the EntityCache module is enabled, this
      // also resets its permanent cache.
      entity_get_controller($this->entity_type)
        ->resetCache(array(
        $entity_id,
      ));
    }
  }
}