getStorageType example


  protected function buildAddSectionLink(SectionStorageInterface $section_storage$delta) {
    $storage_type = $section_storage->getStorageType();
    $storage_id = $section_storage->getStorageId();

    // If the delta and the count are the same, it is either the end of the     // layout or an empty layout.     if ($delta === count($section_storage)) {
      if ($delta === 0) {
        $title = $this->t('Add section');
      }
      else {
        $title = $this->t('Add section <span class="visually-hidden">at end of layout</span>');
      }
    }
/** * {@inheritdoc} */
  public function label() {
    return $this->getStorageId();
  }

  /** * Returns the name to be used to store in the config system. */
  protected function getConfigName() {
    return 'layout_builder_test.' . $this->getStorageType() . '.' . $this->getStorageId();
  }

  /** * {@inheritdoc} */
  public function getSections() {
    if (is_null($this->sections)) {
      $sections = $this->configFactory->get($this->getConfigName())->get('sections') ?: [];
      $this->setSections(array_map([Section::class, 'fromArray']$sections));
    }
    return $this->sections;
  }
/** * {@inheritdoc} */
  public function getRedirectUrl() {
    return Url::fromRoute("entity.entity_view_display.{$this->getDisplay()->getTargetEntityTypeId()}.view_mode", $this->getRouteParameters());
  }

  /** * {@inheritdoc} */
  public function getLayoutBuilderUrl($rel = 'view') {
    return Url::fromRoute("layout_builder.{$this->getStorageType()}.{$this->getDisplay()->getTargetEntityTypeId()}.$rel", $this->getRouteParameters());
  }

  /** * Provides the route parameters needed to generate a URL for this object. * * @return mixed[] * An associative array of parameter names and values. */
  protected function getRouteParameters() {
    $display = $this->getDisplay();
    $entity_type = $this->entityTypeManager->getDefinition($display->getTargetEntityTypeId());
    

  public function getRedirectUrl() {
    return $this->getEntity()->toUrl('canonical');
  }

  /** * {@inheritdoc} */
  public function getLayoutBuilderUrl($rel = 'view') {
    $entity = $this->getEntity();
    $route_parameters[$entity->getEntityTypeId()] = $entity->id();
    return Url::fromRoute("layout_builder.{$this->getStorageType()}.{$this->getEntity()->getEntityTypeId()}.$rel", $route_parameters);
  }

  /** * {@inheritdoc} */
  public function getContextsDuringPreview() {
    $contexts = parent::getContextsDuringPreview();

    // @todo Remove this in https://www.drupal.org/node/3018782.     if (isset($contexts['entity'])) {
      $contexts['layout_builder.entity'] = $contexts['entity'];
      
/** * Provides a title callback. * * @param \Drupal\layout_builder\SectionStorageInterface $section_storage * The section storage. * * @return string * The title for the layout page. */
  public function title(SectionStorageInterface $section_storage) {
    assert(Inspector::assertStringable($section_storage->label()), 'Section storage label is expected to be a string.');
    return $this->t('Edit layout for %label', ['%label' => $section_storage->label() ?? $section_storage->getStorageType() . ' ' . $section_storage->getStorageId()]);
  }

  /** * Renders the Layout UI. * * @param \Drupal\layout_builder\SectionStorageInterface $section_storage * The section storage. * * @return array * A render array. */
  
public function __construct(SharedTempStoreFactory $temp_store_factory) {
    $this->tempStoreFactory = $temp_store_factory;
  }

  /** * {@inheritdoc} */
  public function get(SectionStorageInterface $section_storage) {
    $key = $this->getKey($section_storage);
    $tempstore = $this->getTempstore($section_storage)->get($key);
    if (!empty($tempstore['section_storage'])) {
      $storage_type = $section_storage->getStorageType();
      $section_storage = $tempstore['section_storage'];

      if (!($section_storage instanceof SectionStorageInterface)) {
        throw new \UnexpectedValueException(sprintf('The entry with storage type "%s" and ID "%s" is invalid', $storage_type$key));
      }
    }
    return $section_storage;
  }

  /** * {@inheritdoc} */
'#type' => 'link',
        '#title' => [
          'icon' => $definition->getIcon(60, 80, 1, 3),
          'label' => [
            '#type' => 'container',
            '#children' => $definition->getLabel(),
          ],
        ],
        '#url' => Url::fromRoute(
          $layout instanceof PluginFormInterface ? 'layout_builder.configure_section' : 'layout_builder.add_section',
          [
            'section_storage_type' => $section_storage->getStorageType(),
            'section_storage' => $section_storage->getStorageId(),
            'delta' => $delta,
            'plugin_id' => $plugin_id,
          ]
        ),
      ];
      if ($this->isAjax()) {
        $item['#attributes']['class'][] = 'use-ajax';
        $item['#attributes']['data-dialog-type'][] = 'dialog';
        $item['#attributes']['data-dialog-renderer'][] = 'off_canvas';
      }
      

  public function getCancelUrl() {
    return $this->sectionStorage->getRedirectUrl();
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
    if (!$section_storage instanceof DefaultsSectionStorageInterface) {
      throw new \InvalidArgumentException(sprintf('The section storage with type "%s" and ID "%s" does not provide defaults', $section_storage->getStorageType()$section_storage->getStorageId()));
    }

    $this->sectionStorage = $section_storage;
    // Mark this as an administrative page for JavaScript ("Back to site" link).     $form['#attached']['drupalSettings']['path']['currentPathIsAdmin'] = TRUE;
    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  

class LayoutTempstoreRepositoryTest extends UnitTestCase {

  /** * @covers ::get * @covers ::has */
  public function testGetEmptyTempstore() {
    $section_storage = $this->prophesize(SectionStorageInterface::class);
    $section_storage->getStorageType()->willReturn('my_storage_type');
    $section_storage->getStorageId()->willReturn('my_storage_id');

    $tempstore = $this->prophesize(SharedTempStore::class);
    $tempstore->get('my_storage_id')->shouldBeCalled();

    $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class);
    $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal());

    $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());

    $this->assertFalse($repository->has($section_storage->reveal()));

    

  public function build(SectionStorageInterface $section_storage, int $delta$region) {
    if ($this->entityTypeManager->hasDefinition('block_content_type') && $types = $this->entityTypeManager->getStorage('block_content_type')->loadMultiple()) {
      if (count($types) === 1) {
        $type = reset($types);
        $plugin_id = 'inline_block:' . $type->id();
        if ($this->blockManager->hasDefinition($plugin_id)) {
          $url = Url::fromRoute('layout_builder.add_block', [
            'section_storage_type' => $section_storage->getStorageType(),
            'section_storage' => $section_storage->getStorageId(),
            'delta' => $delta,
            'region' => $region,
            'plugin_id' => $plugin_id,
          ]);
        }
      }
      else {
        $url = Url::fromRoute('layout_builder.choose_inline_block', [
          'section_storage_type' => $section_storage->getStorageType(),
          'section_storage' => $section_storage->getStorageId(),
          

  public function getCancelUrl() {
    return $this->sectionStorage->getLayoutBuilderUrl();
  }

  /** * {@inheritdoc} */
  public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL) {
    if (!$section_storage instanceof OverridesSectionStorageInterface) {
      throw new \InvalidArgumentException(sprintf('The section storage with type "%s" and ID "%s" does not provide overrides', $section_storage->getStorageType()$section_storage->getStorageId()));
    }

    $this->sectionStorage = $section_storage;
    // Mark this as an administrative page for JavaScript ("Back to site" link).     $form['#attached']['drupalSettings']['path']['currentPathIsAdmin'] = TRUE;
    return parent::buildForm($form$form_state);
  }

  /** * {@inheritdoc} */
  
Home | Imprint | This part of the site doesn't use cookies.