This module explains the core Drupal APIs every backend developer must know. These APIs are heavily tested in the Acquia Certified Drupal Developer exam because they separate a real Drupal developer from a PHP developer.
Drupal is not just about writing PHP code. Drupal is about using its APIs correctly.
Acquia expects you to:
- Choose the correct API for the problem
- Understand why the API exists
- Avoid reinventing functionality
Most wrong answers in the exam ignore Drupal APIs and jump straight to custom code.
The essential APIs you must know
In this module we cover:
- Cache API
- Cache Metadata
- Queue API
- Cron API
- Logging API
- Messenger API
- State API
- Lock API
- File API
1. Cache API
The Cache API allows Drupal to store computed data so it does not need to be rebuilt every request.
Drupal caching is not just speed. It is correctness + performance.
Caching is like:
- Writing notes so you do not recalculate answers every time
example
$cache = \Drupal::cache('default');
$data = $cache->get('my_key');
if (!$data) {
$data = expensiveCalculation();
$cache->set('my_key', $data, Cache::PERMANENT);
}
Exam scenario
Scenario: You have expensive calculations that run on every page load.
Correct answer: Cache API
Wrong answer: Database queries on every request
2. Cache Metadata (Contexts, Tags, Max-age)
Cache metadata tells Drupal when cached data is valid.
This is one of the most important Drupal concepts.
The three parts
- Cache contexts: vary by condition (user, language)
- Cache tags: invalidate when data changes
- Cache max-age: how long cache lives
Render array example
return [
'#markup' => 'Hello',
'#cache' => [
'contexts' => ['user.roles'],
'tags' => ['node:1'],
'max-age' => Cache::PERMANENT,
],
];
Exam scenario
Scenario: Content changes but cached page does not update.
Correct answer: Add cache tags
3. Queue API
Queue API allows Drupal to process tasks later, not during the request.
Queue is like:
- Writing tasks on a list and doing them one by one
Real Drupal example
$queue = \Drupal::queue('my_queue');
$queue->createItem($data);
Worker:
class MyQueueWorker extends QueueWorkerBase {
public function processItem($data) {
// Process item
}
}
Exam scenario
Scenario: Sync thousands of records from an external API.
Correct answer: Queue API
Wrong answer: Process everything in one request
4. Cron API
Cron runs scheduled background tasks.
Real Drupal example
function my_module_cron() {
// Periodic task
}
Exam scenario
Scenario: Clean up old data every night.
Correct answer: Cron
5. Logging API
Drupal logs system events and errors using PSR-3 logging.
Real Drupal example
\Drupal::logger('my_module')->error('Something failed');
Exam scenario
Scenario: You need to record errors without showing users.
Correct answer: Logging API
Wrong answer: print or echo
6. Messenger API
Shows messages to users.
Real Drupal example
\Drupal::messenger()->addStatus('Saved successfully');
Exam scenario
Scenario: Inform user after form submission.
Correct answer: Messenger API
7. State API
Stores temporary environment-specific values.
Real Drupal example
\Drupal::state()->set('last_run', time());
Exam scenario
Scenario: Store last cron run time.
Correct answer: State API
8. Lock API
Prevents race conditions.
Real Drupal example
$lock = \Drupal::lock();
if ($lock->acquire('my_lock')) {
// Safe code
$lock->release('my_lock');
}
Exam scenario
Scenario: Prevent multiple processes running same task.
Correct answer: Lock API
9. File API
Handles files securely.
Real Drupal example
$file = file_save_data($data, 'public://example.txt');
Exam scenario
Scenario: Save uploaded files securely.
Correct answer: File API
Security considerations (Acquia focus)
- APIs enforce access and safety
- Avoid raw PHP functions
- Avoid direct filesystem access
Performance considerations
- Cache everything possible
- Use Queue for heavy work
- Avoid work during requests
Common Acquia exam traps (Module 4.4)
- Ignoring cache metadata
- Long-running processes in controllers
- Logging via print or echo
- Using database instead of queues
Final takeaway
Drupal APIs exist to solve common problems safely, performantly, and consistently. Use them.