Skip to content

Instantly share code, notes, and snippets.

@Abby805
Created June 5, 2019 13:11
Show Gist options
  • Save Abby805/30dddfb743b6ccf472a402081b99e532 to your computer and use it in GitHub Desktop.
Save Abby805/30dddfb743b6ccf472a402081b99e532 to your computer and use it in GitHub Desktop.
Common D8 Preprocess Functions
<?php
/**
* @file
* Example of some commonly used functions.
*/
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;
use Drupal\node\Entity\Node;
/**
* Implements hook_preprocess_html().
*/
function hook_preprocess_html(&$variables) {
// If is homepage.
$variables['attributes']['class'][] = \Drupal::service('path.matcher')
->isFrontPage() ? 'frontpage' : '';
// Node type class.
$variables['attributes']['class'][] = isset($variables['node_type']) ? 'page-node-type--' . $variables['node_type'] : '';
// Logged in class.
$variables['attributes']['class'][] = $variables['logged_in'] ? 'logged-in' : 'logged-out';
}
/**
* Implements hook_preprocess_node().
*/
function hook_preprocess_node(&$variables) {
$node = $variables['node'];
$type = $node->getType();
$mode = $variables['view_mode'];
$variables['attributes']['class'][] = 'node-type-' . $type;
$variables['attributes']['class'][] = 'node-mode-' . $mode;
$variables['attributes']['class'][] = 'node-type-' . $type . '-mode-' . $mode;
// Example of how to build a link the "Drupal" way
// You probabbly get the value in the quotes from a field
// It might need another URL::fromXYZ function, depending on input
// See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/class/Url/8.6.x
$my_url = URL::fromUri('https://www.some-url.com/');
// Set the text or markup you want inside the link
$my_text = t('Click Me!');
// Set any attributes you want as an array
$link_options = [
'attributes' => [
'target' => '_blank',
'class' => ['btn', 'btn--large'],
],
];
$my_url->setOptions($link_options);
// See https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Link.php/function/Link%3A%3AfromTextAndUrl/8.5.x
$my_full_link = Link::fromTextAndUrl($my_text, $my_url)->toString();
}
/**
* Implements hook_preprocess_media().
*/
function hook_preprocess_media(&$variables) {
$mediaType = $variables['media']->bundle();
$variables['attributes']['class'][] = 'media-entity';
$variables['attributes']['class'][] = 'media-type-' . $mediaType;
}
/**
* Implements hook_preprocess_menu().
*/
function hook_preprocess_menu(&$variables) {
if (!isset($variables['attributes']['class'])) {
$variables['attributes']['class'] = [];
}
if (!isset($variables['menu_name'])) {
return;
}
$variables['attributes']['class'][] = 'menu--' . $variables['menu_name'];
}
/**
* Implements hook_preprocess_field().
*/
function hook_preprocess_field(&$variables) {
$field_name = $variables['field_name'];
// Add a class specifying what field we're looking at.
if (!isset($variables['attributes']['class'])) {
$variables['attributes']['class'] = [];
}
$name_class = 'field-name--' . $field_name;
$variables['attributes']['class'][] = $name_class;
// Add a class to field labels.
if (!isset($variables['title_attributes']['class'])) {
$variables['title_attributes']['class'] = [];
}
$variables['title_attributes']['class'][] = 'field-label';
$variables['title_attributes']['class'][] = 'field-label--' . $field_name;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function hook_form_search_block_form_alter(&$form, $form_state) {
// Adds a placeholder value to the search form.
$form['keys']['#attributes']['placeholder'] = t('Search');
}
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function hook_theme_suggestions_field_alter(array &$suggestions, array $variables) {
// Set custom template suggestions for fields.
switch ($variables['element']['#field_name']) {
case 'field_something':
case 'field_something_else':
$suggestions[] = 'field__something_or_other';
break;
}
}
/**
* Implements template_preprocess_block().
*/
function hook_preprocess_block(&$variables) {
// Custom block type helper classes.
if (!isset($variables['elements']['content']['#block_content'])) {
return;
}
$bundle = $variables['elements']['content']['#block_content']->bundle();
$bundle_class = str_replace('_', '-', $bundle);
if (!isset($variables['attributes']['class'])) {
$variables['attributes']['class'] = [];
}
$variables['attributes']['class'][] = Html::cleanCssIdentifier('block--bundle-' . $bundle_class);
}
@Abby805
Copy link
Author

Abby805 commented Jun 5, 2019

@maxstarkenburg
Copy link

Thanks, Abby! This might be more obvious if I had a easily available dev environment to test out dsm on at the moment, but I'm curious about the parts reading ->bundle(). Is that some kind of function or method available to certain variables or parts of the render arrays (that I'm guessing returns a list of media types or blocks in the above cases)?

@Abby805
Copy link
Author

Abby805 commented Jun 6, 2019

Hey @maxstarkenburg! Yep, it's pretty much exactly as you said - ->bundle() is available on I believe any entity, and will return the machine name of the bundle (i.e. content type, media type, block type, etc.). The official explanation of it is at https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityInterface.php/function/EntityInterface%3A%3Abundle/8.2.x, though it's pretty sparse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment