Skip to content

Instantly share code, notes, and snippets.

{# Check if a field has a value before printing it #}
{% if node.field_name.isEmpty == false %}
<p>{{ content.field_name }} </p>
{% endif %}
{# Check if block is empty #}
{% if block('block_name')|trim is not empty %}
<div>{% block block_name %}{% endblock %}</div>
{% endif %}
@rikki-iki
rikki-iki / drupal-preprocess.theme
Last active February 7, 2023 01:47
Useful preprocess theme functions (may use deprecated php)
<?php
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Drupal\Core\Link;
/**
* Implements hook_preprocess_page() for page.html.twig.
@rikki-iki
rikki-iki / drupal-theme-suggestions.theme
Last active February 7, 2023 01:47
Useful theme suggestions (may use deprecated php)
<?php
use Drupal\block\Entity\Block;
use Drupal\block_content\BlockContentInterface;
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function HOOK_theme_suggestions_page_alter(array &$hooks, array &$vars): void {
$constrained = [
@rikki-iki
rikki-iki / theme_info.yml
Last active February 7, 2023 01:53
Example Drupal 8 info file for removing base theme CSS or Libraries
# Extend an existing library:
# Ensures overrides are only added when the toolbar is available.
libraries-extend:
toolbar/toolbar.menu:
- my_theme/drupal
# Replace or remove entire libraries;
# Un-comment each library to exclude it's CSS, JS and Dependencies from being loaded.
@rikki-iki
rikki-iki / social-svg.html
Created March 17, 2016 08:08
Example markup for social media links using inline SVG
<ul class="footer__social">
<li><a class="button--svg" href="https://www.facebook.com/" target="_blank">
<svg xmlns="http://www.w3.org/2000/svg" width="46" height="46" viewBox="0 0 46 46" role="img" aria-labelledby="facebook-icon">
<title id="facebook-icon">Facebook</title>
<path fill="#3C599A" d="M18.896 20.12h1.758v-1.708c0-.753.02-1.915.566-2.635.576-.762 1.368-1.28 2.73-1.28 2.218 0 3.15.316 3.15.316l-.438 2.605s-.73-.212-1.417-.212c-.684 0-1.296.245-1.296.93v1.985h2.803l-.194 2.547h-2.61v8.84h-3.297v-8.84h-1.758V20.12z"/>
<path class="button--svg__border" fill="#E5E5E5" d="M23 3c11.027 0 20 8.972 20 20 0 11.027-8.973 20-20 20-11.028 0-20-8.973-20-20C3 11.972 11.972 3 23 3m0-2C10.85 1 1 10.85 1 23s9.85 22 22 22 22-9.85 22-22S35.15 1 23 1z"/>
</svg>
</a></li>
<li><a class="button--svg" href="https://twitter.com/" target="_blank">
<svg xmlns="http://www.w3.org/2000/svg" width="46" height="46" viewBox="0 0 46 46" role="img" aria-labelledby="twitter-icon">