getSectionStorage example

    $events[LayoutBuilderEvents::PREPARE_LAYOUT][] = ['onAfterPrepareLayout', -10];
    return $events;
  }

  /** * Subscriber to test acting before the LB subscriber. * * @param \Drupal\layout_builder\Event\PrepareLayoutEvent $event * The prepare layout event. */
  public function onBeforePrepareLayout(PrepareLayoutEvent $event) {
    $section_storage = $event->getSectionStorage();
    $context = $section_storage->getContextValues();

    if (!empty($context['entity'])) {
      /** @var \Drupal\Core\Entity\EntityInterface $entity */
      $entity = $context['entity'];

      // Node 1 or 2: Append a block to the layout.       if (in_array($entity->id()['1', '2'])) {
        $section = new Section('layout_onecol');
        $section->appendComponent(new SectionComponent('fake-uuid', 'content', [
          'id' => 'static_block',
          
$events[LayoutBuilderEvents::PREPARE_LAYOUT][] = ['onPrepareLayout', 10];
    return $events;
  }

  /** * Prepares a layout for use in the UI. * * @param \Drupal\layout_builder\Event\PrepareLayoutEvent $event * The prepare layout event. */
  public function onPrepareLayout(PrepareLayoutEvent $event) {
    $section_storage = $event->getSectionStorage();

    // If the layout has pending changes, add a warning.     if ($this->layoutTempstoreRepository->has($section_storage)) {
      $this->messenger->addWarning($this->t('You have unsaved changes.'));
    }
    else {
      // If the layout is an override that has not yet been overridden, copy the       // sections from the corresponding default.       if ($section_storage instanceof OverridesSectionStorageInterface && !$section_storage->isOverridden()) {
        $sections = $section_storage->getDefaultSectionStorage()->getSections();
        foreach ($sections as $section) {
          

class LayoutBuilderWidget extends WidgetBase {

  /** * {@inheritdoc} */
  public function formElement(FieldItemListInterface $items$delta, array $element, array &$form, FormStateInterface $form_state) {
    $element += [
      '#type' => 'layout_builder',
      '#section_storage' => $this->getSectionStorage($form_state),
    ];
    $element['#process'][] = [static::class, 'layoutBuilderElementGetKeys'];
    return $element;
  }

  /** * Form element #process callback. * * Save the layout builder element array parents as a property on the top form * element so that they can be used to access the element within the whole * render array later. * * @see \Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController */
Home | Imprint | This part of the site doesn't use cookies.