n Create custom twig templates for custom module in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Example 1

Create mymodule.module file

/**
 * Implements hook_theme() to add the template definition.
 **/
function mymodule_theme($existing, $type, $theme, $path) {
    return array(
        'mymodule_template' => array(
            'variables' => array('test_var' => NULL),
        ),
    );
}

Create src/Controller/MyModuleController.php file

<?php
namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;

class MyModuleController extends ControllerBase {
    public function content() {

        return array(
            '#theme' => 'mymodule_template',
            '#test_var' => $this->t('Test Value'),
        );

    }
}

Create in the templates folder the mymodule-template.html.twig file

<p> This is the lotus template with a value of {{ test_var }} </p>

 

Example 2

I will use the two examples below to show you how to add custom template to our codimth_controller module.

https://codimth.com/blog/drupal8-get-user-list-role-programmatically

https://codimth.com/blog/drupal-8-create-custom-page-programmatically

Create  hook_theme() in codimth_controller.module 

<?php

/**
 * @param $existing
 * @param $type
 * @param $theme
 * @param $path
 * @return array
 */
function codimth_controller_theme($existing, $type, $theme, $path) {
    return [
        'codimth' => [
            'variables' => ['users' => NULL],
        ],
    ];
}

 

Create src/Controller/CodimthController.php file

 
<?php

namespace Drupal\codimth_controller\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\user\Entity\User;

class CodimthController extends ControllerBase
{
    public function index()
    {
        $userlist = [];
        $ids = \Drupal::entityQuery('user')
            ->condition('status', 1)
            ->condition('roles', 'administrator')
            ->execute();
        $users = User::loadMultiple($ids);
        foreach ($users as $user) {
            $username = $user->get('name')->getString();
            $mail = $user->get('mail')->getString();
            $userlist[] = ['mail' => $mail, 'username' => $username];
        }

        return array(
            '#theme' => 'codimth',
            '#users' => $userlist
        );
    }
}

 

Create twig template templates/codimth.html.twig 

<p>Test twig template!</p>

{% for user in users %}
    <p>{{ user.mail }}: {{ user.username }}</p>
{% endfor %}

Next steps

  • Clear your Drupal 8 caches. To do this I use this Drush command: drush cr if you don’t currently use Drush, I highly recommend using it, or the Drupal Console.
  • Now, go back to your site, and you should be able to see the new custom template you have just created.
  • I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.
  • This code can be found and downloaded from https://github.com/codimth/codimth_controller.

 

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook