LayoutPreviewAccessAllowed example



  /** * @covers ::onBuildRender * * @dataProvider providerBlockTypes */
  public function testOnBuildRenderInPreview($refinable_dependent_access) {
    $contexts = [];
    if ($refinable_dependent_access) {
      $block = $this->prophesize(TestBlockPluginWithRefinableDependentAccessInterface::class)->willImplement(PreviewFallbackInterface::class);
      $block->setAccessDependency(new LayoutPreviewAccessAllowed())->shouldBeCalled();

      $layout_entity = $this->prophesize(EntityInterface::class);
      $layout_entity = $layout_entity->reveal();
      $layout_entity->in_preview = TRUE;
      $context = $this->prophesize(ContextInterface::class);
      $context->getContextValue()->willReturn($layout_entity);
      $contexts['layout_builder.entity'] = $context->reveal();
    }
    else {
      $block = $this->prophesize(BlockPluginInterface::class)->willImplement(PreviewFallbackInterface::class);
    }

    


    // Set block access dependency even if we are not checking access on     // this level. The block itself may render another     // RefinableDependentAccessInterface object and need to pass on this value.     if ($block instanceof RefinableDependentAccessInterface) {
      $contexts = $event->getContexts();
      if (isset($contexts['layout_builder.entity'])) {
        if ($entity = $contexts['layout_builder.entity']->getContextValue()) {
          if ($event->inPreview()) {
            // If previewing in Layout Builder allow access.             $block->setAccessDependency(new LayoutPreviewAccessAllowed());
          }
          else {
            $block->setAccessDependency($entity);
          }
        }
      }
    }

    // Only check access if the component is not being previewed.     if ($event->inPreview()) {
      $access = AccessResult::allowed()->setCacheMaxAge(0);
    }
Home | Imprint | This part of the site doesn't use cookies.