
Table of Contents
- Installation and Configuration
- Drush Commands
- Modules and Themes
- Configuration Management
- Entities and Database Operations
- Hooks and Services
- Routes and Controllers
- Users and Access Control
- Bonus: Tips and Best Practices
---
Installation and Configuration of a Drupal Instance
-
Install Drupal:
composer create-project drupal/recommended-project drupal-website cd drupal-website``` -
Install Drupal in a Subdirectory:
composer create-project drupal/recommended-project drupal-website mv drupal-website public_html``` -
Initialize the Site:
drush site:install standard --db-url=mysql://user:password@localhost/databasename --site-name="Drupal Website"
---
Drush Commands
-
Common Drush Commands:
drush cr # Clear cache drush uli # Get login URL drush status # Drupal site status drush pm:enable # Enable a module drush pm:uninstall # Uninstall a module drush config:export -y # Export configuration drush config:import -y # Import configuration
---
Modules and Themes
-
Install a Module via Composer:
composer require drupal/devel drush pm:enable devel -
Install a Custom Module:
- Place the module in
modules/custom/module_name. - Define the
.info.ymlfile, hook implementations, and other components.
- Place the module in
-
Install a Custom Theme:
- Place the theme in
themes/custom/theme_name. - Define the
.info.ymlfile, templates, and assets.
- Place the theme in
---
Configuration Management
-
Export Configuration:
drush config:export -
Import Configuration:
drush config:import -
Reset to Default Configuration:
drush config:delete mymodule.settings -
Run PHP code via Drush:
drush php-eval "dump(\Drupal::Config('system.site')->get('name'));"
---
Entities and Database Operations
-
Query with EntityQuery:
$query = \Drupal::entityQuery('node') ->condition('type', 'article') ->accessCheck(TRUE) ->condition('status', 1); $nids = $query->execute(); -
Load an Entity:
$node = \Drupal::entityTypeManager() ->getStorage('node') ->load($node_id); -
Load Multiple Entities:
$entity_manager = \Drupal::entityTypeManager() ->getStorage('node'); $nids = $entity_manager->getQuery() ->condition('type', 'article') ->condition('status', 1) ->accessCheck(TRUE) ->execute(); $nodes = $entity_manager->loadMultiple($nids); -
Create an Entity:
$node = \Drupal\node\Entity\Node::create([ 'type' => 'article', 'title' => 'My New Article', ]); $node->save();
---
Hooks and Services
-
Implement a Hook:
function mymodule_node_view(array &$build, \Drupal\node\NodeInterface $node) { // Modify the node display here. } -
Define a Service:
-
Add to
mymodule.services.yml:services: mymodule.my_service: class: Drupal\mymodule\MyService arguments: [] -
Create
src/MyService.phpwith the defined class.
-
---
Routes and Controllers
- Define a Route:
-
In
mymodule.routing.yml:mymodule.custom_route: path: '/custom-route' defaults: _controller: '\Drupal\mymodule\Controller\CustomController::content' requirements: _permission: 'access content'
-
- Create a Controller:
-
In
src/Controller/CustomController.php:namespace Drupal\mymodule\Controller; use Drupal\Core\Controller\ControllerBase; class CustomController extends ControllerBase { public function content() { return [ '#markup' => 'Hello, Drupal!', ]; } }
-
---
Users and Access Control
-
Check User Permissions:
$current_user = \Drupal::currentUser(); if ($current_user->hasPermission('administer site configuration')) { // The user has permission to administer the site. } -
Create a User:
// Create a new user $user = \Drupal\user\Entity\User::create([ 'name' => 'username', 'mail' => 'user@example.com', ]); $user->save();
---
Bonus: Tips and Best Practices
-
Debugging Tools:
- Use the Devel module for quick debugging.
- Use the
dump($foo)function to debug code and remember to adddie()after the dump to stop PHP execution. - Use
drush wsto monitor logs and errors.
-
For Better Performance:
- Enable caching wherever possible.
- Use Varnish or Redis for additional cache layers.
- Optimize database queries and use the
Drupal\Core\Database\Connectionservice instead of writing raw SQL.
-
Security Best Practices:
- Keep Drupal and all modules/themes up to date.
- Use strong passwords and limit permissions.
- Enable two-factor authentication (2FA) where possible.
