Skip to content

Instantly share code, notes, and snippets.

@camoa
Last active June 26, 2020 03:24
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save camoa/7686e80ae494f19b3c3cfde1e087ca7b to your computer and use it in GitHub Desktop.
D728 Task-23 Pre-process output provided by other modules
lotus-js:
version: 1.x
js:
js/lotus.js: {}
dependencies:
- core/jquery
/**
* Implements hook_preprocess_page()
* $variables contains all variables used for the template.
* $hook should be ‘page’ in this case.
**/
Function lotus_preprocess_page(&$variables, $hook) {
// Rendering a table inside the template
// Using a render array is the best practice here - Twigg will render it correctly.
$variables['table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
);
//Add a JS library
$variables['#attached']['library'][] = 'lotus/lotus-js';
}
* Implements hook_theme_suggestions_page_alter()
* $variables contains all variables used for the template.
* $suggestions will contain the current suggestions for the hook
* ‘page’ in this case.
**/
Function lotus_theme_suggestions_page_alter(array &$suggestions, array $variables) {
// Add a new suggestion for a page template
// This allows us to use a template name page--lotus.tpl.php
$suggestions[] = 'page__lotus';
}
// Add classes in templates is a preferred way in Drupal 8
{%
set classes = [
'new-class',
]
%}
<div{{ attributes.addClass(classes) }}>
// Print the table variable
<div class=’table’>
{{ table }}
</div>
//Print the text and link to create new content
<p>{{ 'To create some content <a href="@create-content">click here</a>.'|t({'@create-content': url('admin/structure/types/add')}) }}</p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment