Skip to content

Instantly share code, notes, and snippets.

View sachbearbeiter's full-sized avatar
❤️

Sachbearbeiter sachbearbeiter

❤️
View GitHub Profile
@sachbearbeiter
sachbearbeiter / Blocks.md
Created April 19, 2024 10:24 — forked from davidjguru/Blocks.md
Drupal 8 programmatic solutions

Render custom blocks

$bid = 'myblock';
$block = \Drupal\block_content\Entity\BlockContent::load($bid);
$render = \Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block);

Render plugin blocks

$block_manager = \Drupal::service('plugin.manager.block');
@sachbearbeiter
sachbearbeiter / country-list-german.html
Last active June 7, 2023 20:29
German country SELECT element in HTML
<select name="country">
<option value="DE" selected>Deutschland</option>
<option value="AT">Österreich</option>
<option value="CH">Schweiz</option>
<optgroup label="A">
<option value="AF">Afghanistan</option>
<option value="EG">Ägypten</option>
<option value="AX">Åland</option>
<option value="AL">Albanien</option>
<option value="DZ">Algerien</option>
@sachbearbeiter
sachbearbeiter / image.html.twig
Last active September 27, 2021 09:05
D8: Theming examples: Images: How to print individual image values and suppress width and height via the image.html.twig template ... *First results of struggling through Drupal 8 - better solutions are probably out there! **Drupal 8 is still Beta - improvements through API modifications are possible!
{#
/**
* @file
* Default theme implementation of an image.
*
* Available variables:
* - attributes: HTML attributes for the img tag.
* - style_name: (optional) The name of the image style applied.
*
* @see template_preprocess_image()
@sachbearbeiter
sachbearbeiter / html.tpl.php
Created February 10, 2019 16:21 — forked from pascalduez/html.tpl.php
Drupal 7 — Move $scripts at page bottom
<!DOCTYPE html>
<html<?php print $html_attributes; ?>>
<head>
<?php print $head; ?>
<title><?php print $head_title; ?></title>
<?php print $styles; ?>
<?php print $head_scripts; ?>
</head>
<body<?php print $body_attributes;?>>
<?php
// Load the currently logged in user.
global $user;
// Check if the user has the 'editor' role.
if (in_array('editor', $user->roles)) {
// do fancy stuff
}
?>
@sachbearbeiter
sachbearbeiter / theme.php
Created April 8, 2015 08:55
D8: Theming examples: Theme: Global theme variables - get path to the active theme
<?php
\Drupal::theme()->getActiveTheme()->getPath();
?>
@sachbearbeiter
sachbearbeiter / theme
Last active August 29, 2015 14:14
D8: Theming examples: Theme: Additional suggestion for content block templates: taking the bundle type into account. *First results of struggling through Drupal 8 - better solutions are probably out there! **Drupal 8 is still Beta - improvements through API modifications are possible!
<?php
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function MYTHEME_theme_suggestions_block_alter(array &$suggestions, array $variables)
{
$block = $variables['elements'];
$blockType = $block['#configuration']['provider'];
if ($blockType == "block_content") {
@sachbearbeiter
sachbearbeiter / theme.php
Created February 2, 2015 08:42
D8: Theming examples: Theme: New more specific suggestions for the image.html.twig template ... *http://drupal.stackexchange.com/questions/137652/override-image-formatter-html-twig/141340#141340 **Drupal 8 is still Beta - improvements through API modifications are possible!
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function MYTHEME_theme_suggestions_image_formatter_alter(array &$suggestions, array $variables) {
$entity = $variables['item']->getEntity();
$field_name = $variables['item']->getParent()->getName();
$suggestions[] = 'image_formatter__' . $entity->getEntityTypeId() . '__' . $entity->bundle() . '__' . $field_name;
}
@sachbearbeiter
sachbearbeiter / debug-renderable-array.twig
Created January 31, 2015 15:42
I'm not sure if I'm being too obvious but 'item' is a placeholder some "renderable array". Depending on which template you are in that variable name will change. Within any template you can get a list of the available variables when you have twig_debug turned on by means of {{ dump(_context|keys) }} which if I remember right kint() will be nicer…
{{ dump(_context|keys) }}
@sachbearbeiter
sachbearbeiter / page.html.twig
Created January 30, 2015 10:18
D8: Get the current user ID in theme file and set a variable via MYTHEME_preprocess_page to use it in page.html.twig.
{# isAdmin? #}
{% if UserID == '1' %}
{{ kint() }}
{% endif %}