🇪🇸
Drupal Documentation and Guides
Guides
Spanish
Spanish
  • Getting Started with Drupal
  • Installing Drupal
  • Drush
  • Theme & Module Development Concepts
  • Modules
  • Themes
  • FrontEnd
    • Javascript
  • Backend
    • Helpful Drupal Classes and Methods
    • Entities
    • Nodes
    • Taxonomies
    • Hooks
    • Twig
    • Libraries
    • Queries
    • Forms
    • Files & Images
    • Helpful functions and solutions
  • Guides
    • Custom Fields
      • Entity Reference and integer
      • Two Text Fields
      • Textfields, formatted text and numbers
      • Links and media elements
Powered by GitBook
On this page
  1. Backend

Hooks

In Drupal, hooks are the main way to interact with the Drupal core.

PreviousTaxonomiesNextTwig

Last updated 2 years ago

In Drupal, hooks are the main way to interact with the Drupal core. Hooks are functions that are called at specific times in the Drupal core. For example, when a node is saved, a hook is called. When a user logs in, a hook is called. When a page is loaded, a hook is called. There are many hooks that are called in Drupal and they are all documented in the .

Hooks are called from the module_name.module file or from the THEME_NAME.theme file. The module_name.module file is where you put all the code that is related to the module. The THEME_NAME.theme file is where you put all the code that is related to the theme.

Templates - hooks that interact with templates

Create template suggestions

function THEMENAME_theme_suggestions_page_alter(array &$suggestions, array $variables){
 $suggestions[] = 'page__roads';  //page--roads.html.twig
}

Add current path as css class

$current_path = \Drupal::service('path.current')->getPath();
$path_alias = \Drupal::service('path_alias.manager')->getAliasByPath($current_path);
$path_alias = ltrim($path_alias, '/');
$variables['attributes']['class'][] = 'path-' . \Drupal\Component\Utility\Html::cleanCssIdentifier($path_alias);

Get current language

$parameters = \Drupal::routeMatch()->getParameters()->all();
$variables['language'] = \Drupal::languageManager()->getCurrentLanguage();
$variables['language'] = \Drupal::languageManager()->getCurrentLanguage()->getId();

Get base path

$variables['base_path'] = base_path();
Drupal API