SectionComponent example


  public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $region = NULL, $plugin_id = NULL) {
    // Only generate a new component once per form submission.     if (!$component = $form_state->get('layout_builder__component')) {
      $component = new SectionComponent($this->uuidGenerator->generate()$region['id' => $plugin_id]);
      $section_storage->getSection($delta)->appendComponent($component);
      $form_state->set('layout_builder__component', $component);
    }
    $form['#attributes']['data-layout-builder-target-highlight-id'] = $this->blockAddHighlightId($delta$region);
    return $this->doBuildForm($form$form_state$section_storage$delta$component);
  }

}
if ($is_view_configurable_non_extra_field) {
        $configuration['id'] = 'field_block:' . $this->getTargetEntityTypeId() . ':' . $this->getTargetBundle() . ':' . $name;
        $keys = array_flip(['type', 'label', 'settings', 'third_party_settings']);
        $configuration['formatter'] = array_intersect_key($options$keys);
      }
      else {
        $configuration['id'] = 'extra_field_block:' . $this->getTargetEntityTypeId() . ':' . $this->getTargetBundle() . ':' . $name;
      }

      $section = $this->getDefaultSection();
      $region = $options['region'] ?? $section->getDefaultRegion();
      $new_component = (new SectionComponent(\Drupal::service('uuid')->generate()$region$configuration));
      $section->appendComponent($new_component);
    }
    return $this;
  }

  /** * Gets a default section. * * @return \Drupal\layout_builder\Section * The default section. */
  
\Drupal::setContainer($container);

    $expected = [
      '#cache' => [
        'contexts' => [],
        'tags' => [],
        'max-age' => -1,
      ],
      '#markup' => 'block_plugin_id',
    ];

    $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
    $result = $component->toRenderArray();
    $this->assertEquals($expected$result);
  }

}
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',
          'label' => 'Test static block title',
          'label_display' => 'visible',
          'provider' => 'fake_provider',
        ]));
        $section_storage->appendSection($section);
      }

      // Node 2: Stop event propagation.       if ($entity->id() === '2') {
        $event->stopPropagation();
      }
