You are here

Annotations in Drupal 10

Same name and namespace in other branches
  1. 8 core/core.api.php \annotation
  2. 9 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 1845
Documentation landing page and topics, plus core library hooks.

Classes

Namesort descending Location Description
Action core/lib/Drupal/Core/Annotation/Action.php Defines an Action annotation object.
AnnotationEnum core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationEnum.php Plugin annotation @Target("ALL");
AnnotationEnumInvalid core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationEnumInvalid.php Plugin annotation @Target("ALL");
AnnotationEnumLiteral core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationEnumLiteral.php Plugin annotation @Target("ALL");
AnnotationEnumLiteralInvalid core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationEnumLiteralInvalid.php Plugin annotation @Target("ALL");
AnnotationExtendsAnnotationTargetAll core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php
AnnotationTargetAll core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationTargetAll.php Plugin annotation @Target("ALL");
AnnotationTargetAnnotation core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationTargetAnnotation.php Plugin annotation @Target({ "ANNOTATION" })
AnnotationTargetClass core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationTargetClass.php Plugin annotation @Target("CLASS");
AnnotationTargetPropertyMethod core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationTargetPropertyMethod.php Plugin annotation @Target({ "METHOD", "PROPERTY" })
AnnotationWithAttributes core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationWithAttributes.php Plugin annotation @Target("ALL") @Attributes({ @Attribute("mixed", type = "mixed"), @Attribute("boolean", type = "boolean"), @Attribute("bool", type = "bool"), @Attribute("float", …
AnnotationWithConstants core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationWithConstants.php Plugin annotation @Target("ALL");
AnnotationWithInvalidTargetDeclaration core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php Plugin annotation @Target("Foo");
AnnotationWithRequiredAttributes core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationWithRequiredAttributes.php Plugin annotation @Target("ALL") @Attributes({ @Attribute("value", required = true , type = "string"), @Attribute("annot", required = true , type =…
AnnotationWithRequiredAttributesWithoutContructor core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationWithRequiredAttributesWithoutContructor.php Plugin annotation @Target("ALL");
AnnotationWithTargetEmpty core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php @Target
AnnotationWithTargetSyntaxError core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationWithTargetSyntaxError.php Plugin annotation @Target(@)
AnnotationWithVarType core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/AnnotationWithVarType.php Plugin annotation @Target("ALL");
AnnotWithDefaultValue core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/Annotation/AnnotWithDefaultValue.php
Archiver core/lib/Drupal/Core/Archiver/Annotation/Archiver.php Defines an archiver annotation object.
Autoload core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/Annotation/Autoload.php
Block core/lib/Drupal/Core/Block/Annotation/Block.php Defines a Block annotation object.
CKEditor4To5Upgrade core/modules/ckeditor5/src/Annotation/CKEditor4To5Upgrade.php Defines a CKEditor4To5Upgrade annotation object.
CKEditor5AspectsOfCKEditor5Plugin core/modules/ckeditor5/src/Annotation/CKEditor5AspectsOfCKEditor5Plugin.php Defines the "CKEditor 5 aspects of a CKEditor5Plugin" annotation object.
CKEditor5Plugin core/modules/ckeditor5/src/Annotation/CKEditor5Plugin.php Defines a CKEditor5Plugin annotation object.
CKEditorPlugin core/modules/ckeditor/src/Annotation/CKEditorPlugin.php Defines a CKEditorPlugin 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.
CustomPlugin core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryTest.php Custom plugin annotation.
CustomPlugin2 core/tests/Drupal/Tests/Component/Annotation/AnnotatedClassDiscoveryTest.php Custom plugin annotation.
DataType core/lib/Drupal/Core/TypedData/Annotation/DataType.php Defines a data type annotation object.
DisplayVariant core/lib/Drupal/Core/Display/Annotation/DisplayVariant.php Defines a display variant annotation object.
DrupalAspectsOfCKEditor5Plugin core/modules/ckeditor5/src/Annotation/DrupalAspectsOfCKEditor5Plugin.php Defines the "Drupal aspects of a CKEditor5Plugin" annotation object.
Editor core/modules/editor/src/Annotation/Editor.php Defines an Editor annotation object.
Entity core/tests/Drupal/Tests/Component/Annotation/Doctrine/Ticket/DCOM58Entity.php This class is a near-copy of tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM58Entity.php, which is part of the Doctrine project: <http://www.doctrine-project.org>. It was copied from version 1.2.7.
Entity core/tests/Drupal/Tests/Component/Annotation/Doctrine/Ticket/DCOM58Test.php
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.
FieldFormatter core/lib/Drupal/Core/Field/Annotation/FieldFormatter.php Defines a FieldFormatter annotation object.
FieldType core/lib/Drupal/Core/Field/Annotation/FieldType.php Defines a FieldType annotation object.
FieldWidget core/lib/Drupal/Core/Field/Annotation/FieldWidget.php Defines a FieldWidget annotation object.
Filter core/modules/filter/src/Annotation/Filter.php Defines a filter annotation object.
FormElement core/lib/Drupal/Core/Render/Annotation/FormElement.php Defines a form element plugin annotation object.
HelpSection core/modules/help/src/Annotation/HelpSection.php Defines a Plugin annotation object for help page section plugins.
ImageEffect core/modules/image/src/Annotation/ImageEffect.php Defines an image effect annotation object.
ImageToolkit core/lib/Drupal/Core/ImageToolkit/Annotation/ImageToolkit.php Defines a Plugin annotation object for the image toolkit plugin.
ImageToolkitOperation core/lib/Drupal/Core/ImageToolkit/Annotation/ImageToolkitOperation.php Defines a Plugin annotation object for the image toolkit operation plugin.
InPlaceEditor core/modules/quickedit/src/Annotation/InPlaceEditor.php Defines an InPlaceEditor annotation object.
LanguageNegotiation core/modules/language/src/Annotation/LanguageNegotiation.php Defines a language negotiation annotation object.
Layout core/lib/Drupal/Core/Layout/Annotation/Layout.php Defines a Layout annotation object.
Mail core/lib/Drupal/Core/Annotation/Mail.php Defines a Mail annotation object.
Marker core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php
MediaSource core/modules/media/src/Annotation/MediaSource.php Defines a media source plugin annotation object.
MigrateDestination core/modules/migrate/src/Annotation/MigrateDestination.php Defines a migration destination plugin annotation object.
MigrateField core/modules/migrate_drupal/src/Annotation/MigrateField.php Defines a field plugin annotation object.
MigrateProcessPlugin core/modules/migrate/src/Annotation/MigrateProcessPlugin.php Defines a migration process plugin annotation object.
MigrateSource core/modules/migrate/src/Annotation/MigrateSource.php Defines a migration source plugin annotation object.
Name core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php
PageDisplayVariant core/lib/Drupal/Core/Display/Annotation/PageDisplayVariant.php Defines a page display variant annotation object.
Plugin core/lib/Drupal/Component/Annotation/Plugin.php Defines a Plugin annotation object.
PluginExample core/modules/system/tests/modules/plugin_test/src/Plugin/Annotation/PluginExample.php Defines a custom Plugin annotation.
PluginExtended core/modules/system/tests/modules/plugin_test_extended/src/Plugin/Annotation/PluginExtended.php Defines a test annotation that extends an annotation in another namespace.
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.
RenderElement core/lib/Drupal/Core/Render/Annotation/RenderElement.php Defines a render element plugin annotation object.
RestResource core/modules/rest/src/Annotation/RestResource.php Defines a REST resource annotation object.
Route core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/Annotation/Route.php
SearchPlugin core/modules/search/src/Annotation/SearchPlugin.php Defines a SearchPlugin type annotation object.
SectionStorage core/modules/layout_builder/src/Annotation/SectionStorage.php Defines a Section Storage type annotation object.
Secure core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/Annotation/Secure.php
SettingsAnnotation core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php
SomeAnnotationClassNameWithoutConstructor core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php
SomeAnnotationClassNameWithoutConstructorAndProperties core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php
SomeAnnotationWithConstructorWithoutParams core/tests/Drupal/Tests/Component/Annotation/Doctrine/DocParserTest.php
Template core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/Annotation/Template.php
Tip core/modules/tour/src/Annotation/Tip.php Defines a tour item annotation object.
Translation core/lib/Drupal/Core/Annotation/Translation.php Defines a translatable annotation object.
Version core/tests/Drupal/Tests/Component/Annotation/Doctrine/Fixtures/Annotation/Version.php Plugin annotation @Target("PROPERTY");
ViewsAccess core/modules/views/src/Annotation/ViewsAccess.php Defines a Plugin annotation object for views access plugins.
ViewsArea core/modules/views/src/Annotation/ViewsArea.php Defines a Plugin annotation object for views area handlers.
ViewsArgument core/modules/views/src/Annotation/ViewsArgument.php Defines a Plugin annotation object for views argument handlers.
ViewsArgumentDefault core/modules/views/src/Annotation/ViewsArgumentDefault.php Defines a Plugin annotation object for views argument default plugins.
ViewsArgumentValidator core/modules/views/src/Annotation/ViewsArgumentValidator.php Defines a Plugin annotation object for views argument validator plugins.
ViewsCache core/modules/views/src/Annotation/ViewsCache.php Defines a Plugin annotation object for views cache plugins.
ViewsDisplay core/modules/views/src/Annotation/ViewsDisplay.php Defines a Plugin annotation object for views display plugins.
ViewsDisplayExtender core/modules/views/src/Annotation/ViewsDisplayExtender.php Defines a Plugin annotation object for views display extender plugins.
ViewsExposedForm core/modules/views/src/Annotation/ViewsExposedForm.php Defines a Plugin annotation object for views exposed form plugins.
ViewsField core/modules/views/src/Annotation/ViewsField.php Defines a Plugin annotation object for views field handlers.
ViewsFilter core/modules/views/src/Annotation/ViewsFilter.php Defines a Plugin annotation object for views filter handlers.
ViewsJoin core/modules/views/src/Annotation/ViewsJoin.php Defines a Plugin annotation object for views join plugins.
ViewsPager core/modules/views/src/Annotation/ViewsPager.php Defines a Plugin annotation object for views pager plugins.
ViewsQuery core/modules/views/src/Annotation/ViewsQuery.php Defines a Plugin annotation object for views query plugins.
ViewsRelationship core/modules/views/src/Annotation/ViewsRelationship.php Defines a Plugin annotation object for views relationship handlers.
ViewsRow core/modules/views/src/Annotation/ViewsRow.php Defines a Plugin annotation object for views row plugins.
ViewsSort core/modules/views/src/Annotation/ViewsSort.php Defines a Plugin annotation object for views sort handlers.
ViewsStyle core/modules/views/src/Annotation/ViewsStyle.php Defines a Plugin annotation object for views style plugins.
ViewsWizard core/modules/views/src/Annotation/ViewsWizard.php Defines a Plugin annotation object for views wizard plugins.
WorkflowType core/modules/workflows/src/Annotation/WorkflowType.php Defines a Workflow type annotation object.