0
votes

I have problem with template for my custom page in my module. I use hook_theme() to define my twig file. And when I check in hook_theme_registry_alter() I see my new template file but when I try use it it is not working.

My code :

file: first.module

/**
 * Implement hook_theme().
 */
function first_theme($existing, $type, $theme, $path) {
  return array(
    'testtwig' => array(
      'template' => 'testtwig',
      'variables' => array('test_var' => NULL),
    ),
  );
}

Controller:

/**
 * @file
 * Contains \Drupal\first\Controller\FirstController.
 */

namespace Drupal\first\Controller;

use Drupal\Core\Controller\ControllerBase;

class FirstController extends ControllerBase {

  public function content() {
    return array(
        '#theme' => 'testtwig',
        '#test_var' => t('sss'), //$output,
      );
  }
}

Error:

Template "modules/custom/first/templates/testtwig.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "modules/custom/first/templates/testtwig.html.twig" in the Drupal theme registry.).

1
What name did you get the twig file and where did you put it?acrosman
Twig file name : testtwig.html.twig and i put this file in modules/custom/first/templatesDziabodo
I find wher is problem :) so i have another file with the same name. Thanks for HelpDziabodo

1 Answers

0
votes

//.module file

 <?php

    /**
     * Implements hook_theme().
     */
    function MODULE_theme($existing, $type, $theme, $path) {
        return [
            'site_framework_display' => [
                'variables' => ['test_var' => NULL],
                'template'  => 'page--site-framework',
            ],
        ];
    }

//Controller

<?php

namespace Drupal\MODULE\Controller;

use Drupal\Core\Controller\ControllerBase;

class MODULEController extends ControllerBase {

    public function getVersion() {

        return [
            '#theme' => 'site_framework_display',
            //'#test_var' => \DRUPAL::VERSION,
            '#test_var' => 'hello guys'
        ];

    }
}