Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
DanLaufer / Drupal 8 - Load Blocks
Created October 19, 2018 17:30
Drupal 8 - Load Blocks
// Load block and render
$socialNetworksBlock = \Drupal\block_content\Entity\BlockContent::load(7);
$variables['social_networks'] = \Drupal::entityTypeManager()
->getViewBuilder('block_content')
->view($socialNetworksBlock);
@DanLaufer
DanLaufer / Drupal 8 - Get Nodes, Many Ways (load, route, php, from db)
Created October 19, 2018 17:27
Drupal 8 - Get Nodes, Many Ways (load, route, php, from db)
// Load a node
$listing_page_node = \Drupal\node\Entity\Node::load($node_id);
// Get current node from the route
\Drupal::routeMatch()->getParameter('node')
\Drupal::routeMatch()->getParameter('node')->getType()
// Get node in PHP and get field
if(\Drupal::routeMatch()->getParameters()->has('node')) {
$current_node = \Drupal::routeMatch()->getParameter('node');
@DanLaufer
DanLaufer / Drupal 8 - Template Suggestions
Created October 19, 2018 17:24
Drupal 8 - Template Suggestions
//PAGE Suggestions
$suggestions[] = 'page__path_alias__' . formatPathAlias($path_alias);
//NODE Suggestions
$node = \Drupal::request()->attributes->get('node');
if (!is_null($node)) {
$node_alias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/'.$node->id());
$view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'node__path_alias__' . formatPathAlias($path_alias) . '__' . $view_mode;
@DanLaufer
DanLaufer / Drupal 8 - Get path alias
Created October 19, 2018 17:20
Drupal 8 - Get path alias
$current_path = \Drupal::service('path.current')->getPath();
\Drupal::service('path.alias_manager')->getAliasByPath($current_path);
@DanLaufer
DanLaufer / RedirectNodesSubscriber.php
Created October 19, 2018 17:16
Drupal 8 - Module to redirect content type nodes to the homepage
// file at src/EventSubscriber/RedirectNodesSubscriber.php
<?php
/**
* @file
* Contains \Drupal\redirect_nodes\EventSubscriber\RedirectNodesSubscriber
*/
namespace Drupal\redirect_nodes\EventSubscriber;
use Drupal\Core\Url;
@DanLaufer
DanLaufer / Drupal 8 - Truncate title before suffix
Last active October 19, 2018 17:10
Drupal 8 - Truncate <title> and <meta title> before suffix
function mytheme_preprocess_html(&$variables) {
// truncate <title> to 58 characters, including the brand
$title_length = strlen($variables['head_title']['title']);
$ti_str_limit = 58;
$title_suffix = ' | My Company, LLC.';
$suffix_length = strlen($title_suffix);
$allowable_length = $ti_str_limit - $suffix_length;
$allowable_length_w_ellipse = $allowable_length - 3;
if (strpos($variables['head_title']['title'], $title_suffix) == false) {
@DanLaufer
DanLaufer / Drupal 8 - Conditionally remove an attached library
Last active October 19, 2018 17:05
Drupal 8 - Conditionally remove an attached library
function mytheme_page_attachments_alter(array &$attachments) {
if (in_array('core/jquery', $attachments['#attached']['library'])) {
$index = array_search('core/jquery', $attachments['#attached']['library']);
unset($attachments['#attached']['library'][$index]);
}
}
@DanLaufer
DanLaufer / Drupal 8 - Get length of paragraph reference in twig
Created October 19, 2018 16:51
Drupal 8 - Get length of paragraph reference in twig
{{ (content.field_benefits | field_value)[0]['#paragraph'].get('field_accordion') | length }}
@DanLaufer
DanLaufer / Drupal 8 - Get index of paragraph entity in a reference field
Last active October 19, 2018 16:48
Drupal 8 - Get index of paragraph entity in a reference field
{% set index_of_paragraph = content.field_summary['#object']._referringItem.getName() %}
@DanLaufer
DanLaufer / Drupal 8 - Drill down from Node to paragraph reference to paragraph entity to field
Created October 19, 2018 16:46
Drupal 8 - Drill down from Node to paragraph reference to paragraph entity to taxonomy reference field
function example_preprocess_node(&$variables) {
$node = $variables['node'];
$paragraph = $node->field_paragraph_example->entity;
$nested_paragraph = $paragraph->field_another_paragraph_ref->entity;
$term = $nested_paragraph->field_example_term_ref->entity;
// The following simple syntax works when you just need the first value of the field:
$textfield_value_on_term = $term->field_exampletext_on_term->value;
}