2
 * @QueueWorker(
 *   id = "my_module_image_style",
 *   title = @Translation("Generate image styles"),
 *   cron = {"time" = 60}
 * )

And more specifically what is the effect of

 cron = {"time" = 60}
1
  • 1
    Don't agree with the close vote on this.
    – mpdonadio
    Commented May 28, 2020 at 13:55

3 Answers 3

3

This is actually called annotations which provide metadata about the class, and this metadata is gonna be extracted later on with our code to perform a specific logic.

in Drupal context since The QueueWorker is just another drupal plugin, so it will be handled using the Plugin Manager which will extract this information from the Annotations and use them to build some logic for example in this case:

  • $id: the unique identification of the QueueWorker plugin type instance.
  • $title: some drupal method will extract the title from the Annotation and use it to set The human-readable title of the plugin.
  • $cron: another function or same function will extract the cron from the annotation and set us How much time Drupal cron should spend on calling. this worker in seconds. Defaults to 15.

and every drupal plugin fllow this logic:

  1. the plugin manager of this plugin is Drupal\Core\Queue\QueueWorkerManager wich will discover the implementations of the annotation class that has been set in parent:construct() arg 4 ,wich is : (\Drupal\Core\Annotation\QueueWorker) :
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/QueueWorker', $namespaces, $module_handler, 'Drupal\Core\Queue\QueueWorkerInterface', 'Drupal\Core\Annotation\QueueWorker');

  }
  1. @QueueWorker (Annotation class): this annotation is an implementation of the \Drupal\Core\Annotation\QueueWorker class where @QueueWorker = class name and the annotation attributes = the class properties.

    actually in this class peoperty doc Comment you can find a description of each annotation attribute.

   <?php namespace Drupal\Core\Annotation; 
    use Drupal\Component\Annotation\Plugin;  

  class QueueWorker extends Plugin {

  /**
   * The plugin ID.
   *
   * @var string
   */
  public $id;

  /**
   * The human-readable title of the plugin.
   *
   * @ingroup plugin_translatable
   *
   * @var \Drupal\Core\Annotation\Translation
   */
  public $title;

  /**
   * An associative array containing the optional key:
   *   - time: (optional) How much time Drupal cron should spend on calling
   *     this worker in seconds. Defaults to 15.
   *
   * @var array (optional)
   */
  public $cron;

}

6

The class comments are Annotations : What is Annotation mean ?

  1. In general according to techopedia.com

    Annotation is a term used in computer programming to refer to documentation and comments that may be found on code logic. Annotation is typically ignored once the code is executed or compiled.

  2. In Drupal According to Annotations doc page:

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

A lot of frameworks use annotations too like symfony use it in many contexts like creating routes using annotations here.

For the annotation example you put in your question is for QueueWorker plugin:

cron = {"time" = 60}

the definition of cron property QueueWorker::$cron according to QueueWorker::$cron it mean:

time: (optional) How much time Drupal cron should spend on calling this worker in seconds. Defaults to 15.

4

Those are called annotations, and in this context, they are used for plugin discovery.

This:

cron = {"time" = 60}

Signifies a property of the QueueWorker annotation; specifically QueueWorker::$cron, which is:

How much time Drupal cron should spend on calling this worker in seconds. Defaults to 15.

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.