0

I created a custom module to show nodes by taxonomy in a specific html twig. I created a controller that contain two functions. The first function called "content" pass an array to template twig contain all themes and all nodes. I create a path (e.g: /pub) for that. The second function called "contentByTaxonomy" pass an array to twig that contain nodes by specific taxonomy. I create a path (e.g /pub/{id}). The (/pub) works fine, but (/pub/{id}) return "page not found". I just want to know what is missing in this code.

file : custom.module

function custom_theme($existing, $type, $theme, $path) {
   return [
        'my_template' => [
            'variables' => [
                   'items' => [],
            ],

        ],

    ];
}

file custom.routing.yml

custom.content:
  path: '/pub'
  defaults:
    _controller: '\Drupal\custom\Controller\PubController::content'
    _title: 'Pub'
  requirements:
    _permission: 'access content'


custom.contentByTaxonomy:
  path: '/pub/{id}'
  defaults:
    _controller: '\Drupal\custom\Controller\PubController::contentByTaxonomy'
    _title: 'Pub Theme'
  #methods:  [GET]
  requirements:
    _permission: 'access content'
  #options:
   # parameters:
    #  id:
     #   type: entity:node

My controller (\modules\custom\src\Controller) Note: getNodesByTaxonomy() and getNodes() are defined and return array.

class PubController extends ControllerBase {
    public function content() {
                    $items = $this->getNodes();
            /* pass array to twig template */
            return [
                '#theme'=> 'my_template',
                '#items'=> $items,
            ];

    }

    public function contentByTaxonomy($id) {
                    $items = $this->getNodesByTaxonomy($id);
            /* pass array to twig template */
            return [
                '#theme'=> 'my_template',
                '#items'=> $items,
            ];
    }
}

My tiwg template : (\modules\custom\templates\my-template.html.twig)

 {% for item in items %}
     {# here i display my items #}
 {% endfor %}
  • Why aren't you simply using views module with a filter? So much code for something already built and tested in Drupal core. Not missing code, just way too much code ;-) – Hudri Apr 15 at 15:19
  • Did you clear the cache after changing the routing file? Otherwise its hard to debug this without knowing anything else. drupal.org/docs/8/api/routing-system/parameters-in-routes/… – Kevin Apr 15 at 16:36
  • @Hudri I have complex display of items that's why i create a custom module with tiwg template. I uncomment that ligne ( #methods: [GET] ) and it worked. – youssef hrizi Apr 15 at 16:59
  • @Kevin I forgot to uncomment that ligne( #methods: [GET] ) and it worked. – youssef hrizi Apr 15 at 17:01
  • That should not be required. I have plenty of controllers where I do not have to specify that. – Kevin Apr 15 at 18:20

Your Answer

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

Browse other questions tagged or ask your own question.