Drupal 8 - Creating Custom Module & Sending value from preprocessor hook to display using custom module

In Drupal 8, most of the work can be done without writing any code but there may be situation where you want to write your logic and then want to send the output of that logic to view.

Here, we are using custom module where we have various hook function available  to write login inside their  body and where you can add your value to final output. Most of the function has parameter &$variables which is the reference of $variables variable to be rendered by drupal to produce final output.

Here is the module structure :

drupal-8-module-structure

 

custom.info.yml

name: Custom Module
type: module
description: 'Module to implement customized functionalities'
package: Custom
version: 8.x
core: 8.x

 

custom.module

<?php

/**
 * Implements hook_preprocess().
 */
function custom_preprocess_node(&$variables)
{
  $variables['myname'] = 'Jone Doe';
}

 

Now, if you write {{myname}} in twig file, you would see Jone Doe value in final rendered page.

Tags