Tos Web Developer provides insights, tutorials, and advice around topics including content strategy, design, Drupal Development, Drupal Custom Module Development, Drupal 8 Custom themes, PHP, Server, Twig, and more



This code how to pass the variable using Themename_preprocess_page and themename_preprocess_html


 <?php  
   /* This Function in theme file of drupal8 go to theme/YOURTHEME file on root you will get your_theme_name.theme file then add is code inside the perprocess_page function if you call theme all variable in html.html.twig file you have to add code inside YOURTHEMENAME_preprocess_html(&$variables) {} then you can render all code in html.html.twig file   
   */  
      function YOUR_THEME_NAME_preprocess_page(&$variables) {  
           /* this code is how to print texonomy term */  
        $vid = 'tags';  
           $terms =\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree($vid);   
           /*This $variables['all_term'] this variable call in twig file using following syntax {{ all_term }} */  
           $variables['all_term']=$terms;  
           /* this code is how to print main menu */  
        $menu_name = 'main';  
           $storage = \Drupal::entityManager()->getStorage('menu_link_content');  
           $menu_links = $storage->loadByProperties(['menu_name' => $menu_name]);  
           /* This code show how to print single menu link */  
           $variables['main_menu_item_title']=$menu_links[5]->title->value;  
           $variables['main_menu_item_url']=$menu_links[5]->link->uri;  
           $main_menu_item_url=explode(':', $variables['main_menu_item_url']);  
           $variables['menu_item_url']=$main_menu_item_url[1];  
           /* This code show how to print base url in twig file */  
        $variables['base_path'] = base_path();  
      }  
 ?>  
Render code in twig file.
 <!-- Following code call single menu item and this single menu item of submenu are taxonomy term.-->  
 <ul id="navigation">  
 <a class="mobile-menu__link --more" href="{{ base_path }}{{ menu_item_url }}">{{ main_menu_item_title }}</a>  
   {% for item in all_term %}  
     <li><a href="{{ base_path}}taxonomy/term/{{ item.tid }}">{{ item.name }}</a></li>  
   {% endfor %}  
 </ul>  

No comments:

Post a Comment

| Designed And Blog Post by www.toswebdeveloper.com