/** * @covers ::get */ public function testGet() { $this->keyValue->expects($this->at(0))->method('get')->with('test_2')->will($this->returnValue(FALSE)); $this->keyValue->expects($this->at(1))->method('get')->with('test')->will($this->returnValue($this->ownObject)); $this->assertNull($this->tempStore->get('test_2')); $this->assertSame($this->ownObject->data, $this->tempStore->get('test')); }
/** * {@inheritdoc} * * @param string $entity_type_id * The entity type id. */ public function buildForm(array $form, FormStateInterface $form_state, $entity_type_id = NULL) { $this->entityTypeId = $entity_type_id; $this->selection = $this->tempStore->get($this->currentUser->id()); if (empty($this->entityTypeId) || empty($this->selection)) { return new RedirectResponse($this->getCancelUrl()->setAbsolute()->toString()); } $storage = $this->entityTypeManager->getStorage($this->entityTypeId); /** @var \Drupal\Core\Entity\ContentEntityInterface[] $entities */ $entities = $storage->loadMultiple(array_keys($this->selection)); $items = []; foreach ($this->selection as $id => $langcodes) { foreach ($langcodes as $langcode) { $entity = $entities[$id]->getTranslation($langcode); $key = $id . ':' . $langcode; $default_key = $id . ':' . $entity->getUntranslated()->language()->getId(); // If we have a translated entity we build a nested list of translations // that will be deleted. $languages = $entity->getTranslationLanguages(); if (count($languages) > 1 && $entity->isDefaultTranslation()) { $names = []; foreach ($languages as $translation_langcode => $language) { $names[] = $language->getName(); unset($items[$id . ':' . $translation_langcode]); } $items[$default_key] = ['label' => ['#markup' => $this->t('@label (Original translation) - <em>The following translations will be deleted:</em>', ['@label' => $entity->label()])], 'deleted_translations' => ['#theme' => 'item_list', '#items' => $names]]; } elseif (!isset($items[$default_key])) { $items[$key] = $entity->label(); } } } $form['entities'] = ['#theme' => 'item_list', '#items' => $items]; $form = parent::buildForm($form, $form_state); return $form; }
/** * Loads or creates a Block Plugin instance suitable for rendering or testing. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @return \Drupal\Core\Block\BlockBase * The Block Plugin instance. */ protected function getBlockInstance($form_state) { /** @var \Drupal\page_manager\PageVariantInterface $page_variant */ $page_variant = PageVariant::load($form_state->getValue('variant_id')); // If a UUID is provided, the Block should already exist. if ($uuid = $form_state->getValue('uuid')) { /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $variant_plugin */ $variant_plugin = $page_variant->getVariantPlugin(); // If a temporary configuration for this variant exists, use it. $temp_store_key = 'variant.' . $page_variant->id(); if ($variant_config = $this->tempStore->get($temp_store_key)) { $variant_plugin->setConfiguration($variant_config); } // Load the existing Block instance. $block_instance = $variant_plugin->getBlock($uuid); } else { // Create an instance of this Block plugin. /** @var \Drupal\Core\Block\BlockBase $block_instance */ $block_instance = $this->blockManager->createInstance($form_state->getValue('plugin_id')); } // Add context to the block. if ($block_instance instanceof ContextAwarePluginInterface) { $this->contextHandler->applyContextMapping($block_instance, $page_variant->getContexts()); } return $block_instance; }
/** * Removes any temporary changes to the variant. * * @param \Drupal\page_manager\PageVariantInterface $page_variant * The current Page Variant. * * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function cancelPageVariant(PageVariantInterface $page_variant) { // If a temporary configuration for this variant exists, use it. $temp_store_key = 'variant.' . $page_variant->id(); if ($variant_config = $this->tempStore->get($temp_store_key)) { $this->tempStore->delete($temp_store_key); } // Return an empty JSON response. return new JsonResponse(); }
/** * Removes any temporary changes to the variant. * * @param string $panels_display_id * The id of the current Panels display. * @param \Symfony\Component\HttpFoundation\Request $request * The current request. * * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function cancel($panels_storage_type, $panels_storage_id) { $panels_display = $this->loadPanelsDisplay($panels_storage_type, $panels_storage_id); // If a temporary configuration for this variant exists, use it. $temp_store_key = $panels_display->id(); if ($variant_config = $this->tempStore->get($temp_store_key)) { $this->tempStore->delete($temp_store_key); } // Return an empty JSON response. return new JsonResponse(); }
/** * {@inheritdoc} */ public function build(array $regions, array $contexts, LayoutInterface $layout = NULL) { // Load our PageVariant. /** @var \Drupal\page_manager\PageVariantInterface $page_variant */ $page_variant = \Drupal::request()->attributes->get('page_manager_page_variant'); // Check to see if the current user has permissions to use the IPE. $has_permission = $this->account->hasPermission('access panels in-place editing'); // Attach the Panels In-place editor library based on permissions. if ($page_variant && $has_permission) { // This flag tracks whether or not there are unsaved changes. $unsaved = FALSE; // If a temporary configuration for this variant exists, use it. $temp_store_key = 'variant.' . $page_variant->id(); if ($variant_config = $this->tempStore->get($temp_store_key)) { // Reload the PageVariant. This is required to set variant plugin // configuration correctly. $page_variant = PageVariant::load($page_variant->id()); /** @var \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant $variant_plugin */ $variant_plugin = $page_variant->getVariantPlugin(); $variant_plugin->setConfiguration($variant_config); // Override our initial values with what's in TempStore. $layout = $variant_plugin->getLayout(); $regions = $variant_plugin->getRegionAssignments(); // Indicate that the user is viewing un-saved changes. $unsaved = TRUE; } $build = parent::build($regions, $contexts, $layout); foreach ($regions as $region => $blocks) { // Wrap each region with a unique class and data attribute. $region_name = Html::getClass("block-region-{$region}"); $build[$region]['#prefix'] = '<div class="' . $region_name . '" data-region-name="' . $region . '">'; $build[$region]['#suffix'] = '</div>'; if ($blocks) { foreach ($blocks as $block_id => $block) { $build[$region][$block_id]['#attributes']['data-block-id'] = $block_id; } } } // Attach the required settings and IPE. $build['#attached']['library'][] = 'panels_ipe/panels_ipe'; $build['#attached']['drupalSettings']['panels_ipe'] = $this->getDrupalSettings($regions, $layout, $page_variant); // Add our custom elements to the build. $build['#prefix'] = '<div id="panels-ipe-content">'; // Indicate if the current user is viewing temp store. $tray_classes = $unsaved ? 'unsaved' : ''; $build['#suffix'] = '</div><div id="panels-ipe-tray" class="' . $tray_classes . '"></div>'; } else { $build = parent::build($regions, $contexts, $layout); } return $build; }
/** * Loads or creates a Block Plugin instance suitable for rendering or testing. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @return \Drupal\Core\Block\BlockPluginInterface * The Block Plugin instance. */ protected function getBlockInstance(FormStateInterface $form_state) { // If a UUID is provided, the Block should already exist. if ($uuid = $form_state->getValue('uuid')) { // 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); } // Load the existing Block instance. $block_instance = $this->panelsDisplay->getBlock($uuid); } else { // Create an instance of this Block plugin. /** @var \Drupal\Core\Block\BlockBase $block_instance */ $block_instance = $this->blockManager->createInstance($form_state->getValue('plugin_id')); } return $block_instance; }
/** * {@inheritdoc} */ public function build(PanelsDisplayVariant $panels_display) { // Check to see if the current user has permissions to use the IPE. $has_permission = $this->account->hasPermission('access panels in-place editing'); // Attach the Panels In-place editor library based on permissions. if ($has_permission) { // This flag tracks whether or not there are unsaved changes. $unsaved = FALSE; // If a temporary configuration for this variant exists, use it. $temp_store_key = $panels_display->id(); if ($variant_config = $this->tempStore->get($temp_store_key)) { unset($variant_config['id']); $panels_display->setConfiguration($variant_config); // Indicate that the user is viewing un-saved changes. $unsaved = TRUE; } $build = parent::build($panels_display); $regions = $panels_display->getRegionAssignments(); $layout = $panels_display->getLayout(); foreach ($regions as $region => $blocks) { // Wrap each region with a unique class and data attribute. $region_name = Html::getClass("block-region-{$region}"); $build[$region]['#prefix'] = '<div class="' . $region_name . '" data-region-name="' . $region . '">'; $build[$region]['#suffix'] = '</div>'; if ($blocks) { foreach ($blocks as $block_id => $block) { $build[$region][$block_id]['#attributes']['data-block-id'] = $block_id; } } } // Attach the required settings and IPE. $build['#attached']['library'][] = 'panels_ipe/panels_ipe'; $build['#attached']['drupalSettings']['panels_ipe'] = $this->getDrupalSettings($regions, $layout, $panels_display, $unsaved); // Add our custom elements to the build. $build['#prefix'] = '<div id="panels-ipe-content">'; $build['#suffix'] = '</div><div id="panels-ipe-tray"></div>'; } else { $build = parent::build($panels_display); } return $build; }