getContexts example


  public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
    $result = AccessResult::allowed();
    return $return_as_object ? $result : $result->isAllowed();
  }

  /** * {@inheritdoc} */
  public function getContextsDuringPreview() {
    return $this->getContexts();
  }

  /** * {@inheritdoc} */
  public function isApplicable(RefinableCacheableDependencyInterface $cacheability) {
    return TRUE;
  }

}


    private function getPriceContexts(Shop $shop): array
    {
        $currencies = $this->identifierSelector->getShopCurrencyIds($shop->getId());
        if (!$shop->isMain()) {
            $currencies = $this->identifierSelector->getShopCurrencyIds($shop->getParentId());
        }

        $customerGroups = $this->identifierSelector->getCustomerGroupKeys();

        return $this->getContexts($shop->getId()$customerGroups$currencies);
    }

    private function getCustomerGroupContexts(Shop $shop): array
    {
        $customerGroups = $this->identifierSelector->getCustomerGroupKeys();

        return $this->getContexts($shop->getId()$customerGroups[$shop->getCurrency()->getId()]);
    }

    /** * @param int $shopId * @param string[] $customerGroups * @param int[] $currencies */


  /** * {@inheritdoc} */
  public function validateContexts() {
    $violations = new ConstraintViolationList();

    // @todo Implement the Symfony Validator component to let the validator     // traverse and set property paths accordingly.     // See https://www.drupal.org/project/drupal/issues/3153847.     foreach ($this->getContexts() as $context) {
      $violations->addAll($context->validate());
    }
    return $violations;
  }

  /** * {@inheritdoc} */
  public function getCacheContexts() {
    $cache_contexts = [];
    // Applied contexts can affect the cache contexts when this plugin is

  public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
    $block = $event->getPlugin();
    if (!$block instanceof BlockPluginInterface) {
      return;
    }

    // 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);
          }
        }
      }
    }
$display = LayoutBuilderEntityViewDisplay::create([
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'mode' => 'default',
      'status' => TRUE,
    ]);
    $display->save();

    $context = EntityContext::fromEntity($display);
    $this->plugin->setContext('display', $context);

    $result = $this->plugin->getContexts();
    $this->assertSame(['view_mode', 'display']array_keys($result));
    $this->assertSame($context$result['display']);
  }

  /** * @covers ::getContextsDuringPreview */
  public function testGetContextsDuringPreview() {
    $display = LayoutBuilderEntityViewDisplay::create([
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      
// Test Complex compound context handling.     $complex_plugin = $manager->createInstance('complex_context');
    $complex_plugin->setContextValue('user', $user);

    // With only the user context set, try to get the context values.     $values = $complex_plugin->getContextValues();
    $this->assertNull($values['node'], 'The node context is not yet set.');
    $this->assertNotNull($values['user'], 'The user context is set');

    $complex_plugin->setContextValue('node', $node);
    $context_wrappers = $complex_plugin->getContexts();
    // Make sure what came out of the wrappers is good.     $this->assertEquals($user->label()$context_wrappers['user']->getContextValue()->label());
    $this->assertEquals($node->label()$context_wrappers['node']->getContextValue()->label());

    // Make sure what comes out of the context values is good.     $contexts = $complex_plugin->getContextValues();
    $this->assertEquals($user->label()$contexts['user']->label());
    $this->assertEquals($node->label()$contexts['node']->label());

    // Test the title method for the complex context plugin.     $this->assertEquals($user->label() . ' -- ' . $node->label()$complex_plugin->getTitle());
  }


  /** * {@inheritdoc} */
  public function build() {
    $config = $this->getConfiguration();
    if (empty($config['required_configuration'])) {
      throw new \Exception('Required configuration is missing!');
    }

    $contexts = $this->getContexts();
    if (!isset($contexts['context'])) {
      throw new \Exception('Required context is missing!');
    }

    $build = [];
    $build['content']['default'] = [
      '#markup' => $config['required_configuration'] . ' ' . $contexts['context']->getContextValue(),
    ];

    CacheableMetadata::createFromObject($this)->applyTo($build);

    

  public function removeAllSections($set_blank = FALSE) {
    $this->getSectionList()->removeAllSections($set_blank);
    return $this;
  }

  /** * {@inheritdoc} */
  public function getContextsDuringPreview() {
    $contexts = $this->getContexts();

    // view_mode is a required context, but SectionStorage plugins are not     // required to return it (for example, the layout_library plugin provided     // in the Layout Library module. In these instances, explicitly create a     // view_mode context with the value "default".     if (!isset($contexts['view_mode']) || $contexts['view_mode']->validate()->count() || !$contexts['view_mode']->getContextValue()) {
      $contexts['view_mode'] = new Context(new ContextDefinition('string'), 'default');
    }
    return $contexts;
  }

  
$currencies = $this->identifierSelector->getShopCurrencyIds($shop->getParentId());
        }

        $defaultCurrencyId = $shop->getCurrency()->getId();

        if (!\in_array($defaultCurrencyId$currencies, true)) {
            $currencies[] = $defaultCurrencyId;
        }

        $customerGroups = $this->identifierSelector->getCustomerGroupKeys();

        return $this->getContexts($shop->getId()$customerGroups$currencies);
    }

    /** * @param int[] $expandGroupIds * @param Group[] $configurations * @param Group[] $fullConfiguration */
    private function buildFilterConfiguration(array $expandGroupIds, array $configurations, array $fullConfiguration): array
    {
        $merged = [];
        foreach ($configurations as $config) {
            
public function testGetContexts() {
    $entity = EntityTest::create();
    $entity->save();

    $context = EntityContext::fromEntity($entity);
    $this->plugin->setContext('entity', $context);

    $expected = [
      'entity',
      'view_mode',
    ];
    $result = $this->plugin->getContexts();
    $this->assertEquals($expectedarray_keys($result));
    $this->assertSame($context$result['entity']);
  }

  /** * @covers ::getContextsDuringPreview */
  public function testGetContextsDuringPreview() {
    $entity = EntityTest::create();
    $entity->save();

    

      $page_display
        ->setMainContent($main_content)
        ->setTitle($title)
        ->addCacheableDependency($event);
      // Some display variants need to be passed an array of contexts with       // values because they can't get all their contexts globally. For example,       // in Page Manager, you can create a Page which has a specific static       // context (e.g. a context that refers to the Node with nid 6), if any       // such contexts were added to the $event, pass them to the $page_display.       if ($page_display instanceof ContextAwareVariantInterface) {
        $page_display->setContexts($event->getContexts());
      }

      // Generate a #type => page render array using the page display variant,       // the page display will build the content for the various page regions.       $page = [
        '#type' => 'page',
      ];
      $page += $page_display->build();
    }

    // $page is now fully built. Find all non-empty page regions, and add a
Home | Imprint | This part of the site doesn't use cookies.