3

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.

4

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".

| improve this answer | |
  • 1
    Thanks! This is the kind of background information I was looking for. – donquixote Aug 9 '17 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 Aug 9 '17 at 18:48
  • 1
    Isn't Symfony using Doctrine components for its annotation parsing? – donquixote Aug 9 '17 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. – Lincoln Bergeson Aug 10 '17 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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