W3cubDocs

/Drupal 8

Annotation for context definition

Describes how to use ContextDefinition annotation.

When providing plugin annotations, contexts can be defined to support UI interactions through providing limits, and mapping contexts to appropriate plugins. Context definitions can be provided as such:

  context = {
    "node" = @ContextDefinition("entity:node")
  }

Remove spaces after @ in your actual plugin - these are put into this sample code so that it is not recognized as an annotation.

To add a label to a context definition use the "label" key:

  context = {
    "node" = @ContextDefinition("entity:node", label = @Translation("Node"))
  }

Contexts are required unless otherwise specified. To make an optional context use the "required" key:

  context = {
    "node" = @ContextDefinition("entity:node", required = FALSE, label = @Translation("Node"))
  }

To define multiple contexts, simply provide different key names in the context array:

  context = {
    "artist" = @ContextDefinition("entity:node", label = @Translation("Artist")),
    "album" = @ContextDefinition("entity:node", label = @Translation("Album"))
  }

Specifying a default value for the context definition:

  context = {
    "message" = @ContextDefinition("string",
      label = @Translation("Message"),
      default_value = @Translation("Checkout complete! Thank you for your purchase.")
    )
  }

See also

Annotations

File

core/lib/Drupal/Core/Annotation/ContextDefinition.php, line 8

Classes

Name Location Description
ContextDefinition core/lib/Drupal/Core/Annotation/ContextDefinition.php Defines a context definition annotation object.

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Annotation!ContextDefinition.php/group/plugin_context/8.1.x