Annotations

Same name and namespace in other branches
  1. 9 core/core.api.php \annotation
  2. 8.9.x core/core.api.php \annotation
  3. 11.x core/core.api.php \annotation

Annotations for class discovery and metadata description.

The Drupal plugin system has a set of reusable components that developers can use, override, and extend in their modules. Most of the plugins use annotations, which let classes register themselves as plugins and describe their metadata. (Annotations can also be used for other purposes, though at the moment, Drupal only uses them for the plugin system.)

To annotate a class as a plugin, add code similar to the following to the end of the documentation block immediately preceding the class declaration:


* @ContentEntityType(
*   id = "comment",
*   label = @Translation("Comment"),
*   ...
*   base_table = "comment"
* )

Note that you must use double quotes; single quotes will not work in annotations.

Some annotation types, which extend the "@ PluginID" annotation class, have only a single 'id' key in their annotation. For these, it is possible to use a shorthand annotation. For example:


* @ViewsArea("entity")

in place of


* @ViewsArea(
*   id = "entity"
*)

The available annotation classes are listed in this topic, and can be identified when you are looking at the Drupal source code by having "@ Annotation" in their documentation blocks (without the space after @). To find examples of annotation for a particular annotation class, such as EntityType, look for class files that have an @ annotation section using the annotation class.

See also

Annotation for translatable text

Annotation for context definition

File

core/core.api.php, line 1847

Classes

Title Sort descending File name Summary
Action core/lib/Drupal/Core/Annotation/Action.php Defines an Action annotation object.
Block core/lib/Drupal/Core/Block/Annotation/Block.php Defines a Block annotation object.
Condition core/lib/Drupal/Core/Condition/Annotation/Condition.php Defines a condition plugin annotation object.
ConfigEntityType core/lib/Drupal/Core/Entity/Annotation/ConfigEntityType.php Defines a config entity type annotation object.
Constraint core/lib/Drupal/Core/Validation/Annotation/Constraint.php Defines a validation constraint annotation object.
ContentEntityType core/lib/Drupal/Core/Entity/Annotation/ContentEntityType.php Defines a content entity type annotation object.
ContextDefinition core/lib/Drupal/Core/Annotation/ContextDefinition.php Defines a context definition annotation object.
EntityReferenceSelection core/lib/Drupal/Core/Entity/Annotation/EntityReferenceSelection.php Defines an EntityReferenceSelection plugin annotation object.
EntityType core/lib/Drupal/Core/Entity/Annotation/EntityType.php Defines an Entity type annotation object.
ImageToolkitOperation core/lib/Drupal/Core/ImageToolkit/Attribute/ImageToolkitOperation.php Defines a Plugin attribute for the image toolkit plugin.
Mail core/lib/Drupal/Core/Annotation/Mail.php Defines a Mail annotation object.
Plugin core/lib/Drupal/Component/Annotation/Plugin.php Defines a Plugin annotation object.
PluginID core/lib/Drupal/Component/Annotation/PluginID.php Defines a Plugin annotation object that just contains an ID.
PluralTranslation core/lib/Drupal/Core/Annotation/PluralTranslation.php Defines an annotation object for strings that require plural forms.
QueueWorker core/lib/Drupal/Core/Annotation/QueueWorker.php Declare a worker class for processing a queue item.
Translation core/lib/Drupal/Core/Annotation/Translation.php Defines a translatable annotation object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.