Blocks can have different view modes and it would be helpful if this module supported that granularity for templates.

Comments

nerdstein created an issue. See original summary.

chris burge’s picture

Take a look at this article: https://www.phase2technology.com/blog/big-bad-layout

The author provides a code sample that would be very easy to implement here:

/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function mine_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  // Provide suggestion for block templates by custom block type.
  if (!empty($variables['elements']['content']['#block_content'])) {
    $block = $variables['elements']['content']['#block_content'];
    // Add `block--BLOCK-TYPE.html.twig`.
    $suggestions[] = 'block__' . $block->bundle();
    $view_mode = $variables['elements']['#configuration']['view_mode'];
    if (!empty($view_mode)) {
      // Add `block--BLOCK-TYPE--VIEW-MODE.html.twig`.
      $suggestions[] = 'block__' . $block->bundle() . '__' . $view_mode;
    }
  }
}
chris burge’s picture

Assigned: Unassigned » chris burge
chris burge’s picture

Assigned: chris burge » Unassigned
Status: Active » Needs review
StatusFileSize
new1.94 KB

Patch attached. I re-organized the code a bit to better accommodate the addition of the view-mode suggestion code. I also added some comments.

This patch assumes that all BlockContentInterface instances have a view mode. If this isn't the case, let me know, and I'll add a check.

The original code assumes that 'block__block_content' may be not be present as a suggestion. I left this in, even thought I'm not sure where this would occur off the top of my head.

nerdstein’s picture

@Chris Burge -- thank you. I did a code review of the patch and it looks good. We can work on testing this soon. Here are some steps:

1. Enable the module and load the patch
2. Create a custom block type and a new view mode
3. Create a corresponding template for that block type and view mode
4. Verify the markup shows up upon rendering

chris burge’s picture

@nerdstein - I'm going to submit another patch that also updates README.md.

chris burge’s picture

StatusFileSize
new3.07 KB

Patch attached.

daggerhart’s picture

Status: Needs review » Reviewed & tested by the community

Had a bit of trouble testing this because none of the core D8 mechanisms for rendering blocks with alternate view modes seem to render blocks like regions do.

I was ultimately able to get this working with the "Full" view mode, which proves that it works. RTBC

chris burge’s picture

If #3067045: Add classes to blocks gets committed first, then we'll need to update this patch to also add a class for the view mode.

  • nerdstein committed a8dc236 on 8.x-1.x authored by Chris Burge
    Issue #3018307 by Chris Burge, nerdstein, daggerhart: Support for View...
nerdstein’s picture

Status: Reviewed & tested by the community » Fixed

Fixed - thank you both so much.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.