Advanced Drupal Interview Questions and Answers
---
1. How does caching work in Drupal?
Drupal uses multiple layers of caching to improve performance and scalability. These include:
- **Page cache**: Stores the entire rendered HTML page for anonymous users.
- **Dynamic page cache**: For authenticated users, stores parts of the rendered page.
- **Render cache**: Stores the output of render arrays.
- **Entity & view cache**: Caches specific entity and Views outputs.
- **Internal page cache**: For quick serving of cached pages without hitting full bootstrap.
---
2. Where is cache stored in Drupal?
By default, cache data is stored in the database (`cache_*` tables). However, high-performance environments typically
use external cache backends like Redis or Memcached. These are configured in `settings.php`.
---
3. If we want to cache a particular block, how do we do it?
You can control block caching in a custom block plugin by implementing:
- `getCacheMaxAge()` to set cache lifespan.
- `getCacheTags()` to attach tags for invalidation.
- `getCacheContexts()` to vary by conditions like route, user role, language.
Example:
```php
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function getCacheContexts() {
return ['user.roles'];
public function getCacheTags() {
return ['node_list'];
```
---
4. What are hooks in Drupal?
Hooks are PHP functions named with a pattern like `hook_form_alter()` that allow modules to intercept and modify core
or contrib behaviors. Common hooks include:
- `hook_node_insert()` - Called after a node is saved.
- `hook_entity_presave()` - Before an entity is saved.
- `hook_cron()` - Runs during cron.
- `hook_form_FORM_ID_alter()` - Alter specific forms.
---
5. Hooks for themes
Themes can implement hooks to manipulate templates and assets. Examples include:
- `hook_theme()` - Register new templates and preprocess logic.
- `hook_preprocess_HOOK()` - Preprocess variables before rendering.
- `hook_page_attachments()` - Add CSS/JS assets conditionally.
- `hook_html_head_alter()` - Modify meta tags.
---
6. Event Subscribers
Event Subscribers are OOP replacements for some procedural hooks. You create a service tagged with
`event_subscriber`, implement `EventSubscriberInterface`, and define which events to listen for.
Example: Listening to `KernelEvents::REQUEST`.
```php
public static function getSubscribedEvents() {
return [KernelEvents::REQUEST => ['onRequest', 27]];
```
---
7. Drupal Database Questions
- Use `\Drupal::database()` for custom queries.
- Prefer the **Entity Query API** for retrieving entities.
- Always sanitize inputs or use placeholders to avoid SQL injection.
- Use the `Database::getConnection()->select()` for complex joins.
---
8. AJAX in Drupal
Drupal handles AJAX via the Form API using the `#ajax` property. On submission or change, Drupal triggers a callback
that returns an `AjaxResponse` object with commands.
```php
$form['example']['#ajax'] = [
'callback' => '::myAjaxCallback',
'wrapper' => 'replace-div',
];
```
Commands include `HtmlCommand`, `ReplaceCommand`, and `InvokeCommand`.
---
9. Migration API Questions
The Migrate API allows structured data import via YAML config or PHP plugins.
Key parts:
- **Source plugin**: e.g., CSV, SQL, JSON.
- **Process plugin**: e.g., `default_value`, `concat`, `migration_lookup`.
- **Destination plugin**: Usually nodes, taxonomy, users, etc.
Run migrations via Drush:
- `drush migrate:import my_migration`
- `drush migrate:rollback my_migration`
Track relationships using `migration_lookup`, and add multilingual support using `content_translation` plugins.
---
10. Cache Tags, Contexts, and Max Age
- **Cache Tags**: Invalidate items based on tags (e.g., `node:1`, `config:block.block.site_footer`).
- **Cache Contexts**: Vary by request context (e.g., language, user permissions).
- **Max Age**: TTL for cache. Set to 0 (no cache) or `Cache::PERMANENT`.
---
11. Migrate API - In-Depth Questions
- **What is the Migrate API?** Framework for importing data using Source, Process, and Destination plugins.
- **Custom Process Plugin**: Create under `Plugin/migrate/process/`, extend `ProcessPluginBase`, override
`transform()`.
- **Entity Relationships**: Use `migration_lookup` to map referenced entity IDs.
- **File/Image Migration**: Use `file_import`/`image_import` plugins with valid paths.
- **Delta Updates**: Use `highwater` or `track_changes` to import only changed records.
- **Migration Groups**: Organize related migrations and share configuration.
- **migrate_plus**: Adds YAML support, source plugins.
- **migrate_tools**: Provides UI and Drush support.
- **Multilingual Migrations**: Run separate migration for translations using `langcode`, `sourceid1`, `translation`
mapping.
- **Debugging**: Use `drush migrate:messages`, logs, or Xdebug.
---