/** * Act on a Panels Display before it is saved via the IPE. * * @param \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display * The current Panels display. * @param array $layout_model * The decoded LayoutModel from our App. */ function hook_panels_ipe_panels_display_presave(PanelsDisplayVariant $panels_display, array $layout_model) { if (isset($layout_model['use_custom_storage'])) { $configuration = $panels_display->getConfiguration(); $panels_display->setStorage('custom_storage_key', $configuration['storage_id']); } }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $block_instance = $this->getBlockInstance($form_state); // Submit the block configuration form. $block_form_state = (new FormState())->setValues($form_state->getValue('settings')); $block_instance->submitConfigurationForm($form, $block_form_state); // Update the original form values. $form_state->setValue('settings', $block_form_state->getValues()); // If a temporary configuration for this variant exists, use it. $temp_store_key = $this->panelsDisplay->id(); if ($variant_config = $this->tempStore->get($temp_store_key)) { $this->panelsDisplay->setConfiguration($variant_config); } // Set the block region appropriately. $block_config = $block_instance->getConfiguration(); $block_config['region'] = $form_state->getValue(array('settings', 'region')); // Determine if we need to update or add this block. if ($uuid = $form_state->getValue('uuid')) { $this->panelsDisplay->updateBlock($uuid, $block_config); } else { $uuid = $this->panelsDisplay->addBlock($block_config); } // Set the tempstore value. $this->tempStore->set($this->panelsDisplay->id(), $this->panelsDisplay->getConfiguration()); // Assemble data required for our App. $build = $this->buildBlockInstance($block_instance); $form['build'] = $build; // Add our data attribute for the Backbone app. $build['#attributes']['data-block-id'] = $uuid; $block_model = ['uuid' => $uuid, 'label' => $block_instance->label(), 'id' => $block_instance->getPluginId(), 'region' => $block_config['region'], 'html' => $this->renderer->render($build)]; // Add Block metadata and HTML as a drupalSetting. $form['#attached']['drupalSettings']['panels_ipe']['updated_block'] = $block_model; return $form; }
/** * {@inheritdoc} */ public function save(PanelsDisplayVariant $panels_display) { $id = $panels_display->getStorageId(); if ($id && ($page_variant = $this->loadPageVariant($id))) { $variant_plugin = $page_variant->getVariantPlugin(); if (!$variant_plugin instanceof PanelsDisplayVariant) { throw new \Exception("Page variant doesn't use a Panels display variant"); } $variant_plugin->setConfiguration($panels_display->getConfiguration()); $page_variant->save(); } else { throw new \Exception("Couldn't find page variant to store Panels display"); } }
/** * Saves the current Panels display in the tempstore or real storage.. * * @param \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display * The Panels display to be saved. * @param bool $temp * Whether or not to save to temp store. * * @return \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant * The Panels display that was saved. * * @throws \Drupal\user\TempStoreException * If there are any issues manipulating the entry in the temp store. */ protected function savePanelsDisplay(PanelsDisplayVariant $panels_display, $temp = TRUE) { $temp_store_key = $panels_display->id(); // Save configuration to temp store. if ($temp) { $this->tempStore->set($temp_store_key, $panels_display->getConfiguration()); } else { // Check to see if temp store has configuration saved. if ($variant_config = $this->tempStore->get($temp_store_key)) { // Delete the existing temp store value. $this->tempStore->delete($temp_store_key); } // Save to the real storage. $this->panelsStorage->save($panels_display); } return $panels_display; }
/** * {@inheritdoc} */ public function exportDisplay(PanelsDisplayVariant $display) { return $display->getConfiguration(); }
/** * Saves the given Panels Display to TempStore. * * @param \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display * @throws \Drupal\user\TempStoreException */ protected function savePanelsDisplayToTempStore(PanelsDisplayVariant $panels_display) { $this->tempStore->set($panels_display->id(), $panels_display->getConfiguration()); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Return early if there are any errors. if ($form_state->hasAnyErrors()) { return $form; } $block_instance = $this->getBlockInstance($form_state); // Submit the block configuration form. $this->submitBlock($block_instance, $form, $form_state); // If a temporary configuration for this variant exists, use it. $temp_store_key = $this->panelsDisplay->id(); if ($variant_config = $this->tempStore->get($temp_store_key)) { $this->panelsDisplay->setConfiguration($variant_config); } // Set the block region appropriately. $block_config = $block_instance->getConfiguration(); $block_config['region'] = $form_state->getValue(array('settings', 'region')); // Determine if we need to update or add this block. if ($uuid = $form_state->getValue('uuid')) { $this->panelsDisplay->updateBlock($uuid, $block_config); } else { $uuid = $this->panelsDisplay->addBlock($block_config); } // Set the tempstore value. $this->tempStore->set($this->panelsDisplay->id(), $this->panelsDisplay->getConfiguration()); // Assemble data required for our App. $build = $this->buildBlockInstance($block_instance); // Bubble block attributes up if possible. This allows modules like // Quickedit to function. // See \Drupal\block\BlockViewBuilder::preRender() for reference. if ($build['content'] !== NULL && !Element::isEmpty($build['content'])) { foreach (['#attributes', '#contextual_links'] as $property) { if (isset($build['content'][$property])) { $build[$property] += $build['content'][$property]; unset($build['content'][$property]); } } } // Add our data attribute for the Backbone app. $build['#attributes']['data-block-id'] = $uuid; $block_model = ['uuid' => $uuid, 'label' => $block_instance->label(), 'id' => $block_instance->getPluginId(), 'region' => $block_config['region'], 'html' => $this->renderer->render($build)]; $form['build'] = $build; // Add Block metadata and HTML as a drupalSetting. $form['#attached']['drupalSettings']['panels_ipe']['updated_block'] = $block_model; return $form; }