Documentation follow-ups for #2354889

Created on 8 July 2015, almost 10 years ago
Updated 20 May 2025, 14 days ago

Problem/Motivation

The runtime vs. available contexts methods from #2354889: Make block context faster by removing onBlock event and replace it with loading from a ContextManager β†’ were a very confusing part of that issue.

I think the docs still need work:

**
+   * Gets runtime context values for the given context IDs.
+   *
+   * For context-aware plugins to function correctly, all of the contexts that
+   * they require must be populated with values. So this method should set a
+   * value for each context that it adds. For example:
+   *
+   * @code
+   *   // Determine a specific node to pass as context to a block.
+   *   $node = ...
+   *
+   *   // Set that specific node as the value of the 'node' context.
+   *   $context = new Context(new ContextDefinition('entity:node'));
+   *   $context->setContextValue($node);
+   *   return ['node' => $context];
+   * @endcode
+   *
+   * On the other hand, there are cases, on which providers no longer are
+   * possible to provide context objects, even without the value, so the caller
+   * should not expect it.
+   *
+   * @param string[] $unqualified_context_ids
+   *   The requested context IDs. The context provider must only return contexts
+   *   for those IDs.
+   *
+   * @return \Drupal\Core\Plugin\Context\ContextInterface[]
+   *   The determined available contexts, keyed by the unqualified context_id.
+   *
+   * @see \Drupal\Core\Plugin\Context\ContextProviderInterface:getAvailableContexts()
+   */
+  public function getRuntimeContexts(array $unqualified_context_ids);
+

It looks like there are at least two possible outcomes from this function, which probably have the same effect in practice:

1. Method is unable to get a value for a context object, so returns the object with no value set.

2. Method is unable to get a value for a context object, so doesn't return the object at all (i..e only returns objects with values set and none others).

If both of these have the same implications for calling code, then why not only return context objects with values set, and just don't return anything for ones that don't. You can create a context object with no value yourself anyway.

This also brings up another question, which is why do we both having context objects with no value at all? There are context definition objects. What does a context object with no value provide that the definition object doesn't? And if so why not a different interface?

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ“Œ Task
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

block.module

Created by

πŸ‡¬πŸ‡§United Kingdom catch

Live updates comments and jobs are added and updated live.
  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for creating this issue to improve Drupal.

    We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024