Skip to content

Instantly share code, notes, and snippets.

View NikLP's full-sized avatar

NikLP NikLP

  • Nottingham, UK
  • 16:06 (UTC -12:00)
  • X @NikLP
View GitHub Profile
@NikLP
NikLP / gist:4a2b3aa5359017359b29
Created October 22, 2015 15:16
Profile2 "profile page" title set using entity_metadata_wrapper in hook_entity_view
/*
* Implementation of hook_entity_view
*/
function helpermodule_entity_view($entity, $type, $view_mode, $langcode) {
// we're viewing an entity
// check if it's a profile2 type
// - if there's more than one profile2, check for that too!
// - and the view mode is 'page' (not 'full', for profile2 it seems)
if ($type == 'profile2' && $entity->type == 'member' && $view_mode == 'page') {
# If your site can be accessed both with and without the 'www.' prefix, you
# can use one of the following settings to redirect users to your preferred
# URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
#
# To redirect all users to access the site WITH the 'www.' prefix,
# (http://example.com/... will be redirected to http://www.example.com/...)
# uncomment the following:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
@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',
@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
/**
* 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--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 %}
@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 / 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 / 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>
.view-VIEWNAME .form--inline .form-item {
display: inline;
float: none;
}
.view-VIEWNAME .form--inline .form-actions {
display: inline-block;
}