toRenderArray example


  public function toRenderArray(array $contexts = []$in_preview = FALSE) {
    $regions = [];
    foreach ($this->getComponents() as $component) {
      if ($output = $component->toRenderArray($contexts$in_preview)) {
        $regions[$component->getRegion()][$component->getUuid()] = $output;
      }
    }

    $layout = $this->getLayout($contexts);
    if ($layout instanceof PreviewAwarePluginInterface) {
      $layout->setInPreview($in_preview);
    }

    $build = $layout->build($regions);
    // If an entity was used to build the layout, store it on the build.


  /** * Tests deprecation of toRenderArray() method. * * @covers ::toRenderArray * @group legacy */
  public function testToRenderArray() {
    $this->expectDeprecation('Drupal\Core\Url::toRenderArray() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3342977');
    $url = Url::fromRoute('entity.node.canonical', ['node' => 3]);
    $this->assertIsArray($url->toRenderArray());
  }

  /** * Tests the fromRouteMatch() method. */
  public function testFromRouteMatch() {
    $route = new Route('/test-route/{foo}');
    $route_match = new RouteMatch('test_route', $route['foo' => (object) [1]]['foo' => 1]);
    $url = Url::fromRouteMatch($route_match);
    $this->assertSame('test_route', $url->getRouteName());
    $this->assertEquals(['foo' => '1']$url->getRouteParameters());
  }
    $label = new TranslatableMarkup('@entity being viewed', [
      '@entity' => $entity->getEntityType()->getSingularLabel(),
    ]);
    $contexts['layout_builder.entity'] = EntityContext::fromEntity($entity$label);

    $cacheability = new CacheableMetadata();
    $storage = $this->sectionStorageManager()->findByContext($contexts$cacheability);

    $build = [];
    if ($storage) {
      foreach ($storage->getSections() as $delta => $section) {
        $build[$delta] = $section->toRenderArray($contexts);
      }
    }
    // The render array is built based on decisions made by @SectionStorage     // plugins and therefore it needs to depend on the accumulated     // cacheability of those decisions.     $cacheability->applyTo($build);
    return $build;
  }

  /** * Gets the available contexts for a given entity. * * @param \Drupal\Core\Entity\FieldableEntityInterface $entity * The entity. * * @return \Drupal\Core\Plugin\Context\ContextInterface[] * An array of context objects for a given entity. */
$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 */
  public function testToRenderArrayAccessDenied() {
    $block = $this->prophesize(BlockPluginInterface::class);
    $this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());

    $access_result = AccessResult::forbidden();
    
$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);
  }

}

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

    $layout = $section->getLayout($this->getPopulatedContexts($section_storage));
    $layout_settings = $section->getLayoutSettings();
    $section_label = !empty($layout_settings['label']) ? $layout_settings['label'] : $this->t('Section @section', ['@section' => $delta + 1]);

    $build = $section->toRenderArray($this->getPopulatedContexts($section_storage), TRUE);
    $layout_definition = $layout->getPluginDefinition();

    $region_labels = $layout_definition->getRegionLabels();
    foreach ($layout_definition->getRegions() as $region => $info) {
      if (!empty($build[$region])) {
        foreach (Element::children($build[$region]) as $uuid) {
          $build[$region][$uuid]['#attributes']['class'][] = 'js-layout-builder-block';
          $build[$region][$uuid]['#attributes']['class'][] = 'layout-builder-block';
          $build[$region][$uuid]['#attributes']['data-layout-block-uuid'] = $uuid;
          $build[$region][$uuid]['#attributes']['data-layout-builder-highlight-id'] = $this->blockUpdateHighlightId($uuid);
          $build[$region][$uuid]['#contextual_links'] = [
            
Home | Imprint | This part of the site doesn't use cookies.