0

I'm trying to print a markup in a peg. I've implemented a function passed the values through the variables. Also I've implemented a custom theme function with a markup.

But it is giving the error.

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

Here is my code.

/**
 * Implements hook_theme().
 */
function example_theme($existing, $type, $theme, $path) {
   return array(
    'example' => array(
      'variables' => array('items' => array()),
    ),
  );
}
// Theme function
function theme_example($variables) {
   return 'hi'
}
// Preprocess implementation.
function template_preprocess_example(&$variables) {
  $value = 'hi how are you';
  return $value;
}

I've implemented both one after another, but they didn't work. Is the custom theme function format changed in Drupal 8?

1

From the description of valid parameters in the docs:

function: (deprecated in Drupal 8.0.x, will be removed in Drupal 9.0.x) If specified, this will be the function name to invoke for this implementation. If neither 'template' nor 'function' are specified, a default template name will be assumed. See above for more details.

So if you must use a function, you need to be explicit:

function example_theme($existing, $type, $theme, $path) {
   return array(
    'example' => array(
      'variables' => array('items' => array()),
      'function' => 'theme_example',
    ),
  );
}

But considering that method is deprecated in favour of template files, it would be better to use one. You can do so by simply creating a template file at modules/custom/example/templates/example.html.twig

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.