How can you contact us

2By phone at +40 749 014 360

If you still have problems, please let us know, by sending an email to info@westweb-solutions.com Thank you!

OFFICE HOURS

Mon-Fri 9:00AM - 6:00AM
Sat - 9:00AM - 1:00PM
Sundays by appointment only!

Drupal 8 - How to use hook_theme

/ / Drupal 8

In this tutorial I will show you a quick implementation of hook_theme, that will list the title/body of a FAQ nodes.

I've created a new routing and a new controller class (FaqController) for this.


mymodule.faq_controller_faq:
  path: 'faq'
  defaults:
    _controller: '\Drupal\mymodule\Controller\FaqController::faq'
    _title: 'FAQ'
  requirements:
    _permission: 'access content'


Because I want to get all FAQ nodes, I'm injecting the entity_type.manager service into our controller class.


namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManager;

/**
 * Class FaqController.
 *
 * @package Drupal\mymodule\Controller
 */
class FaqController extends ControllerBase {

  /**
   * Drupal\Core\Entity\EntityTypeManager definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entity_type_manager;

  /**
   * {@inheritdoc}
   */
  public function __construct(EntityTypeManager $entity_type_manager) {
    $this->entity_type_manager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager')
    );
  }

  /**
   * Faq.
   *
   * @return string
   *   Return Hello string.
   */
  public function faq() {
    // @todo
    // Here goes the implementation of FAQ page using a custom twig template file.
  }
}


Similar to Drupl 7, we need to implement hook_theme that will look very similar:


/**
 * Implements hook_theme().
 */
function mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'faq' => array(
      'variables' => array(
        'items' => array(),
      )
    )
  );
}


In mymodule, I've created a new folder "templates" an dinside of this folder a new file "faq.html.twig".
Please note that in Drupal 8, the extension of the file is .html.twig.

This file will be used to output the html code for our FAQ page.


{% for item in items %}
  

{{ item.title }}

{{ item.body |raw }}

{% endfor %}

In the faq() method from our controller, we can use the #theme property of our output array.


  /**
   * Faq.
   *
   * @return string
   *   Return Hello string.
   */
  public function faq() {
    $build = array();

    // Load all FAQ nodes.
    $nodes = $this->entity_type_manager->getStorage('node')->loadByProperties(array('type' => 'faq'));

    $items = array();
    foreach ($nodes as $node) {
      // Add title and body to the list of items.
      $items[$node->id()] = array(
        'title' => $node->get('title')->value,
        'body' => $node->get('body')->value,
      );
    }

    // Render the 'faq' theme.
    $build['faq'] = array(
      '#theme' => 'faq',
      '#items' => $items,
    );

    return $build;
  }


Sign in your account to have access to different features

Forgot your details?

TOP