0

I'm very new to Drupal 10 and I'm having an issue with a custom module I'm trying to create. I have a controller that I'm querying nodes of an FAQ content type that I created. The defined route works when called in the browser. The controller gets called and the query is being made but when I try to theme the output the template is not found. I have a template named "faq-display.html.twig" in my custom/my_theme/templates directory. I've posted my controller code below. I'm sure I have to be missing something simple here but nothing I've tried seems to work.

<?php
namespace Drupal\faq_display\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\node\Entity\Node;

class FaqDisplayController extends ControllerBase {
  
  public function display() {
    $query = \Drupal::entityQuery('node')
      ->accessCheck(FALSE)
      ->condition('type', 'faq')
      ->sort('created', 'DESC');
    
    $nids = $query->execute();
    $nodes = Node::loadMultiple($nids);
    
    $items = [];
    foreach ($nodes as $node) {
      $items[] = [
        '#theme' => 'node',
        '#node' => $node,
      ];
    }
    //\Drupal::logger('FAQ')->error(print_r($items, TRUE));
    return [
      '#theme' => 'faq_display',
      '#items' => $items,
    ];
  }
}

I'm sure I'm missing a step somewhere to get this displayed to the browser but my brain is obviously refusing to look at this the right way or ask google the proper question.

Any links or pointers are appreciated.

3
  • 2
    You need to register the #theme implementation first: api.drupal.org/api/drupal/… Commented Aug 27, 2024 at 20:39
  • Did you clear caches? Commented Aug 28, 2024 at 2:25
  • Thank you @leymannx! Registering the theme in my module did the trick. Commented Aug 28, 2024 at 11:31

1 Answer 1

0

You need to register the #theme first, see hook_theme.

function MYMODULE_theme($existing, $type, $theme, $path) {
  return [
    'faq_display' => [
      'variables' => [
        'items' => NULL,
      ],
    ],
  ];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.