Skip to content

Instantly share code, notes, and snippets.

View NikLP's full-sized avatar

NikLP NikLP

  • Nottingham, UK
  • 19:21 (UTC -12:00)
  • X @NikLP
View GitHub Profile
@NikLP
NikLP / mytheme.theme
Last active June 14, 2022 10:01
MYTHEME.theme snippet for language switcher block in Drupal 8+
<?php
// Changes the language switcher block to use country codes instead of long format country names
// So if the site was in English/Italian, the original switcher would show (at D9 iirc):
// English - Italian, and this code makes it into EN - IT (lack of correct formatting notwithstanding)
// This could easily be adapted to show flags etc instead I imagine.
/**
* Implements hook_preprocess_block().
*/
function mytheme_preprocess_block(&$variables) {
@NikLP
NikLP / gist:ba2dd314f7d76a11c0f90ae6692660f1
Created March 24, 2022 16:27
Add inline styles to head tag
function customtheme_preprocess_html(&$variables) {
$variables['#attached']['html_head'][] = [
[
'#type' => 'html_tag',
'#tag' => 'style',
'#value' => 'custom-class { background: red; }'
],
'my-custom-theme'
];
}
.view-VIEWNAME .form--inline .form-item {
display: inline;
float: none;
}
.view-VIEWNAME .form--inline .form-actions {
display: inline-block;
}
@NikLP
NikLP / zebra.html.twig
Last active February 25, 2021 12:55
Set zebra-style (odd/even) classes in twig loop in drupal 8 (bootstrap) theme
<div{{ attributes.addClass('container') }}>
{% for item in items %}
{# NB! notation: loop.index is 1 start, loop.index0 is zero start #}
<div class="row {{ loop.index0 is odd ? 'zebra-odd' : 'zebra-even' }}">
<div{{ item.attributes }}>{{ item.content }}</div>
</div>
{% endfor %}
</div>
@NikLP
NikLP / gist:aea66764f7a5173d83bab9338640754d
Last active May 14, 2018 10:09
Reposition dropbutton (multiple) on views table, remove spurious(?) padding/margin - CSS, Drupal 8
.view-VIEWNAME td .dropbutton-multiple {
display: flex;
justify-content: center;
align-items: center;
padding: 0 !important;
margin: 0 !important;
}
@NikLP
NikLP / theme.theme
Created May 5, 2017 10:11
Sacrilegious's way to get forms in templates for D8
/**
* Provides suggestions for different parts, so that blocks are properly loaded
*
* @see hook_theme_suggestions_alter()
*
* @param array $suggestions
* @param array $variables
* @param string $hook
*/
@NikLP
NikLP / paragraph--PARA-NAME.html.twig
Last active April 3, 2017 17:54
Drop-in paragraph template to display fields as bootstrap accordian
{% extends "paragraph.html.twig" %}
{% block paragraph %}
<div{{ attributes.addClass('panel-group') }} id="accordion-{{ paragraph.id() }}" role="tablist">
{% for field in content %}
{% set field_instance_items = [] %}
{% for key, field_instance in field if key|first != '#' %}
{% set field_instance_items = field_instance_items|merge([field_instance]) %}
{% endfor %}
{% if field_instance_items is not empty %}
/**
* Implements hook_theme_suggestions_HOOK_alter().
* Courtesy @nicrodgers
*/
function MYTHEME_theme_suggestions_block_alter(array &$suggestions, array $variables) {
if (isset($variables['elements']['content']['#block_content'])) {
$block_content = $variables['elements']['content']['#block_content'];
$suggestions[] = 'block__' . $block_content->bundle();
}
@NikLP
NikLP / paragraph--field.html.twig
Created March 24, 2017 16:26
Paragraph field extraction
// paragraph text field actual text
{{ paragraph.field_title.value }}
// paragraph link field uri - internal:/ style (useless?)
{{ paragraph.field_link.0.uri }}
// content has this value - is processed, not validated (good in some cases)
{{ content.field_link[0]['#url'] }}
// paragraph image field uri
@NikLP
NikLP / menu--yourmenu.html.twig
Last active February 24, 2017 15:13
Item links of a single Drupal menu, in adjacent columns, using Bootstrap.
{# for a generic answer to this problem, see:
http://stackoverflow.com/questions/19836567/bootstrap-3-multi-column-within-a-single-ul-not-floating-properly
Note that this example has been tailored to a single level menu of plain links; modify a copy of the core
menu.html.twig with the changes below to keep your menu as per core.
#}
{{ menus.menu_links(items, attributes, 0) }}
{% if items %}
{%
set menu_classes = [
'list-unstyled row',