protectedfunctiongetPopulatedContexts(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);
// Test an anonymous account.
$anonymous = $this->prophesize(AccountInterface::class); $anonymous->id()->willReturn(0); $this->container->get('current_user')->setAccount($anonymous->reveal());
// 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 = []; }
// 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; }