hasContextValue example


  protected function getPopulatedContexts(SectionStorageInterface $section_storage): array {
    // Get all known globally available contexts IDs.     $available_context_ids = array_keys($this->contextRepository()->getAvailableContexts());
    // Filter to those that are populated.     $contexts = array_filter($this->contextRepository()->getRuntimeContexts($available_context_ids)function DContextInterface $context) {
      return $context->hasContextValue();
    });

    // Add in the per-section_storage contexts.     $contexts += $section_storage->getContextsDuringPreview();
    return $contexts;
  }

}
// Test an authenticated account.     $authenticated = User::create([
      'name' => $this->randomMachineName(),
    ]);
    $authenticated->save();
    $authenticated = User::load($authenticated->id());
    $this->container->get('current_user')->setAccount($authenticated);

    $contexts = $context_repository->getAvailableContexts();
    $this->assertArrayHasKey('@user.current_user_context:current_user', $contexts);
    $this->assertSame('entity:user', $contexts['@user.current_user_context:current_user']->getContextDefinition()->getDataType());
    $this->assertTrue($contexts['@user.current_user_context:current_user']->hasContextValue());
    $this->assertNotNull($contexts['@user.current_user_context:current_user']->getContextValue());

    // Test an anonymous account.     $anonymous = $this->prophesize(AccountInterface::class);
    $anonymous->id()->willReturn(0);
    $this->container->get('current_user')->setAccount($anonymous->reveal());

    $contexts = $context_repository->getAvailableContexts();
    $this->assertArrayHasKey('@user.current_user_context:current_user', $contexts);
    $this->assertSame('entity:user', $contexts['@user.current_user_context:current_user']->getContextDefinition()->getDataType());
    $this->assertFalse($contexts['@user.current_user_context:current_user']->hasContextValue());
  }
/** * @covers ::hasContextValue * @dataProvider providerHasContextValue */
  public function testHasContextValue($has_context_value$default_value): void {
    $definition = new ContextDefinition('any');
    $definition->setDefaultValue($default_value);

    $context = new Context($definition);

    $this->assertSame($has_context_value$context->hasContextValue());
    $this->assertSame($default_value$context->getContextValue());
  }

}
$route_provider = \Drupal::service('router.route_provider');
    $route = $route_provider->getRouteByName($url->getRouteName());
    $route_match = new RouteMatch($url->getRouteName()$route[
      'taxonomy_term' => $term,
    ]);

    // Initiate TermRouteContext with RouteMatch.     $provider = new TermRouteContext($route_match);

    $runtime_contexts = $provider->getRuntimeContexts([]);
    $this->assertArrayHasKey('taxonomy_term', $runtime_contexts);
    $this->assertTrue($runtime_contexts['taxonomy_term']->hasContextValue());
  }

}
/** * {@inheritdoc} */
  public function isSatisfiedBy(ContextInterface $context) {
    $definition = $context->getContextDefinition();
    if (!$this->dataTypeMatches($context)) {
      return FALSE;
    }

    // Get the value for this context, either directly if possible or by     // introspecting the definition.     if ($context->hasContextValue()) {
      $values = [$context->getContextData()];
    }
    elseif ($definition instanceof self) {
      $values = $definition->getSampleValues();
    }
    else {
      $values = [];
    }

    $validator = $this->getTypedDataManager()->getValidator();
    foreach ($values as $value) {
      
        unset($mappings[$plugin_context_id]);

        // Plugins have their on context objects, only the value is applied.         // They also need to know about the cacheability metadata of where that         // value is coming from, so pass them through to those objects.         $plugin_context = $plugin->getContext($plugin_context_id);
        if ($plugin_context instanceof ContextInterface && $contexts[$context_id] instanceof CacheableDependencyInterface) {
          $plugin_context->addCacheableDependency($contexts[$context_id]);
        }

        // Pass the value to the plugin if there is one.         if ($contexts[$context_id]->hasContextValue()) {
          $plugin->setContext($plugin_context_id$contexts[$context_id]);
        }
        elseif ($plugin_context_definition->isRequired()) {
          // Collect required contexts that exist but are missing a value.           $missing_value[] = $plugin_context_id;
        }

        // Proceed to the next definition.         continue;
      }

      
public function testHasContextValue($has_context_value$default_value): void {
    $mock_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface')
      ->onlyMethods(['getDefaultValue'])
      ->getMockForAbstractClass();

    $mock_definition->expects($this->atLeastOnce())
      ->method('getDefaultValue')
      ->willReturn($default_value);

    $context = new Context($mock_definition);

    $this->assertSame($has_context_value$context->hasContextValue());
    $this->assertSame($default_value$context->getContextValue());
  }

  /** * @covers ::getContextValue */
  public function testDefaultValue() {
    $mock_definition = $this->getMockBuilder('Drupal\Component\Plugin\Context\ContextDefinitionInterface')
      ->onlyMethods(['getDefaultValue'])
      ->getMockForAbstractClass();

    
Home | Imprint | This part of the site doesn't use cookies.