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 %}