Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
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();
}

Comments

Submitted by Gonzalo (not verified) on Mon, 03/29/2021 - 02:55 Permalink

If you are getting "/" as the output you can use the next code: /** * Implements template_preprocess_block(). */

 

function rma_preprocess_block(&$variables) {

     global $base_url;

   $variables['base_path'] = $base_url;

}

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon