I am finally diving in to Drupal 8 for a project. In my module though I can not seem to nail down how to render a template from my module based on the route.
In Drupal 7 I would normally do this
custom.module
function union_views_menu() {
$items = array();
$items['home'] = array(
'title' => 'Home',
'description' => 'home apge',
'page callback' => 'home_page',
'access arguments' => array( 'access content'),
'type' => MENU_CALLBACK
);
return $items;
}
function home_page() {
return theme('home_page');
}
function union_views_theme(){
return array(
'home_page' => array(
'template' => 'templates/home-page'
)
);
}
And then I would have a template in the templates folder
With Drupal 8 I got to about here:
custom.routing.yml
custom:
path: /home
defaults:
_controller: Drupal\custom\Controller\CustomController::custom
requirements:
_permission: 'access content'
src/Controller/CustomController.php
namespace Drupal\custom\Controller;
class CustomController {
public function custom(){
return array(
'#title' => 'Custom Theme',
'#markup' => 'This is a content.'
);
}
}
And all the works great for getting to the route. But I can not seem to figure out creating a hook_theme function for my hook_menu to use as a callback.
Figured it out
Add custom.module
function custom_theme() {
$theme['home_page'] = [
'variables' => ['name' => NULL],
'template' => 'home_page'
];
return $theme;
}
in my controller replaced '#markup' with:
'#theme' => 'home_page'