Skip to main content
Category:

To pass custom variables to twig file in drupal 8, you must create a preprocess function, for the template:

/**
 * @param $variables
 */
function MY_MODULE_preprocess_TEMPLATE(&$variables) {

    $VARIABLE_NAME = 'my_variable custom text';
    $variables['my_variable'] = $VARIABLE_NAME ;

}

to print variable in twig file just use {{ my_variable }}.

Example how to pass variables to node twig file:

/**
 * @param $variables
 */
function mytheme_preprocess_node(&$variables)
{
    $node = \Drupal::routeMatch()->getParameter('node');
    $nid = $node->id();
    $variables['id'] = $nid;
}

Example how to pass variables to page twig file:

/**
 * @param $variables
 */
function mytheme_preprocess_page(&$variables)
{
    $parameters = \Drupal::routeMatch()->getParameters()->all();
    if (isset($parameters['taxonomy_term'])) {
        $term = $parameters['taxonomy_term'];
        if ($term->getVocabularyId() == 'tag') {
            $variables['is_tag_taxonomy_term_page'] = TRUE;
        }
    }
}

Example how to pass variables to region twig file:

/**
 * @param $variables
 */
function mytheme_preprocess_region(&$variables)
{
    if ($variables['region'] == 'content') {
        $variables['custom_variable'] = 'DEFAULT_VALUE';
    }
}

Example how to pass variables to menu twig file:

/**
 * @param $variables
 */
function THEMENAME_preprocess_menu(&$variables) {
    $variables['language'] = \Drupal::languageManager()->getCurrentLanguage();
}

Example how to pass variables to links twig file:

/**
 * @param $variables
 */
function mytheme_preprocess_links(&$variables) {
    $variables['language'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
}

Example how to pass variables to views view unformatted twig file (views-view-unformatted.html.twig):

/**
 * @param $variables
 */
function THEMENAME_preprocess_views_view_unformatted(&$variables)
{
//    Example 1
    $variables['current_language'] = \Drupal::languageManager()
        ->getCurrentLanguage()
        ->getId();
// Example 2
    $variables['count_rows'] = count($variables['rows']);
}

Example how to pass variables to block twig file (block.html.twig):

/**
 * Process block content
 */
function THEMENAME_preprocess_block(&$variables) {
    $variables['base_path'] = base_path();
}

Example how to pass current language variable to select twig file (select.html.twig):

/**
 * @param $variables
 */
function THEMENAME_preprocess_select(&$variables)
{
    $variables['current_language'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
}

Example how to pass variable to menu twig file (menu.html.twig):

/**
 * @param $variables
 */
function THEMENAME_preprocess_menu(&$variables)
{
    $variables['current_language'] = \Drupal::languageManager()->getCurrentLanguage()->getId();
}

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook