diff --git a/panelizer.routing.yml b/panelizer.routing.yml index 2bda08e..956b33d 100644 --- a/panelizer.routing.yml +++ b/panelizer.routing.yml @@ -17,7 +17,7 @@ panelizer.panels_ipe.revert_to_default: panelizer.wizard.add: path: '/admin/structure/panelizer/add/{machine_name}' defaults: - _wizard: '\Drupal\panelizer\Wizard\PanelizerWizard' + _wizard: '\Drupal\panelizer\Wizard\PanelizerAddWizard' _title: 'Panelizer Wizard' tempstore_id: 'panelizer.wizard' requirements: @@ -26,7 +26,7 @@ panelizer.wizard.add: panelizer.wizard.add.step: path: '/admin/structure/panelizer/add/{machine_name}/{step}' defaults: - _wizard: '\Drupal\panelizer\Wizard\PanelizerWizard' + _wizard: '\Drupal\panelizer\Wizard\PanelizerAddWizard' _title: 'Panelizer Wizard' tempstore_id: 'panelizer.wizard' requirements: diff --git a/src/Tests/PanelizerNodeFunctionalTest.php b/src/Tests/PanelizerNodeFunctionalTest.php index 8fb379c..3ffda7f 100644 --- a/src/Tests/PanelizerNodeFunctionalTest.php +++ b/src/Tests/PanelizerNodeFunctionalTest.php @@ -64,7 +64,10 @@ class PanelizerNodeFunctionalTest extends WebTestBase { $this->drupalGet('admin/structure/types/manage/article/display'); $this->clickLink('Panelize this view mode'); // General settings step. - $edit = ['custom' => TRUE]; + $edit = [ + 'enable' => TRUE, + 'custom' => TRUE, + ]; // Contexts step. $this->drupalPostForm(NULL, $edit, 'Next'); diff --git a/src/Wizard/PanelizerAddWizard.php b/src/Wizard/PanelizerAddWizard.php new file mode 100644 index 0000000..66f7dbd --- /dev/null +++ b/src/Wizard/PanelizerAddWizard.php @@ -0,0 +1,61 @@ +getTemporaryValue('wizard'); + $cached_values['id'] = $this->getMachineName(); + // Some variants like PanelsDisplayVariant need this. Set it to empty. + $cached_values['access'] = []; + if (empty($cached_values['page_variant'])) { + $variant_plugin_id = 'panels_variant'; + $page_variant = \Drupal::entityManager() + ->getStorage('page_variant') + ->create([ + '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); + return $form; + } + +} diff --git a/src/Wizard/PanelizerEditWizard.php b/src/Wizard/PanelizerEditWizard.php index f2d6eed..6fa20c6 100644 --- a/src/Wizard/PanelizerEditWizard.php +++ b/src/Wizard/PanelizerEditWizard.php @@ -1,8 +1,4 @@ t('Wizard Information'); - } - - /** - * {@inheritdoc} - */ - public function getMachineLabel() { - return $this->t('Wizard Test Name'); - } - - /** - * {@inheritdoc} - */ - public function getOperations($cached_values) { - $operations = [ - 'general' => [ - 'form' => PanelizerWizardGeneralForm::class, - 'title' => $this->t('General settings'), - ], - 'contexts' => [ - 'form' => PanelizerWizardContextForm::class, - 'title' => $this->t('Contexts'), - ], - ]; - - // Add any wizard operations from the plugin itself. - if (isset($cached_values['page_variant'])) { - $page_variant = $cached_values['page_variant']; - $variant_plugin = $page_variant->getVariantPlugin(); - $variant_plugin->setContexts($this->getContexts($cached_values)); - foreach ($variant_plugin->getWizardOperations($cached_values) as $name => $operation) { - $operation['values']['plugin'] = $variant_plugin; - $operation['submit'][] = '::submitVariantStep'; - $operations[$name] = $operation; - } - - // Change the class that manages the Content step. - if (isset($operations['content'])) { - $operations['content']['form'] = PanelizerWizardContentForm::class; - } - } - - return $operations; - } - - /** - * {@inheritdoc} - */ - public function getRouteName() { - return 'panelizer.wizard.edit'; - } - - /** - * {@inheritdoc} - */ protected function customizeForm(array $form, FormStateInterface $form_state) { $cached_values = $form_state->getTemporaryValue('wizard'); @@ -253,52 +199,6 @@ class PanelizerEditWizard extends FormWizardBase { } /** - * Submission callback for the variant plugin steps. - */ - public function submitVariantStep(array &$form, FormStateInterface $form_state) { - $cached_values = $form_state->getTemporaryValue('wizard'); - /** @var \Drupal\page_manager\PageVariantInterface $page_variant */ - $page_variant = $cached_values['page_variant']; - /** @var \Drupal\Core\Display\VariantInterface $plugin */ - $plugin = $cached_values['plugin']; - - // Make sure the variant plugin on the page variant gets the configuration - // from the 'plugin' which should have been setup by the variant's steps. - if (!empty($plugin) && !empty($page_variant)) { - $page_variant->getVariantPlugin()->setConfiguration($plugin->getConfiguration()); - } - } - - /** - * {@inheritdoc} - */ - public function finish(array &$form, FormStateInterface $form_state) { - $cached_values = $form_state->getTemporaryValue('wizard'); - - $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'); - list($entity_type, $bundle, $display_id) = explode('__', $cached_values['id']); - $display = $panelizer->getEntityViewDisplay($entity_type, $bundle, $display_id); - $settings = $panelizer->getPanelizerSettings($entity_type, $bundle, $display_id); - $settings['enable'] = $cached_values['enable']; - $settings['custom'] = $cached_values['custom']; - /** @var \Drupal\panelizer\Plugin\PanelizerEntityInterface $panelizer_entity_plugin */ - $panelizer_entity_plugin = $panelizer_entity_manager->createInstance($display->getTargetEntityTypeId(), []); - $displays = $display->getThirdPartySetting('panelizer', 'displays', []); - $displays[$display_id] = $panels_display_manager->exportDisplay($cached_values['plugin']); - $display->setThirdPartySetting('panelizer', 'displays', $displays); - $panelizer->setPanelizerSettings($entity_type, $bundle, $display_id, $settings, $display); - - parent::finish($form, $form_state); - $form_state->setRedirect('panelizer.wizard.edit', ['machine_name' => $cached_values['id']]); - } - - /** * Clears the temporary store. * * @param array $form @@ -308,20 +208,4 @@ class PanelizerEditWizard extends FormWizardBase { $this->getTempstore()->delete($this->getMachineName()); } - /** - * Wraps the context mapper. - * - * @return \Drupal\page_manager\ContextMapperInterface - */ - protected function getContextMapper() { - return \Drupal::service('page_manager.context_mapper'); - } - - /** - * {@inheritdoc} - */ - protected function getContexts($cached_values) { - return $this->getContextMapper()->getContextValues($cached_values['page_variant']->getStaticContexts()); - } - } diff --git a/src/Wizard/PanelizerWizard.php b/src/Wizard/PanelizerWizard.php deleted file mode 100644 index 64bcd04..0000000 --- a/src/Wizard/PanelizerWizard.php +++ /dev/null @@ -1,178 +0,0 @@ -t('Wizard Information'); - } - - /** - * {@inheritdoc} - */ - public function getMachineLabel() { - return $this->t('Wizard Test Name'); - } - - /** - * {@inheritdoc} - */ - public function getOperations($cached_values) { - $operations = [ - 'general' => [ - 'form' => PanelizerWizardGeneralForm::class, - 'title' => $this->t('General settings'), - ], - 'contexts' => [ - 'form' => PanelizerWizardContextForm::class, - 'title' => $this->t('Contexts'), - ], - ]; - - // Add any wizard operations from the plugin itself. - if (isset($cached_values['page_variant'])) { - $page_variant = $cached_values['page_variant']; - $variant_plugin = $page_variant->getVariantPlugin(); - $variant_plugin->setContexts($this->getContexts($cached_values)); - foreach ($variant_plugin->getWizardOperations($cached_values) as $name => $operation) { - $operation['values']['plugin'] = $variant_plugin; - $operation['submit'][] = '::submitVariantStep'; - $operations[$name] = $operation; - } - - // Change the class that manages the Content step. - if (isset($operations['content'])) { - $operations['content']['form'] = PanelizerWizardContentForm::class; - } - } - - return $operations; - } - - /** - * {@inheritdoc} - */ - public function getRouteName() { - return 'panelizer.wizard.add.step'; - } - - /** - * {@inheritdoc} - */ - public function buildForm(array $form, FormStateInterface $form_state) { - $form = parent::buildForm($form, $form_state); - $cached_values = $form_state->getTemporaryValue('wizard'); - $cached_values['id'] = $this->getMachineName(); - // Some variants like PanelsDisplayVariant need this. Set it to empty. - $cached_values['access'] = []; - if (empty($cached_values['page_variant'])) { - $variant_plugin_id = 'panels_variant'; - $page_variant = \Drupal::entityManager() - ->getStorage('page_variant') - ->create([ - '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); - return $form; - } - - /** - * {@inheritdoc} - */ - public function finish(array &$form, FormStateInterface $form_state) { - $cached_values = $form_state->getTemporaryValue('wizard'); - - $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'); - list($entity_type, $bundle, $display_id) = explode('__', $cached_values['id']); - $display = $panelizer->getEntityViewDisplay($entity_type, $bundle, $display_id); - $settings = $panelizer->getPanelizerSettings($entity_type, $bundle, $display_id); - $settings['enable'] = TRUE; - $settings['custom'] = $cached_values['custom']; - /** @var \Drupal\panelizer\Plugin\PanelizerEntityInterface $panelizer_entity_plugin */ - $panelizer_entity_plugin = $panelizer_entity_manager->createInstance($display->getTargetEntityTypeId(), []); - $displays = $display->getThirdPartySetting('panelizer', 'displays', []); - $displays[$display_id] = $panels_display_manager->exportDisplay($cached_values['plugin']); - $display->setThirdPartySetting('panelizer', 'displays', $displays); - $panelizer->setPanelizerSettings($entity_type, $bundle, $display_id, $settings, $display); - - parent::finish($form, $form_state); - $form_state->setRedirect('panelizer.wizard.edit', ['machine_name' => $cached_values['id']]); - } - - /** - * Wraps the context mapper. - * - * @return \Drupal\page_manager\ContextMapperInterface - */ - protected function getContextMapper() { - return \Drupal::service('page_manager.context_mapper'); - } - - /** - * {@inheritdoc} - */ - protected function getContexts($cached_values) { - return $this->getContextMapper()->getContextValues($cached_values['page_variant']->getStaticContexts()); - } - - /** - * Submission callback for the variant plugin steps. - */ - public function submitVariantStep(array &$form, FormStateInterface $form_state) { - $cached_values = $form_state->getTemporaryValue('wizard'); - /** @var \Drupal\page_manager\PageVariantInterface $page_variant */ - $page_variant = $cached_values['page_variant']; - /** @var \Drupal\Core\Display\VariantInterface $plugin */ - $plugin = $cached_values['plugin']; - - // Make sure the variant plugin on the page variant gets the configuration - // from the 'plugin' which should have been setup by the variant's steps. - if (!empty($plugin) && !empty($page_variant)) { - $page_variant->getVariantPlugin()->setConfiguration($plugin->getConfiguration()); - } - } - -} diff --git a/src/Wizard/PanelizerWizardBase.php b/src/Wizard/PanelizerWizardBase.php new file mode 100644 index 0000000..fe7e59a --- /dev/null +++ b/src/Wizard/PanelizerWizardBase.php @@ -0,0 +1,129 @@ +t('Wizard Information'); + } + + /** + * {@inheritdoc} + */ + public function getMachineLabel() { + return $this->t('Wizard name'); + } + + /** + * {@inheritdoc} + */ + public function getOperations($cached_values) { + $operations = [ + 'general' => [ + 'form' => PanelizerWizardGeneralForm::class, + 'title' => $this->t('General settings'), + ], + 'contexts' => [ + 'form' => PanelizerWizardContextForm::class, + 'title' => $this->t('Contexts'), + ], + ]; + + // Add any wizard operations from the plugin itself. + if (isset($cached_values['page_variant'])) { + $page_variant = $cached_values['page_variant']; + $variant_plugin = $page_variant->getVariantPlugin(); + $variant_plugin->setContexts($this->getContexts($cached_values)); + foreach ($variant_plugin->getWizardOperations($cached_values) as $name => $operation) { + $operation['values']['plugin'] = $variant_plugin; + $operation['submit'][] = '::submitVariantStep'; + $operations[$name] = $operation; + } + + // Change the class that manages the Content step. + if (isset($operations['content'])) { + $operations['content']['form'] = PanelizerWizardContentForm::class; + } + } + + return $operations; + } + + /** + * {@inheritdoc} + */ + public function finish(array &$form, FormStateInterface $form_state) { + $cached_values = $form_state->getTemporaryValue('wizard'); + + $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'); + list($entity_type, $bundle, $display_id) = explode('__', $cached_values['id']); + $display = $panelizer->getEntityViewDisplay($entity_type, $bundle, $display_id); + $settings = $panelizer->getPanelizerSettings($entity_type, $bundle, $display_id); + $settings['enable'] = $cached_values['enable']; + $settings['custom'] = $cached_values['custom']; + /** @var \Drupal\panelizer\Plugin\PanelizerEntityInterface $panelizer_entity_plugin */ + $panelizer_entity_plugin = $panelizer_entity_manager->createInstance($display->getTargetEntityTypeId(), []); + $displays = $display->getThirdPartySetting('panelizer', 'displays', []); + $displays[$display_id] = $panels_display_manager->exportDisplay($cached_values['plugin']); + $display->setThirdPartySetting('panelizer', 'displays', $displays); + $panelizer->setPanelizerSettings($entity_type, $bundle, $display_id, $settings, $display); + + parent::finish($form, $form_state); + $form_state->setRedirect('panelizer.wizard.edit', ['machine_name' => $cached_values['id']]); + } + + /** + * Wraps the context mapper. + * + * @return \Drupal\page_manager\ContextMapperInterface + */ + protected function getContextMapper() { + return \Drupal::service('page_manager.context_mapper'); + } + + /** + * {@inheritdoc} + */ + protected function getContexts($cached_values) { + return $this->getContextMapper()->getContextValues($cached_values['page_variant']->getStaticContexts()); + } + + /** + * Submission callback for the variant plugin steps. + */ + public function submitVariantStep(array &$form, FormStateInterface $form_state) { + $cached_values = $form_state->getTemporaryValue('wizard'); + /** @var \Drupal\page_manager\PageVariantInterface $page_variant */ + $page_variant = $cached_values['page_variant']; + /** @var \Drupal\Core\Display\VariantInterface $plugin */ + $plugin = $cached_values['plugin']; + + // Make sure the variant plugin on the page variant gets the configuration + // from the 'plugin' which should have been setup by the variant's steps. + if (!empty($plugin) && !empty($page_variant)) { + $page_variant->getVariantPlugin()->setConfiguration($plugin->getConfiguration()); + } + } + +}