Search code examples
phptwigblockdrupal-8base-url

Link to other page in Drupal 8


In Drupal 7, I used following codes to link to other pages. I have "Service" block and inside that block , I write like this.

<?php
global $base_url;    
global $base_path;   
$link = $base_url . '/sites/all/themes/bootstrap_business/images';
?>
<div><img alt="" src="<?php print $link?>/customer.png" /></div>
<p><a href="<?php echo $base_url;?>/en/test#collapseOne"> Service</a></p>

and I save text format with PHP.
But for now Drupal 8, we don't have Text format option "PHP" and also I don't know how to write codes to connect with other page.
Anyone help me please?
Thanks.


Solution

  • In drupal 8 you can use hook_preprocess_HOOK() to pass variables to twig files and call your variables like

    <header class="main-header">
      {{ title_prefix }}
      {% if page.header and logged_in %}
        {{ page.header }}
      {% endif %}
      {% if not logged_in %}
      <a href="{{ front_page }}" title="{{ 'Home'|t }}" rel="home" id="logo" class="logo">
        <img src="{{ base_path }}themes/custom/mytheme/logo-login.png" alt="{{ 'Home'|t }}" />
      </a>
      <h2 class="login-logo">{{ site_name }}</h2>
      {% endif %}
      {{ title_suffix }}
    </header>
    

    Please see https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates for more details

    You can also include other pages using

    {# this template is located in templates/layout.html.twig #}
    {% extends "layout.html.twig" %}
    
    {# this template is located in templates/user/profile.html.twig #}
    {{ include('user/profile.html.twig') }}