Skip to content

Instantly share code, notes, and snippets.

@gorkdesign
Forked from rikki-iki/drupal-preprocess.theme
Created September 30, 2022 08:59
Show Gist options
  • Save gorkdesign/0102c023854b79cc56803d2d1c52bab4 to your computer and use it in GitHub Desktop.
Save gorkdesign/0102c023854b79cc56803d2d1c52bab4 to your computer and use it in GitHub Desktop.

Revisions

  1. @rikki-iki rikki-iki revised this gist Dec 20, 2017. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions drupal-8-preprocess.theme
    Original file line number Diff line number Diff line change
    @@ -35,10 +35,14 @@ function THEME_preprocess_page(&$vars) {

    if ($node->getType() === 'article') {
    // Add created date when viewing an article node.
    $vars['created_date'] = format_date($node->getCreatedTime(), 'medium');
    $vars['created_date'] = \Drupal::service('date.formatter')->format($node->getCreatedTime(), 'medium'),

    // Add the author when viewing an article node.
    $vars['author'] = \Drupal::entityTypeManager()->getViewBuilder('user')->view($node->getOwner(), 'compact');
    $uid = $node->getOwnerId();
    $vars['author'] = \Drupal::entityTypeManager()->getViewBuilder('user')->view($uid, 'compact');
    // Or just their name.
    $user = \Drupal\user\Entity\User::load($uid);
    $vars['author'] = $user->getDisplayName();

    // Render node header_image.
    $vars['header_image'] = \Drupal::entityTypeManager()->getViewBuilder('node')->view($node, 'header_image');
  2. @rikki-iki rikki-iki revised this gist Dec 20, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions drupal-8-preprocess.theme
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,7 @@ function THEME_preprocess_page(&$vars) {
    // Do things based on route.
    $route_match = \Drupal::routeMatch();
    $current_route = $route_match->getRouteName();
    $current_path = \Drupal::service('path.current')->getPath();

    switch ($current_route) {
    case 'system.401':
  3. @rikki-iki rikki-iki revised this gist Dec 20, 2017. 1 changed file with 33 additions and 0 deletions.
    33 changes: 33 additions & 0 deletions drupal-8-preprocess.theme
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,7 @@ function THEME_preprocess_page(&$vars) {

    // Set a global var we can use when viewing a node.
    $vars['is_node'] = TRUE;
    $vars['type_class'] = 'page-type--' . strtr($node->getType(), '_', '-');

    if ($node->getType() === 'article') {
    // Add created date when viewing an article node.
    @@ -159,4 +160,36 @@ function THEME_preprocess_paragraph(&$vars) {
    */
    function THEME_preprocess_maintenance_page(&$vars) {
    $vars['title'] = t('Temporarily down for maintenance');
    }

    /**
    * Implements hook_form_alter().
    */
    function THEME_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    // Add search placeholder for keywords field.
    if ('views_exposed_form' === $form_id) {
    $view = $form_state->get('view');
    if ($view->id() === 'search') {
    $form['#attributes']['class'][] = 'form__search';
    $form['query']['#attributes']['placeholder'] = t("How can we help you today?");
    }
    }
    }

    /**
    * Implements hook_preprocess_breadcrumb().
    */
    function THEME_preprocess_breadcrumb(&$vars) {
    $route = \Drupal::routeMatch()->getRouteObject();
    $title = \Drupal::service('title_resolver')->getTitle(\Drupal::request(), $route);

    $vars['breadcrumb'][] = [
    'text' => $title,
    ];

    // Adding a dependency on the route actually disables caching for this block
    // but that's what we want, to prevent caching issues.
    CacheableMetadata::createFromRenderArray($vars)
    ->addCacheableDependency($route)
    ->applyTo($vars);
    }
  4. @rikki-iki rikki-iki revised this gist Dec 20, 2017. 1 changed file with 70 additions and 25 deletions.
    95 changes: 70 additions & 25 deletions drupal-8-preprocess.theme
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    use Drupal\node\NodeInterface;
    use Drupal\Core\Template\Attribute;
    use Drupal\Core\Url;

    /**
    * Implements hook_preprocess_page() for page.html.twig.
    */
    @@ -6,14 +10,24 @@ function THEME_preprocess_page(&$vars) {
    $vars['site_name'] = \Drupal::config('system.site')->get('name');
    $vars['site_slogan'] = \Drupal::config('system.site')->get('slogan');

    // Add a toggle for the front page.
    if ($is_front = \Drupal::service('path.matcher')->isFrontPage()) {
    $vars['is_front'] = $is_front;
    // Do things based on route.
    $route_match = \Drupal::routeMatch();
    $current_route = $route_match->getRouteName();

    switch ($current_route) {
    case 'system.401':
    case 'system.403':
    $vars['title'] = t('Access denied');
    break;

    case 'system.404':
    $vars['title'] = t('Page not found');
    break;
    }

    // Do things based on the node.
    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node) {
    if (($node = $route_match->getParameter('node')) && $node instanceof NodeInterface) {

    // Set a global var we can use when viewing a node.
    $vars['is_node'] = TRUE;

    @@ -74,44 +88,75 @@ function THEME_preprocess_node(&$vars) {
    function THEME_preprocess_field(&$vars, $hook) {
    $name = $vars['element']['#field_name'];
    $bundle = $vars['element']['#bundle'];
    $view_mode = $vars['element']['#view_mode'];
    $entity_type = $vars['element']['#entity_type'];
    $field_type = $vars['element']['#field_type'];

    // Ds overrides '#view_mode' to '_custom' but keeps the original in '#ds_view_mode'.
    $view_mode = isset($vars['element']['#ds_view_mode']) ? $vars['element']['#ds_view_mode'] : $vars['element']['#view_mode'];

    if ($name === 'field_suburb') {
    // Add a value to a field
    // Add a custom variable to a field
    $vars['items'][0]['icon_id'] = 'location';

    // Add a class to a field
    $vars['attributes']['class'][] = 'is-suburb';

    // Change it's rendered label
    $vars['label'] = 'My suburb';
    $vars['label'] = t('My suburb');

    // Add a class to each item in a field
    foreach ($vars['items'] as $delta => $item) {
    if ($vars['items'][$delta]['attributes'] instanceof Attribute) {
    $vars['items'][$delta]['attributes']->addClass('meta__type');
    }
    }
    }
    }

    /**
    * Implements hook_preprocess_HOOK() for Block document templates.
    */
    function THEME_preprocess_block(&$vars) {
    $bundle = NULL;
    $block_id = $vars['elements']['#id'];
    $is_front = \Drupal::service('path.matcher')->isFrontPage();
    $provider = $vars['elements']['#configuration']['provider'];
    // These variables are set in HOOK_theme_suggestions_block_alter().
    // @see https://gist.github.com/rikki-iki/6fe41e11ac59b3e1d07e#file-drupal-8-theme-suggestions-theme-L7

    if ($provider === 'block_content' &&
    isset($vars['elements']['content']['#block_content']) &&
    ($block_entity = $vars['elements']['content']['#block_content']) &&
    $block_entity instanceof BlockContentInterface) {
    $bundle = $block_entity->bundle();
    // Pass the block_id through to the content.
    if (isset($vars['attributes']['id'])) {
    $vars['content']['#attributes']['block_id'] = $vars['attributes']['id'];
    }

    // Hide the default homepage content.
    if ($block_id === 'wcc_theme_content' && $is_front) {
    $vars['content'] = [];
    $vars['attributes']['class'] = 'visually-hidden';
    // Pass the region through to the content.
    if (isset($vars['elements']['#region'])) {
    $vars['content']['#attributes']['region'] = $vars['elements']['#region'];
    }

    // Get the value of a field.
    if ($bundle === 'basic') {
    $vars['suburb'] = $block_entity->get('field_suburb')->value;
    $block_id = $vars['attributes']['id'];
    $provider = $vars['elements']['#configuration']['provider'];

    if (isset($vars['elements']['#bundle'])) {
    $bundle = $vars['elements']['#bundle'];

    // Get the value of a field.
    if ($bundle === 'basic') {
    $vars['suburb'] = $block_entity->get('field_suburb')->value;
    }
    }
    }

    /**
    * Implements hook_preprocess_paragraph().
    */
    function THEME_preprocess_paragraph(&$vars) {
    $paragraph = $vars['paragraph'];
    $bundle = paragraph->bundle();
    if ($paragraph instanceof Paragraph && $paragraph->hasField('field_display') && $paragraph->get('field_display')->value === 'title_summary') {
    // Add a class for child displays.
    $vars['featured_display'] = 'featured--child';
    };
    }

    /**
    * Implements hook_preprocess_maintenance_page().
    */
    function THEME_preprocess_maintenance_page(&$vars) {
    $vars['title'] = t('Temporarily down for maintenance');
    }
  5. @rikki-iki rikki-iki revised this gist Jul 14, 2016. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions drupal-8-preprocess.theme
    Original file line number Diff line number Diff line change
    @@ -48,6 +48,24 @@ function THEME_preprocess_node(&$vars) {
    // Get referenced entity (if entity ref field)
    $vars['suburb'] = $node->get('field_suburb')->entity;
    }

    // Make a new link from an entity reference and plain text field.
    // So the editor can customise the link text.
    if ($node->hasField('field_reference') && $node->hasField('field_link_label')) {
    // Get the URL of the referenced node.
    $referenced_url = $node->get('field_reference')->entity->toUrl()->getInternalPath();
    // Get the value of the label field.
    $label = $node->get('field_link_label')->value;
    // Create the link.
    $link = Link::fromTextAndUrl($label,
    Url::fromUserInput(base_path() . $references_url, [
    'attributes' => [
    'class' => ['button'],
    ]
    ]));
    // Create a var to use in the template.
    $vars['link'] = $link->toString();
    }
    }

    /**
  6. @rikki-iki rikki-iki revised this gist Jun 21, 2016. 1 changed file with 23 additions and 0 deletions.
    23 changes: 23 additions & 0 deletions drupal-8-preprocess.theme
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,29 @@ function THEME_preprocess_page(&$vars) {
    // Add sitename and slogan to page.
    $vars['site_name'] = \Drupal::config('system.site')->get('name');
    $vars['site_slogan'] = \Drupal::config('system.site')->get('slogan');

    // Add a toggle for the front page.
    if ($is_front = \Drupal::service('path.matcher')->isFrontPage()) {
    $vars['is_front'] = $is_front;
    }

    // Do things based on the node.
    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node) {
    // Set a global var we can use when viewing a node.
    $vars['is_node'] = TRUE;

    if ($node->getType() === 'article') {
    // Add created date when viewing an article node.
    $vars['created_date'] = format_date($node->getCreatedTime(), 'medium');

    // Add the author when viewing an article node.
    $vars['author'] = \Drupal::entityTypeManager()->getViewBuilder('user')->view($node->getOwner(), 'compact');

    // Render node header_image.
    $vars['header_image'] = \Drupal::entityTypeManager()->getViewBuilder('node')->view($node, 'header_image');
    }
    }
    }

    /**
  7. @rikki-iki rikki-iki revised this gist Jun 21, 2016. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion drupal-8-preprocess.theme
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,12 @@
    /**
    * Implements hook_preprocess_page() for page.html.twig.
    */
    function THEME_preprocess_page(&$vars) {
    // Add sitename and slogan to page.
    $vars['site_name'] = \Drupal::config('system.site')->get('name');
    $vars['site_slogan'] = \Drupal::config('system.site')->get('slogan');
    }

    /**
    * Implements hook_preprocess_node().
    */
    @@ -63,4 +72,5 @@ function THEME_preprocess_block(&$vars) {
    // Get the value of a field.
    if ($bundle === 'basic') {
    $vars['suburb'] = $block_entity->get('field_suburb')->value;
    }
    }
    }
  8. @rikki-iki rikki-iki created this gist Jun 21, 2016.
    66 changes: 66 additions & 0 deletions drupal-8-preprocess.theme
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    /**
    * Implements hook_preprocess_node().
    */
    function THEME_preprocess_node(&$vars) {
    $node = $vars['elements']['#node'];
    $view_mode = $vars['elements']['#view_mode'];
    $type = $node->getType();

    // Check if field has value.
    if ($node->hasField('field_suburb') && !$node->field_suburb->isEmpty()) {
    $vars['attributes']['class'][] = 'has-suburb';

    // Get field value (if normal field)
    $vars['suburb'] = $node->get('field_suburb')->value;

    // Get referenced entity (if entity ref field)
    $vars['suburb'] = $node->get('field_suburb')->entity;
    }
    }

    /**
    * Implements hook_preprocess_field().
    */
    function THEME_preprocess_field(&$vars, $hook) {
    $name = $vars['element']['#field_name'];
    $bundle = $vars['element']['#bundle'];
    $view_mode = $vars['element']['#view_mode'];

    if ($name === 'field_suburb') {
    // Add a value to a field
    $vars['items'][0]['icon_id'] = 'location';

    // Add a class to a field
    $vars['attributes']['class'][] = 'is-suburb';

    // Change it's rendered label
    $vars['label'] = 'My suburb';
    }
    }

    /**
    * Implements hook_preprocess_HOOK() for Block document templates.
    */
    function THEME_preprocess_block(&$vars) {
    $bundle = NULL;
    $block_id = $vars['elements']['#id'];
    $is_front = \Drupal::service('path.matcher')->isFrontPage();
    $provider = $vars['elements']['#configuration']['provider'];

    if ($provider === 'block_content' &&
    isset($vars['elements']['content']['#block_content']) &&
    ($block_entity = $vars['elements']['content']['#block_content']) &&
    $block_entity instanceof BlockContentInterface) {
    $bundle = $block_entity->bundle();
    }

    // Hide the default homepage content.
    if ($block_id === 'wcc_theme_content' && $is_front) {
    $vars['content'] = [];
    $vars['attributes']['class'] = 'visually-hidden';
    }

    // Get the value of a field.
    if ($bundle === 'basic') {
    $vars['suburb'] = $block_entity->get('field_suburb')->value;
    }