Discover the new way to

Learning Drupal

Master Drupal with practical, real-world exercises designed to make you industry-ready.

Get Started

How DrupalXpert Works

Comprehensive and flexible training programs tailored to your learning goals.

Learning Paths

Access structured lessons that adapt to your pace and objectives.

Hands-On Projects

Engage in real-world exercises and projects that enhance problem-solving skills.

Simulated Work Environment

Gain experience with scenarios that mirror industry challenges.

Industry Expertise

Prepare to excel in professional settings with confidence and proficiency.

Recent Blogs

Welcome to DrupalXpert

Drupal can be intimidating at first. Its learning curve, graphically represented with humor as an almost vertical cliff, is well known among those who have ventured to explore it. That's where DrupalXpert comes in: our purpose is to flatten that curve so that you can learn in an effective, natural and, above all, practical way.

Protecting User Accounts

Drupal is a powerful tool for creating communities or large repositories of users, and with that capability comes the responsibility to protect accounts. Whether you manage an open forum, a news site with limited editors, or an online store, secure authentication is fundamental.

Recent Snippets

Update Data in a Table

Update Data in a Table
                

/**
* Updates data in a custom table.
*/
function my_module_update_data() {
    \Drupal::database()->update('custom_table')
        ->fields([
            'status' => 0,
        ])
        ->condition('id', 5, '=')
        ->execute();
}
 

Update translations with Drupal

Update translations with Drupal
                

/**
* Updates the translations installed on the system.
*/
drush locale:update;

Altering Entity View to Add Cache Contexts

Altering Entity View to Add Cache Contexts
                

/**
* Implements hook_entity_view_alter() to add cache contexts.
*/
function my_module_entity_view_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
   $build['#cache']['contexts'][] = 'user.permissions';
}
 

Altering Entity View to Add Cache Contexts

Altering Entity View to Add Cache Contexts
                

/**
* Implements hook_entity_view_alter() to add cache contexts.
*/
function my_module_entity_view_alter(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
   $build['#cache']['contexts'][] = 'user.permissions';
}
 

Render Array with Cache Metadata

Render Array with Cache Metadata
                

/**
* Render array with Cache API metadata.
*/
$form = [];
$form['content'] = [
   '#markup' => 'Hello World',
   '#cache' => [
      'contexts' => ['user.permissions'],
      'tags' => ['node_list'],
      'max-age' => 3600,
   ],
];
return $form;
 

Render Array with Cache Contexts

Render Array with Cache Contexts
                

$build = [
   '#markup' => 'Hello, world!',
   '#cache' => [
      'contexts' => ['user.roles'],
   ],
];
return $build;
 

Set configuration value with Drush

Set configuration value with Drush
                

/**
* Saves a configuration value directly.
*/
drush config:set system.site name "New Site Name";
 

Delete a Record

Delete a Record
                

/**
* Deletes a record from a custom table.
*/
function my_module_delete_data() {
    \Drupal::database()->delete('custom_table')
        ->condition('id', 10, '=')
        ->execute();
}
 

Check configurable field information

Check configurable field information
                

/**
* Lists the configurable fields for a bundle.
*/
drush field:info node article;
 

Check Drupal directory with Drush

Check Drupal directory with Drush
                

/**
* Displays the directory path for modules, themes or other items.
*/
drush drupal:directory modules/contrib;
// Alias: dr:dir
// Output: /var/www/html/modules/contrib
 

Check environment information with Drush

Check environment information with Drush
                

/**
* Displays a summary of the status of the Drupal and Drush environment.
*/
drush core:status;

 

Check status report with Drush

Check status report with Drush
                

/**
* Displays the Drupal site requirements report.
*/
drush core:requirements
 

Check translation updates with Drush

Check translation updates with Drush
                

/**
* Check for updates in translations.
*/
drush locale:check;

Clear translation update status with Drush

Clear translation update status with Drush
                

/**
* Clears the status of translation updates.
*/
drush locale:clear-status;
 

Post-Deploy command

Post-Deploy command
                

/**
* Executes post-deployment commands.
*/
drush deploy;
 

Check translation updates with Drush

Check translation updates with Drush
                

/**
* Check for updates in translations.
*/
drush locale:check;

Check Drupal directory with Drush

