The idea of annotations in Drupal 8 is that annotations names map to classes. E.g. @ViewsRow -> \Drupal\Views\Annotation\ViewsRow.

From other code I have seen (Symfony, Doctrine), I assumed that to make this work, the annotation class needs to be imported, e.g.

use Drupal\Views\Annotation\ViewsRow;

However, this seems not to be the case for many of the plugin annotations e.g. in Views.

Why? Is this a Drupalism, or would we find the same in some places in Doctrine or Symfony? Is it good practice to omit the import?

In the past I disliked the need for such use statements, because they would show up in the IDE as "unused import". However, now I am using the annotation plugin in PhpStorm, so now it recognizes those imports.


1 Answer 1


Drupal's annotation system is based on the Doctrine PHP library, and not on Symfony like the rest of Drupal.

Doctrine uses a global annotation registry to map classes to files (rather than an autoloader). This page contains the list of classes in that registry.

This issue from 2013 shows the Drupal maintainers' decision to remove the need to import annotation classes.

You wouldn't know this by looking at the official Drupal documentation -- they say the annotation classes are loaded "magically".

  • 1
    Thanks! This is the kind of background information I was looking for.
    – donquixote
    Commented Aug 9, 2017 at 18:39
  • 1
    I think I remember a discussion somewhere, perhaps in php-fig where people discuss to require annotations being namespaced. Does this sound familiar?
    – donquixote
    Commented Aug 9, 2017 at 18:48
  • 1
    Isn't Symfony using Doctrine components for its annotation parsing?
    – donquixote
    Commented Aug 9, 2017 at 19:03
  • 1
    @donquixote I don't follow php-fig, so I'm not sure. And according to symfony.com/doc/current/doctrine.html, looks like Doctrine is decoupled and optional but still included in Symfony. Commented Aug 10, 2017 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.