drupal 8 how to print any custom variable in twig

  • Theme name : atvdirect
    create a file atvdirect.theme on root of your theme where atvdirect.info.yml file is places.

    • add below code in atvdirect.theme file
<?php
function atvdirect_preprocess_page(&$variables) {
  $variables['logopath'] = '/' . drupal_get_path('theme','atvdirect') . '/logo.png';
}
?>

now you can use {{ logopath }} in page.html.twig

add custom class to menu in drupal 8

file name
/var/www/html/drupal/themes/atvdirect/templates/navigation/menu.html.twig

we are checking menu name in if condition
{% if menu_name ==’account’ %}

below is the whole code you may copy and replace it

{#
/**
 * @file
 * Theme override to display a menu.
 *
 * Available variables:
 * - menu_name: The machine name of the menu.
 * - items: A nested list of menu items. Each menu item contains:
 *   - attributes: HTML attributes for the menu item.
 *   - below: The menu item child items.
 *   - title: The menu link title.
 *   - url: The menu link url, instance of \Drupal\Core\Url
 *   - localized_options: Menu link localized options.
 *   - is_expanded: TRUE if the link has visible children within the current
 *     menu tree.
 *   - is_collapsed: TRUE if the link has children within the current menu tree
 *     that are not currently visible.
 *   - in_active_trail: TRUE if the link is in the active trail.
 */
#}
{% import _self as menus %}

{#
  We call a macro which calls itself to render the full tree.
  @see http://twig.sensiolabs.org/doc/tags/macro.html
#}
{{ menus.menu_links(items, attributes, 0,menu_name) }}


{% macro menu_links(items, attributes, menu_level,menu_name) %}
  {% import _self as menus %}
  {% if items %}
  
    {% if menu_level == 0 %}
          {% if menu_name =='account' %}
              <ul{{ attributes.addClass('') }}>
          {% endif %}

          {% if menu_name =='main' %}
              <ul{{ attributes.addClass('menu nav navbar-nav') }}>
          {% endif %}
          
    {% else %}
            <ul{{ attributes.addClass('menu nav navbar-nav') }}>
    {% endif %}
    {% for item in items %}
      {%
        set classes = [
          'menu-item',
          item.is_expanded ? 'menu-item--expanded',
          item.is_collapsed ? 'menu-item--collapsed',
          item.in_active_trail ? 'menu-item--active-trail',
        ]
      %}
      <li{{ item.attributes.addClass(classes) }}>
        {{ link(item.title, item.url) }}
        {% if item.below %}
          {{ menus.menu_links(item.below, attributes, menu_level + 1) }}
        {% endif %}
      </li>
    {% endfor %}
    </ul>
  {% endif %}
{% endmacro %}

Add view port in html header in drupal 7

Add below function in template.php of your theme
replace emerachem with your theme name .


function emerachem_preprocess_html(&$vars) { 
  $viewport = array(
   '#tag' => 'meta',
   '#attributes' => array(
     'name' => 'viewport',
     'content' => 'width=device-width, initial-scale=1, maximum-scale=1',
   ),
  );
  drupal_add_html_head($viewport, 'viewport');
}

after adding this code clear chache