Check Drupal directory with Drush
                

/**
* Displays the directory path for modules, themes or other items.
*/
drush drupal:directory modules/contrib;
// Alias: dr:dir
// Output: /var/www/html/modules/contrib
 

Check configurable field information

Check configurable field information
                

/**
* Lists the configurable fields for a bundle.
*/
drush field:info node article;
 

Check status report with Drush

Check status report with Drush
                

/**
* Displays the Drupal site requirements report.
*/
drush core:requirements
 

Check environment information with Drush

Check environment information with Drush
                

/**
* Displays a summary of the status of the Drupal and Drush environment.
*/
drush core:status;

 

Simple Select Query

Simple Select Query
                

/**
* Fetches data from a custom table.
*/
function my_module_get_data() {
    $query = \Drupal::database()->select('custom_table', 'ct');
    $query->fields('ct', ['id', 'name', 'status']);

    $result = $query->execute()->fetchAll();

    return $result;
}
 

Creates a new field with Drush

Creates a new field with Drush
                

/**
* Creates a new field for an entity bundle.
*/
drush field:create node article field_tags taxonomy_term;
 

Create an Admin User using Drush

Create an Admin User using Drush
                

/**
* Create a new user with a password.
*/
drush user:create username --mail=user@example.com --password=password;
 

Create an Admin User using Drush

Create an Admin User using Drush
                

/**
* Create a new user with a password.
*/
drush user:create username --mail=user@example.com --password=password;
 

Creates a new field with Drush

Creates a new field with Drush
                

/**
* Creates a new field for an entity bundle.
*/
drush field:create node article field_tags taxonomy_term;
 

Delete a Record

Delete a Record
                

/**
* Deletes a record from a custom table.
*/
function my_module_delete_data() {
    \Drupal::database()->delete('custom_table')
        ->condition('id', 10, '=')
        ->execute();
}
 

Dump a whole project

Dump a whole project
                

/**
* Creates an archive dump that includes code, files and database.
*/
drush archive:dump --destination=/path/to/archive.tar.gz --overwrite --exclude-code-paths=web/sites/default/settings.php
// Alias: N/A
// Output: Archive created at /path/to/archive.tar.gz
 

Remove entities with Drush

Remove entities with Drush
                

/**
* Removes content entities from a given bundle.
*/
drush entity:delete node --bundle=article;
 

Remove a field with Drush

Remove a field with Drush
                

/**
* Removes a field from a bundle.
*/
drush field:delete node article field_tags;
 

Export translations with Drush

Export translations with Drush
                

/**
* Export translations to a .po file.
*/
drush locale:export fr --file=translations.fr.po;
 

Dump a whole project

Dump a whole project
                

/**
* Creates an archive dump that includes code, files and database.
*/
drush archive:dump --destination=/path/to/archive.tar.gz --overwrite --exclude-code-paths=web/sites/default/settings.php
// Alias: N/A
// Output: Archive created at /path/to/archive.tar.gz
 

Export translations with Drush

Export translations with Drush
                

/**
* Export translations to a .po file.
*/
drush locale:export fr --file=translations.fr.po;
 

Generate code with Drush

Generate code with Drush
                

/**
* Generates code base for different elements (modules, plugins, etc.).
*/
drush generate;
 

Generate code with Drush

Generate code with Drush
                

/**
* Generates code base for different elements (modules, plugins, etc.).
*/
drush generate;
 

Get maintenance status

Get maintenance status
                

/**
* Shows whether the maintenance mode is activated or not.
*/
drush maint:get;
Alias: N/A
Output: Maintenance mode is OFF.
 

Import translations with Drush

Import translations with Drush
                

/**
* Import translations from a .po file.
*/
drush locale:import fr translations.fr.po;
 

Import translations with Drush

Import translations with Drush
                

/**
* Import translations from a .po file.
*/
drush locale:import fr translations.fr.po;
 

Insert Data into a Table

Insert Data into a Table
                

/**
* Inserts a record into a custom table.
*/
function my_module_insert_data() {
    \Drupal::database()->insert('custom_table')
        ->fields([
            'name' => 'Sample Name',
            'status' => 1,
        ])
        ->execute();
}
 

Insert Data into a Table

Insert Data into a Table
                

