Skip to content

Instantly share code, notes, and snippets.

@DejanBelic
DejanBelic / mytheme.theme
Last active March 29, 2017 11:37
Template suggestion for contact form
function THEME_theme_suggestions_form_alter(array &$suggestions, array $variables) {
$suggestions[] = 'form__' . $variables['element']['#form_id'];
}
@DejanBelic
DejanBelic / templateFile.html.twig
Created February 25, 2017 07:13
Link other fields with link field in drupal 8.
{{ link(content.field_products_image, content.field_products_read_more[0]['#url'], { 'class':['class-link']} ) }}
@DejanBelic
DejanBelic / mytheme.theme
Created February 25, 2017 07:04
Attaching library to front page in drupal 8.
function my_theme_preprocess_page(&$variables) {
if ($variables['is_front']) {
$variables['#attached']['library'][] = 'my_theme/googleMap';
}
}
@DejanBelic
DejanBelic / mytheme.theme
Created February 25, 2017 07:03
Add class to specific article in drupal 8.
function my_theme_preprocess_node(&$variables) {
if (($variables['elements']['#attributes']['data-history-node-id']) == 10) {
$variables['attributes']['class'][] = 'about-us-';
}
}
@DejanBelic
DejanBelic / mytheme.theme
Created February 25, 2017 07:01
Naming templates based on page title in drupal 8.
function my_theme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
// Naming templates based on page title.
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$translation = $node->getTranslation('en');
$suggestions[] = 'page__node__' . strtolower($translation->title->value);
$title = $node->getTitle();
foreach ($suggestions as &$replace) {
$replace = str_replace(' ', '_', $replace);
}
}
@DejanBelic
DejanBelic / mytheme.theme
Created February 25, 2017 06:57
Add node title as body class in drupal 8.
function hook_preprocess_html(&$variables) {
// Add node title as body class.
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$node = $node->getTranslation('en');
$title = $node->label();
$title = str_replace(' ', '-', $title);
$title = str_replace('_', '-', $title);
$variables['title_en'] = strtolower($title);
}
@DejanBelic
DejanBelic / page.html.twig
Created February 25, 2017 06:54
Create dynamic year copyright in twig
<span class="copyright">&copy; {{ 'now'|date('Y') }} Dejan • All rights reserved</span>