Drupal Drupal Module Development & API 1 — Questions and Answers
Question 1: In Drupal module development, what file declares a module's name, description, and package for the admin UI?
- module_name.routing.yml
- module_name.info.yml (Correct answer)
- module_name.services.yml
- module_name.module
Correct answer: module_name.info.yml
The .info.yml file is required for every Drupal module and defines its human-readable name, description, type, core_version_requirement, and package.
Question 2: Which Drupal API function is used to implement a hook that reacts after a node is saved?
- hook_node_presave()
- hook_node_insert() (Correct answer)
- hook_entity_postsave()
- hook_node_save_complete()
Correct answer: hook_node_insert()
hook_node_insert() fires after a new node is saved to the database for the first time, while hook_node_update() fires on subsequent saves.
Question 3: What is the Drupal service container used for in module development?
- Caching rendered HTML blocks
- Managing dependency injection of reusable service objects (Correct answer)
- Storing configuration in the database
- Queuing background batch jobs
Correct answer: Managing dependency injection of reusable service objects
Drupal's service container (built on Symfony's DependencyInjection component) manages service instantiation and dependency injection throughout custom code.
Question 4: Which Drupal class should a custom plugin implement to create a new Block plugin type?
- \Drupal\Core\Plugin\PluginBase
- \Drupal\Core\Block\BlockBase (Correct answer)
- \Drupal\Core\Controller\ControllerBase
- \Drupal\Core\Form\FormBase
Correct answer: \Drupal\Core\Block\BlockBase
Custom blocks extend BlockBase, which implements the BlockPluginInterface and provides build(), blockAccess(), and blockForm() scaffold methods.
Question 5: In Drupal's routing system, what file defines URL paths and their corresponding controller classes?
- module_name.links.menu.yml
- module_name.routing.yml (Correct answer)
- module_name.permissions.yml
- module_name.services.yml
Correct answer: module_name.routing.yml
Routes are defined in the module's .routing.yml file, mapping URL paths to controller methods and specifying access requirements.
Question 6: What does the Drupal Entity API's EntityTypeManager service allow developers to do?
- Render twig templates for any entity
- Load, query, create, and delete any entity type (Correct answer)
- Manage file system paths for uploaded media
- Register new content types programmatically
Correct answer: Load, query, create, and delete any entity type
EntityTypeManager provides handlers for loading, saving, deleting, and querying any entity type, replacing older entity_load() procedural functions.
In Drupal module development, what file declares a module's name, description, and package for the admin UI?