/** * {@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; }
/** * Updates the current Panels display based on the changes done in our app. * * @param \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display * The current Panels display. * @param array $layout_model * The decoded LayoutModel from our App. * * @return \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant */ private static function updatePanelsDisplay(PanelsDisplayVariant $panels_display, array $layout_model) { // Set our weight and region based on the metadata in our Backbone app. foreach ($layout_model['regionCollection'] as $region) { $weight = 0; foreach ($region['blockCollection'] as $block) { /** @var \Drupal\Core\Block\BlockBase $block_instance */ $block_instance = $panels_display->getBlock($block['uuid']); $block_instance->setConfigurationValue('region', $region['name']); $block_instance->setConfigurationValue('weight', ++$weight); $panels_display->updateBlock($block['uuid'], $block_instance->getConfiguration()); } } return $panels_display; }
/** * Updates the current Panels display based on the changes done in our app. * * @param \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $panels_display * The current Panels display. * @param array $layout_model * The decoded LayoutModel from our App. * * @return \Symfony\Component\HttpFoundation\JsonResponse */ protected function updatePanelsDisplay(PanelsDisplayVariant $panels_display, array $layout_model) { // Set our weight and region based on the metadata in our Backbone app. foreach ($layout_model['regionCollection'] as $region) { $weight = 0; foreach ($region['blockCollection'] as $block) { /** @var \Drupal\Core\Block\BlockBase $block_instance */ $block_instance = $panels_display->getBlock($block['uuid']); $block_instance->setConfigurationValue('region', $region['name']); $block_instance->setConfigurationValue('weight', ++$weight); $panels_display->updateBlock($block['uuid'], $block_instance->getConfiguration()); } } // Remove blocks that need removing. // @todo We should do this on the fly instead of at on save. foreach ($layout_model['deletedBlocks'] as $uuid) { $panels_display->removeBlock($uuid); } // Save the variant and remove temp storage. $this->savePanelsDisplay($panels_display, FALSE); return new JsonResponse(['deletedBlocks' => []]); }
/** * {@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; }