/**
* Inserts a record into a custom table.
*/
function my_module_insert_data() {
    \Drupal::database()->insert('custom_table')
        ->fields([
            'name' => 'Sample Name',
            'status' => 1,
        ])
        ->execute();
}
 

Install and uninstall a module with drush in Drupal

Install and uninstall a module with drush in Drupal
                

/**
* Enable a specific module.
*/
drush en module_name -y;
 

/**
* Uninstall a specific module.
*/
drush pm:uninstall module_name -y;
 

Install and uninstall a module with drush in Drupal

Install and uninstall a module with drush in Drupal
                

/**
* Enable a specific module.
*/
drush en module_name -y;
 

/**
* Uninstall a specific module.
*/
drush pm:uninstall module_name -y;
 

Invalidating Cache Tags

Invalidating Cache Tags
                

/**
* Invalidate cache entries tagged with 'my_module_tag'.
*/
\Drupal::cache()->invalidateTags(['my_module_tag']);
 

Invalidating Cache Tags

Invalidating Cache Tags
                

/**
* Invalidate cache entries tagged with 'my_module_tag'.
*/
\Drupal::cache()->invalidateTags(['my_module_tag']);
 

Clear translation update status with Drush

Clear translation update status with Drush
                

/**
* Clears the status of translation updates.
*/
drush locale:clear-status;
 

List all Drush commands

List all Drush commands
                

/**
* Displays a complete list of Drush commands.
*/
drush list;
 

List all Drush commands

List all Drush commands
                

/**
* Displays a complete list of Drush commands.
*/
drush list;
 

Get maintenance status

Get maintenance status
                

/**
* Shows whether the maintenance mode is activated or not.
*/
drush maint:get;
Alias: N/A
Output: Maintenance mode is OFF.
 

Overwrite a base field with Drush

Overwrite a base field with Drush
                

/**
* Creates an overwrite of a base field for a bundle.
*/
drush field:base-override-create node article field_example;
 

Post-Deploy command

Post-Deploy command
                

/**
* Executes post-deployment commands.
*/
drush deploy;
 

Remove a field with Drush

Remove a field with Drush
                

/**
* Removes a field from a bundle.
*/
drush field:delete node article field_tags;
 

Remove entities with Drush

Remove entities with Drush
                

/**
* Removes content entities from a given bundle.
*/
drush entity:delete node --bundle=article;
 

Render Array with Cache Contexts

Render Array with Cache Contexts
                

$build = [
   '#markup' => 'Hello, world!',
   '#cache' => [
      'contexts' => ['user.roles'],
   ],
];
return $build;
 

Render Array with Cache Metadata

Render Array with Cache Metadata
                

/**
* Render array with Cache API metadata.
*/
$form = [];
$form['content'] = [
   '#markup' => 'Hello World',
   '#cache' => [
      'contexts' => ['user.permissions'],
      'tags' => ['node_list'],
      'max-age' => 3600,
   ],
];
return $form;
 

Set configuration value with Drush

Set configuration value with Drush
                

/**
* Saves a configuration value directly.
*/
drush config:set system.site name "New Site Name";
 

Simple Select Query

Simple Select Query
                

/**
* Fetches data from a custom table.
*/
function my_module_get_data() {
    $query = \Drupal::database()->select('custom_table', 'ct');
    $query->fields('ct', ['id', 'name', 'status']);

    $result = $query->execute()->fetchAll();

    return $result;
}
 

Overwrite a base field with Drush

Overwrite a base field with Drush
                

/**
* Creates an overwrite of a base field for a bundle.
*/
drush field:base-override-create node article field_example;
 

Update Data in a Table

Update Data in a Table
                

/**
* Updates data in a custom table.
*/
function my_module_update_data() {
    \Drupal::database()->update('custom_table')
        ->fields([
            'status' => 0,
        ])
        ->condition('id', 5, '=')
        ->execute();
}
 

Update translations with Drupal

Update translations with Drupal
                

/**
* Updates the translations installed on the system.
*/
drush locale:update;

View core documentation

View core documentation
                

/**
* Displays documentation on a specific Drush topic.
*/
drush core:topic;
 

View core documentation

View core documentation
                

/**
* Displays documentation on a specific Drush topic.
*/
drush core:topic;