Drupal Drupal Module Development & API 2 — Questions and Answers
Question 1: Which Drupal hook allows a module to alter an existing form before it is rendered?
- hook_form_validate()
- hook_form_alter() (Correct answer)
- hook_form_submit()
- hook_form_rebuild()
Correct answer: hook_form_alter()
hook_form_alter() receives the form array by reference and lets any module modify fields, validators, submit handlers, or structure of any form.
Question 2: In Drupal, what is the purpose of the #cache property in a render array?
- Mark the render array as sensitive, skipping the page cache
- Define cache tags, contexts, and max-age for granular caching (Correct answer)
- Force a full page rebuild on every request
- Store the rendered HTML in the database permanently
Correct answer: Define cache tags, contexts, and max-age for granular caching
The #cache property with tags, contexts, and max-age keys lets Drupal cache render array output at the right granularity and invalidate it precisely.
Question 3: What Drupal API is used to execute a database query that loads multiple nodes matching specific field values?
- db_query()
- EntityQuery via \Drupal::entityQuery() (Correct answer)
- SQL Views API
- NodeStorage::loadMultiple()
Correct answer: EntityQuery via \Drupal::entityQuery()
EntityQuery (via \Drupal::entityQuery() or injected entity type manager) provides a safe, entity-aware query builder that respects access control.
Question 4: What is a Drupal 'Event Subscriber' and which Symfony component does it leverage?
- A hook that fires on cron runs, using Symfony Console
- A class that listens to dispatched events using Symfony EventDispatcher (Correct answer)
- A plugin that subscribes to RSS feeds via Symfony HttpClient
- A service that logs events using Symfony Monolog
Correct answer: A class that listens to dispatched events using Symfony EventDispatcher
Event Subscribers implement EventSubscriberInterface from Symfony's EventDispatcher component and react to events like KernelEvents::REQUEST without using hooks.
Question 5: In Drupal's Plugin API, what is the role of the @annotation on a plugin class?
- Documents the class for PHPDoc generators
- Registers the plugin with Drupal's plugin manager at discovery time (Correct answer)
- Marks the class as abstract so it cannot be instantiated
- Defines ACL permissions for the plugin
Correct answer: Registers the plugin with Drupal's plugin manager at discovery time
Drupal uses Doctrine annotations (like @Block, @Field, @ViewsField) in class docblocks to auto-discover and register plugins without manual registry files.
Question 6: Which Drush command regenerates Drupal's autoloader and clears all caches after adding a new module?
- drush pm:enable
- drush cr
- drush cache:rebuild
- Both drush cr and drush cache:rebuild (they are aliases) (Correct answer)
Correct answer: Both drush cr and drush cache:rebuild (they are aliases)
'drush cr' and 'drush cache:rebuild' are aliases for the same command that rebuilds all Drupal caches including the service container and autoloader.
Which Drupal hook allows a module to alter an existing form before it is rendered?