diff --git a/src/Form/PanelizerWizardContextConfigure.php b/src/Form/PanelizerWizardContextConfigure.php index ddc8c73..d171fff 100644 --- a/src/Form/PanelizerWizardContextConfigure.php +++ b/src/Form/PanelizerWizardContextConfigure.php @@ -26,7 +26,8 @@ class PanelizerWizardContextConfigure extends ContextConfigure { * {@inheritdoc} */ protected function getContexts($cached_values) { - return $cached_values['contexts']; + $static_contexts = $cached_values['page_variant']->getStaticContexts(); + return $this->getContextMapper()->getContextValues($static_contexts); } /** @@ -39,7 +40,7 @@ class PanelizerWizardContextConfigure extends ContextConfigure { 'description' => $context->getContextDefinition()->getDescription(), 'value' => strpos($context->getContextDefinition()->getDataType(), 'entity:') === 0 ? $context->getContextValue()->uuid() : $context->getContextValue(), ]; - $cached_values['contexts'][$context_id] = $context_config; + $cached_values['page_variant']->setStaticContext($context_id, $context_config); return $cached_values; } @@ -55,9 +56,18 @@ class PanelizerWizardContextConfigure extends ContextConfigure { */ protected function disableMachineName($cached_values, $machine_name) { if ($machine_name) { - return !empty($cached_values['contexts'][$machine_name]); + return !empty($cached_values['page_variant']->getStaticContext($machine_name)); } return FALSE; } + /** + * Wraps the context mapper. + * + * @return \Drupal\page_manager\ContextMapperInterface + */ + protected function getContextMapper() { + return \Drupal::service('page_manager.context_mapper'); + } + } diff --git a/src/Form/PanelizerWizardContextDeleteForm.php b/src/Form/PanelizerWizardContextDeleteForm.php index c5339c5..ec4f3b5 100644 --- a/src/Form/PanelizerWizardContextDeleteForm.php +++ b/src/Form/PanelizerWizardContextDeleteForm.php @@ -28,7 +28,8 @@ class PanelizerWizardContextDeleteForm extends ContextDelete { */ public function getQuestion() { $cached_values = $this->getTempstore(); - return $this->t('Are you sure you want to delete the context @label?', ['@label' => $cached_values['contexts'][$this->context_id]['label']]); + $context = $cached_values['page_variant']->getStaticContext($this->context_id); + return $this->t('Are you sure you want to delete the context @label?', ['@label' => $context['label']]); } /** @@ -48,8 +49,9 @@ class PanelizerWizardContextDeleteForm extends ContextDelete { */ public function submitForm(array &$form, FormStateInterface $form_state) { $cached_values = $this->getTempstore(); - drupal_set_message($this->t('The static context %label has been removed.', ['%label' => $cached_values['contexts'][$this->context_id]['label']])); - unset($cached_values['contexts'][$this->context_id]); + $context = $cached_values['page_variant']->getStaticContext($this->context_id); + drupal_set_message($this->t('The static context %label has been removed.', ['%label' => $context['label']])); + $cached_values['page_variant']->removeStaticContext($this->context_id); $this->setTempstore($cached_values); parent::submitForm($form, $form_state); } diff --git a/src/Form/PanelizerWizardContextForm.php b/src/Form/PanelizerWizardContextForm.php index dd3556a..bbb8a18 100644 --- a/src/Form/PanelizerWizardContextForm.php +++ b/src/Form/PanelizerWizardContextForm.php @@ -61,11 +61,7 @@ class PanelizerWizardContextForm extends ManageContext { * {@inheritdoc} */ protected function getContexts($cached_values) { - if (!isset($cached_values['contexts'])) { - $cached_values['contexts'] = []; - } - - return $this->getContextMapper()->getContextValues($cached_values['contexts']); + return $this->getContextMapper()->getContextValues($cached_values['page_variant']->getStaticContexts()); } /** @@ -81,7 +77,7 @@ class PanelizerWizardContextForm extends ManageContext { * {@inheritdoc} */ protected function getTempstoreId() { - return 'panelizer.wizard.contexts'; + return 'panelizer.wizard'; } /** @@ -108,7 +104,8 @@ class PanelizerWizardContextForm extends ManageContext { * {@inheritdoc} */ protected function isEditableContext($cached_values, $row) { - return !empty($cached_values['contexts'][$row]); + $context = $cached_values['page_variant']->getStaticContext($row); + return !empty($context['value']); } } diff --git a/src/Wizard/PanelizerEditWizard.php b/src/Wizard/PanelizerEditWizard.php index d5a5e3e..1c2a355 100644 --- a/src/Wizard/PanelizerEditWizard.php +++ b/src/Wizard/PanelizerEditWizard.php @@ -35,27 +35,22 @@ class PanelizerEditWizard extends FormWizardBase { $cached_values['access'] = []; if (empty($cached_values['page_variant'])) { - // Create a page variant to host the panels display - $variant_plugin_id = 'panels_variant'; - $page_variant = \Drupal::entityManager() + // Load the page variant. + $cached_values['page_variant'] = \Drupal::entityManager() ->getStorage('page_variant') - ->create([ - 'variant' => $variant_plugin_id, - 'id' => $cached_values['id'] . '__variant', - ]); - $cached_values['page_variant'] = $page_variant; + ->load($cached_values['id'] . '__variant'); - // Load the panels variant data. + // Load the panels display variant. $panelizer = \Drupal::service('panelizer'); list($entity_type, $bundle, $display_id) = explode('__', $this->getMachineName()); $variant_plugin = $panelizer->getDefaultPanelsDisplay($display_id, $entity_type, $bundle, $display_id); $cached_values['plugin'] = $variant_plugin; - $cached_values['contexts'] = $cached_values['plugin']->getContexts(); // Load general settings. $settings = $panelizer->getPanelizerSettings($entity_type, $bundle, $display_id); $cached_values['custom'] = $settings['custom']; } + $form_state->setTemporaryValue('wizard', $cached_values); $form = parent::buildForm($form, $form_state); return $form; @@ -269,11 +264,7 @@ class PanelizerEditWizard extends FormWizardBase { * {@inheritdoc} */ protected function getContexts($cached_values) { - if (!isset($cached_values['contexts'])) { - $cached_values['contexts'] = []; - } - - return $this->getContextMapper()->getContextValues($cached_values['contexts']); + return $this->getContextMapper()->getContextValues($cached_values['page_variant']->getStaticContexts()); } } diff --git a/src/Wizard/PanelizerWizard.php b/src/Wizard/PanelizerWizard.php index 772024a..64bcd04 100644 --- a/src/Wizard/PanelizerWizard.php +++ b/src/Wizard/PanelizerWizard.php @@ -88,6 +88,25 @@ class PanelizerWizard extends FormWizardBase { 'variant' => $variant_plugin_id, 'id' => $cached_values['id'] . '__variant', ]); + + // Add a dummy page entity to the page variant. This is needed so the + // variant can be saved. + $page = \Drupal::entityManager() + ->getStorage('page') + ->create([ + 'id' => $cached_values['id'] . '__page', + ]); + $page_variant->setPageEntity($page); + + // Initialize contexts by adding the current entity context. + list($entity_type_id, $bundle, $display_id) = explode('__', $cached_values['id']); + $page_variant->setStaticContext('panelizer_context_entity', [ + 'label' => 'Current entity', + 'type' => 'entity:' . $entity_type_id, + 'description' => 'The entity being viewed', + 'value' => NULL, + ]); + $cached_values['page_variant'] = $page_variant; } $form_state->setTemporaryValue('wizard', $cached_values); @@ -100,7 +119,10 @@ class PanelizerWizard extends FormWizardBase { public function finish(array &$form, FormStateInterface $form_state) { $cached_values = $form_state->getTemporaryValue('wizard'); - // Save the wizard data into the Panelizer settings for this display mode. + $cached_values['page_variant']->save(); + + // Save the panels display mode and its custom settings as third party + // data of the display mode for this entity+bundle+display. $panelizer = \Drupal::service('panelizer'); $panelizer_entity_manager = \Drupal::service('plugin.manager.panelizer_entity'); $panels_display_manager = \Drupal::service('panels.display_manager'); @@ -133,11 +155,7 @@ class PanelizerWizard extends FormWizardBase { * {@inheritdoc} */ protected function getContexts($cached_values) { - if (!isset($cached_values['contexts'])) { - $cached_values['contexts'] = []; - } - - return $this->getContextMapper()->getContextValues($cached_values['contexts']); + return $this->getContextMapper()->getContextValues($cached_values['page_variant']->getStaticContexts()); } /**