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"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 configurationInstall a Module via Composer:
composer require drupal/devel
drush pm:enable develInstall a Custom Module:
modules/custom/module_name..info.yml file, hook implementations, and other components.Install a Custom Theme:
themes/custom/theme_name..info.yml file, templates, and assets.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'));"
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();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: []src/MyService.php with the defined class.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!',
];
}
}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();Debugging Tools:
dump($foo) function to debug code and remember to add die() after the dump to stop PHP execution.drush ws to monitor logs and errors.For Better Performance:
Drupal\Core\Database\Connection service instead of writing raw SQL.Security Best Practices: