diff --git a/plugins/entity/PanelizerEntityDefault.class.php b/plugins/entity/PanelizerEntityDefault.class.php old mode 100644 new mode 100755 index c9dc149..912f6a0 --- a/plugins/entity/PanelizerEntityDefault.class.php +++ b/plugins/entity/PanelizerEntityDefault.class.php @@ -657,13 +657,32 @@ abstract class PanelizerEntityDefault implements PanelizerEntityInterface { } } - if (!$ids) { + if (empty($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'); + // When accessing pane configurations through the panelizer panels backend + // page, we need to make sure, that we are working on the latest display. + // Else it will work on the latest published node/display and that will in + // many cases not match the latest display, linked to a non-published + // node. + if (strpos($_GET['q'], 'panels/ajax/editor') === 0) { + $panelizers = db_query(" + SELECT + * + FROM {panelizer_entity} + WHERE + entity_type = '$this->entity_type' + AND entity_id IN (:ids) + ORDER BY revision_id ASC + ", + array(':ids' => $ids))->fetchAllAssoc('entity_id'); + } + else { + $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'); @@ -840,6 +859,11 @@ abstract class PanelizerEntityDefault implements PanelizerEntityInterface { // And write the new record. return drupal_write_record('panelizer_entity', $panelizer, $update); } + elseif (isset($entity->panelizer) && is_array($entity->panelizer) && !empty($entity->panelizer['name']) && !empty($entity->original->panelizer)) { + $entity->panelizer = $entity->original->panelizer; + $entity->panelizer->revision_id = $revision_id; + drupal_write_record('panelizer_entity', $entity->panelizer); + } else { $entity->panelizer->entity_type = $this->entity_type; $entity->panelizer->entity_id = $entity_id; @@ -1073,7 +1097,7 @@ abstract class PanelizerEntityDefault implements PanelizerEntityInterface { $this->entity_save($entity); } else { - $this->delete_entity_panelizer($entity); + $this->reset_entity_panelizer($entity, !empty($form_state['values']['revision'])); } drupal_goto($_GET['q']); @@ -1221,6 +1245,64 @@ abstract class PanelizerEntityDefault implements PanelizerEntityInterface { } /** + * Reset panelized entity to default. + * + * @param object &$entity + * Current entity being worked on. + * @param boolean $new_revision + * Create a new revision, but only if we actually have a default panelizer + * display to attach. Else revision is ignored. + * + * @return boolean + * TRUE if there was a default panelizer display to attach. FALSE if the + * panelizer display was just deleted. + */ + function reset_entity_panelizer(&$entity, $new_revision = FALSE) { + list($entity_id, $revision_id, $bundle) = entity_extract_ids($this->entity_type, $entity); + + $panelizer_default_name = FALSE; + if ($this->has_default_panel($bundle)) { + $panelizer_default_name = implode(':', array($this->entity_type, $bundle, 'default')); + } + + if (!$panelizer_default_name) { + // Fall back to default handling of panelizer reset. + $this->delete_entity_panelizer($entity); + return FALSE; + } + + $default_panelizers = $this->get_default_panelizer_objects($bundle); + + $default_panelizer_object = isset($default_panelizers[$panelizer_default_name]) ? $default_panelizers[$panelizer_default_name] : FALSE; + + if (!$default_panelizer_object) { + // Fall back to default handling of panelizer reset. + $this->delete_entity_panelizer($entity); + return FALSE; + } + + $default_panelizer_object->did = 0; + + // Handle revisions. + if ($new_revision && $this->entity_allows_revisions($entity)) { + $entity->revision = TRUE; + } + else { + // Make room for the new panelizer object, replacing the existing entry. + db_delete('panelizer_entity') + ->condition('entity_type', $this->entity_type) + ->condition('entity_id', $entity_id) + ->condition('revision_id', $revision_id) + ->execute(); + } + // Reset the panelizer object and save the entity. + $entity->panelizer = $default_panelizer_object; + $this->entity_save($entity); + + return TRUE; + } + + /** * Determine if a bundle is panelized. */ public function is_panelized($bundle) {