You are here

mgv.module in More Global Variables 8

Create global variables to be printed in any template like so {{ variable }}.

File

mgv.module
View source
<?php

/**
 * @file
 * Create global variables to be printed in any template like so {{ variable }}.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function mgv_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.mgv':

      /** @var \Drupal\mgv\MgvPluginManagerInterface $mgv */
      $mgv = \Drupal::service('plugin.manager.mgv');

      /** @var \Drupal\Core\Render\RendererInterface $renderer */
      $renderer = \Drupal::service('renderer');
      $rows = [];
      foreach ($mgv
        ->getDefinitions() as $definition) {
        $variable = [
          '#type' => 'html_tag',
          '#tag' => 'pre',
          '#value' => '{{ global_variable.' . str_replace('\\', '.', $definition['id']) . ' }}',
        ];
        $class = [
          '#type' => 'html_tag',
          '#tag' => 'i',
          '#value' => $definition['class'],
        ];
        $rows[] = [
          $definition['id'],
          $renderer
            ->render($variable),
          $renderer
            ->render($class),
        ];
      }
      return [
        '#type' => 'container',
        'description1' => [
          '#type' => 'html_tag',
          '#tag' => 'p',
          '#value' => t('Create global variables to be printed in any template like so {{ variable }}.'),
        ],
        'description2' => [
          '#type' => 'html_tag',
          '#tag' => 'p',
          '#value' => t('Module provides variables by the next template - {{ global_variables.**variable_name** }}, i.e. simple - {{ global_variables.variable1 }}, namespaced - {{ global_variables.my_collection.var1 }}, {{ global_variables.my_collection.var2 }}'),
        ],
        'current' => [
          '#type' => 'details',
          '#opened' => FALSE,
          '#title' => t('List of available global variables'),
          'list' => [
            '#type' => 'table',
            '#header' => [
              t('ID'),
              t('Variable name'),
              t('Class'),
            ],
            '#rows' => $rows,
          ],
        ],
        'original_description_wrapper' => [
          '#type' => 'details',
          '#opened' => FALSE,
          '#title' => t('Original description'),
          'original_description' => [
            '#type' => 'html_tag',
            '#tag' => 'pre',
            '#value' => t('Table of Contents.
1) Paths
1.1) Current Path - {{ global_variables.current_path }}
1.2) Current Path Alias - {{ global_variables.current_path_alias }}
1.3) Base URL - {{ global_variables.base_url }}
2) Current Items
2.1) Current Page Title {{ global_variables.current_page_title }}
2.2) Current Langcode {{ global_variables.current_langcode }}
2.3) Current Langname {{ global_variables.current_langname }}
3) Site Information Page Global variables
3.1) Site Name - {{ global_variables.site_name }}
3.2) Site Slogan - {{ global_variables.site_slogan }}
3.3) Site Mail - {{ global_variables.site_mail }}
3.4) Site Logo - {{ global_variables.logo }}
4) Social Sharing
4.1) Twitter - {{ global_variables.social_sharing.twitter }}
4.2) Facebook - {{ global_variables.social_sharing.facebook }}
4.3) LinkedIn - {{ global_variables.social_sharing.linkedin }}
4.4) Email - {{ global_variables.social_sharing.email }}
4.5) WhatsApp - {{ global_variables.social_sharing.whatsapp }}'),
          ],
        ],
      ];
    default:
      return NULL;
  }
}

/**
 * Implements hook_template_preprocess_default_variables_alter().
 */
function mgv_template_preprocess_default_variables_alter(&$variables) {

  /* @var \Drupal\mgv\MgvPluginManager $variables_manager */
  $variables_manager = \Drupal::service('plugin.manager.mgv');
  $variables['global_variables'] = $variables_manager
    ->getVariables();
}