$this->assertFalse($result);
    $result = $this->plugin->access('view', $account);
    $this->assertFalse($result);
  }

  /** * Provides test data for ::testAccess(). */
  public function providerTestAccess() {
    $section_data = [
      new Section('layout_onecol', [][
        '10000000-0000-1000-a000-000000000000' => new SectionComponent('10000000-0000-1000-a000-000000000000', 'content', ['id' => 'foo']),
      ]),
    ];

    // Data provider values are:     // - the expected outcome of the call to ::access()     // - whether Layout Builder has been enabled for this display     // - any section data     // - any permissions to grant to the user.     $data = [];
    $data['disabled, no data, no permissions'] = [
      FALSE, FALSE, [][],
    ];
FieldConfig::loadByName('entity_test', 'entity_test', OverridesSectionStorage::FIELD_NAME)
      ->setTranslatable(TRUE)
      ->save();
  }

  /** * Tests that sections on cleared when creating a new translation. */
  public function testSectionsClearedOnCreateTranslation() {
    $section_data = [
      new Section('layout_onecol', [][
        '10000000-0000-1000-a000-000000000000' => new SectionComponent('10000000-0000-1000-a000-000000000000', 'content', ['id' => 'foo']),
      ]),
    ];
    $entity = EntityTest::create([OverridesSectionStorage::FIELD_NAME => $section_data]);
    $entity->save();
    $this->assertFalse($entity->get(OverridesSectionStorage::FIELD_NAME)->isEmpty());

    $entity = EntityTest::load($entity->id());
    /** @var \Drupal\entity_test\Entity\EntityTest $translation */
    $translation = $entity->addTranslation('es', $entity->toArray());

    // Per-language layouts are not supported.
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->section = new Section(
      'layout_onecol',
      [],
      [
        new SectionComponent('existing-uuid', 'some-region', ['id' => 'existing-block-id']),
        (new SectionComponent('20000000-0000-1000-a000-000000000000', 'ordered-region', ['id' => 'second-block-id']))->setWeight(3),
        (new SectionComponent('10000000-0000-1000-a000-000000000000', 'ordered-region', ['id' => 'first-block-id']))->setWeight(2),
      ],
      [
        'bad_judgement' => ['blink_speed' => 'fast', 'spin_direction' => 'clockwise'],
        'hunt_and_peck' => ['delay' => '300ms'],
      ]
    );
  }

  /** * @covers ::__construct * @covers ::setComponent * @covers ::getComponents */


  /** * Provides test data for ::testLayoutSectionFormatter(). */
  public function providerTestLayoutSectionFormatter() {
    $data = [];
    $data['block_with_global_context'] = [
      [
        [
          'section' => new Section('layout_onecol', [][
            'baz' => new SectionComponent('baz', 'content', [
              'id' => 'test_context_aware',
              'context_mapping' => [
                'user' => '@user.current_user_context:current_user',
              ],
            ]),
          ]),
        ],
      ],
      [
        '.layout--onecol',
        '#test_context_aware--username',
      ],

class TestStateBasedSectionStorage extends SectionStorageBase {

  /** * {@inheritdoc} */
  public function getSections() {
    // Return a custom section.     $section = new Section('layout_onecol');
    $section->appendComponent(new SectionComponent('fake-uuid', 'content', [
      'id' => 'system_powered_by_block',
      'label' => 'Test block title',
      'label_display' => 'visible',
    ]));
    return [$section];
  }

  /** * {@inheritdoc} */
  public function isApplicable(RefinableCacheableDependencyInterface $cacheability) {
    
$block->build()->willReturn($block_content);
    $block->getCacheContexts()->willReturn([]);
    $block->getCacheTags()->willReturn([]);
    $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
    $block->getPluginId()->willReturn('block_plugin_id');
    $block->getBaseId()->willReturn('block_plugin_id');
    $block->getDerivativeId()->willReturn(NULL);
    $block->getConfiguration()->willReturn([]);
    $block->getPreviewFallbackString()->willReturn($placeholder_label);

    $section = [
      new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
    ];
    $expected = [
      'content' => [
        'some_uuid' => $render_array,
      ],
    ];
    $result = (new Section('layout_onecol', []$section))->toRenderArray();
    $this->assertEquals($expected$result);
  }

  /** * @covers ::toRenderArray */

  protected $sectionList;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $section_data = [
      new Section('layout_test_plugin', [][
        '10000000-0000-1000-a000-000000000000' => new SectionComponent('10000000-0000-1000-a000-000000000000', 'content', ['id' => 'foo']),
      ]),
      new Section('layout_test_plugin', ['setting_1' => 'bar'][
        '20000000-0000-1000-a000-000000000000' => new SectionComponent('20000000-0000-1000-a000-000000000000', 'content', ['id' => 'foo']),
      ]),
    ];
    $this->sectionList = $this->getSectionList($section_data);
  }

  /** * Sets up the section list. * * @param array $section_data * An array of section data. * * @return \Drupal\layout_builder\SectionListInterface * The section list. */
$block->getDerivativeId()->willReturn(NULL);
    $placeholder_label = 'Placeholder Label';
    $block->getPreviewFallbackString()->willReturn($placeholder_label);

    $block_content = [
      '#markup' => 'The block content.',
      '#cache' => ['tags' => ['build-tag']],
    ];
    $block->build()->willReturn($block_content);
    $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());

    $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
    $in_preview = FALSE;
    $event = new SectionComponentBuildRenderArrayEvent($component$contexts$in_preview);

    $subscriber = new BlockComponentRenderArray($this->account->reveal());

    $expected_build = [
      '#theme' => 'block',
      '#weight' => 0,
      '#configuration' => [],
      '#plugin_id' => 'block_plugin_id',
      '#base_plugin_id' => 'block_plugin_id',
      


  /** * Provides test data for ::testAccess(). */
  public function providerTestAccess() {
    $section_data = [
      new Section(
        'layout_onecol',
        [],
        [
          '10000000-0000-1000-a000-000000000000' => new SectionComponent('10000000-0000-1000-a000-000000000000', 'content', ['id' => 'foo']['harold' => 'maude']),
        ],
        ['layout_builder_defaults_test' => ['which_party' => 'third']]
      ),
    ];

    // Data provider values are:     // - the expected outcome of the call to ::access()     // - the operation     // - whether Layout Builder has been enabled for this display     // - whether this display has any section data.     $data = [];
    
Home | Imprint | This part of the site doesn't use cookies.