Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
DanLaufer / Drupal 8 - Get a field from a paragraph reference in node preprocessing
Last active April 13, 2023 12:38
Drupal 8 - Get a field from a paragraph in template_preprocess_node()
if($node) {
// we have a node, so use headline in hero, or fall back to node title
if($node->field_hero_section && $node->field_hero_section->getValue() && $node->field_hero_section->getValue()[0]) {
$paragraph = $node->field_hero_section->getValue()[0];
$paragraph_entity = \Drupal\paragraphs\Entity\Paragraph::load( $paragraph['target_id'] );
return $paragraph_entity->field_headline->value;
} else {
return $node->getTitle();
}
}
@DanLaufer
DanLaufer / Drupal 8 - Set and Truncate Title tag and meta tag head items in Drupal 8
Last active October 19, 2018 12:16
Drupal 8 - PHP - Set and Truncate Title tag and meta tag head items in Drupal 8
/**
* Gets a title from the field_headline first and falls back on the node
* title, then truncates the site name (/admin/config/system/site-information).
* - Stays under 58 characters, as that is best for SEO
* - Used for <title> and <meta name="title" content="xxxxxx">.
* - Will truncate the page title, and always add the full ' | site-name'.
* - Example output: <meta name="title" content="Homepage | MyCompany, Inc.">
* @param string $raw_title
* @param string $site_name
* @return string truncated and joined title string up to 58 characters