drupaldrupal-modulesdrupal-8drupal-themesdrupal-templates

How do I pass variables to theme template via custom controller drupal 8


I currently have a theme I purchased installed on my Drupal website.

Inside the theme folder there is a templates folder that contains:

a snippet from page.html.twig looks as follows:

    <footer class="site-footer">
  <div class="layout-container">
    {% if page.footer_first or page.footer_second or page.footer_third %}
      <div class="footer-top">
        <div class="footer-top-inner">
          {{ page.footer_first }}
          {{ page.footer_second }}
          {{ page.footer_third }}
        </div>
      </div>
    {% endif %}

    {% if page.footer_left or page.footer_right %}
      <div class="footer-bottom">
        <div class="footer-bottom-inner">
          {{ page.footer_left }}
          {{ page.footer_right }}
        </div><!-- /.footer-bottom-inner -->
      </div>
    {% endif %}
  </div>
</footer>

I have a custom module I created (business_listing) that loads different .html.twig templates I add these templates in business_listing.module:

        function business_listing_theme($existing, $type, $theme, $path) {

      return [
        // business-listing-detail.html.twig
        'business_listing_detail' => [
            'variables' => ['data' => [], 'slides' => [], 'paths' => [], 'page' => []],
        ],
     ]
   }

Basically I would like to know how I can add the markup for the footer dynamically from my custom modules controller.

I have a page called business-listing-detail.html.twig inside my custom modules templates folder. From what I understand business-listing-detail.html.twig uses/somehow extends page.html.twig in my theme/templates. What I would now like to know is how I can add the sections like {{page.footer_left}} to my business-listing-detail.html.twig or to page.html.twig using my controller? I have tried adding the footer using the following: `

$page['footer_left'] = "This should appear in the .footer-bottom div?";

    $build = [];
    $build['business_listing_detail'] = [
      '#theme' => 'business_listing_detail',
      '#data' => $record,
      '#slides' => $slides,
      '#paths' => $this->paths,
      '#page' => $page
    ];`

in the controller function associated to my business-listing-detail page the hopes that page.html.twig will detect this page.footer_left and render display the footer, however this did not work. The variable exists in business-listing-detail.html.twig but the {% if page.footer_left %} in my themes page.html.twig is not fired. Please please please, any help or advice would be greatly appreciated <3

Basically all I am trying to do, is dynamically render a template for a specific page in my custom module, that allows me to implement/send markup to the sections/regions defined in my theme eg. featured_top, content_top & page.content

Kind regards, Matt


Solution

  • It seems like you might be going down the wrong path... I'd highly suggest you read up about the Drupal 8 Block system, and then investigate custom blocks.