Verified Commit 5de749b8 authored by Théodore Biadala's avatar Théodore Biadala
Browse files

Issue #3468180 by john.oltman, sonfd, acbramley, smustgrave, tobiasb, nicxvan,...

Issue #3468180 by john.oltman, sonfd, acbramley, smustgrave, tobiasb, nicxvan, catch, larowlan: Undefined array key "view_mode" in block_content_theme_suggestions_block_alter
parent 405ac9da
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -123,7 +123,7 @@ public function themeSuggestionsBlockAlter(array &$suggestions, array $variables
    $block_content = $variables['elements']['content']['#block_content'] ?? NULL;
    if ($block_content instanceof BlockContentInterface) {
      $bundle = $content['#block_content']->bundle();
      $view_mode = strtr($variables['elements']['#configuration']['view_mode'], '.', '_');
      $view_mode = strtr($variables['elements']['content']['#view_mode'], '.', '_');
      $suggestions_new[] = 'block__block_content__view__' . $view_mode;
      $suggestions_new[] = 'block__block_content__type__' . $bundle;
      $suggestions_new[] = 'block__block_content__view_type__' . $bundle . '__' . $view_mode;
+5 −0
Original line number Diff line number Diff line
name: 'Block Content Theme Suggestions Test'
type: module
description: 'Support module for testing.'
package: Testing
version: VERSION
+20 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Support module for testing.
 */

declare(strict_types=1);

use Drupal\block_content\BlockContentInterface;

/**
 * Implements hook_preprocess_block().
 */
function block_content_theme_suggestions_test_preprocess_block(&$variables): void {
  $block_content = $variables['elements']['content']['#block_content'] ?? NULL;
  if ($block_content instanceof BlockContentInterface) {
    $variables['label'] = $block_content->label();
  }
}
+65 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\block_content_theme_suggestions_test\Hook;

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
 * Hook implementations for block_content_theme_suggestions_test.
 */
class BlockContentThemeSuggestionsTestHooks {

  use StringTranslationTrait;

  /**
   * Implements hook_entity_extra_field_info().
   */
  #[Hook('entity_extra_field_info')]
  public function entityExtraFieldInfo(): array {
    // Add an extra field to the test bundle.
    $extra['node']['bundle_with_extra_field']['display']['block_content_extra_field_test'] = [
      'label' => $this->t('Extra field'),
      'description' => $this->t('Extra field description'),
      'weight' => 0,
    ];
    return $extra;
  }

  /**
   * Implements hook_node_view().
   */
  #[Hook('node_view')]
  public function nodeView(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, string $view_mode): void {
    // Provide content for the extra field in the form of a content block.
    if ($display->getComponent('block_content_extra_field_test')) {
      $entity_type_manager = \Drupal::entityTypeManager();
      // Load a block content entity with a known UUID created by test setup.
      // @see \Drupal\Tests\block_content\Functional\BlockContentThemeSuggestionsTest::setUp()
      $block_content = $entity_type_manager->getStorage('block_content')->loadByProperties([
        'uuid' => 'b22c881a-bcfd-4d0c-a41d-3573327705df',
      ]);
      $block_content = reset($block_content);
      $build['block_content_extra_field_test'] = $entity_type_manager->getViewBuilder('block_content')->view($block_content);
    }
  }

  /**
   * Implements hook_theme().
   */
  #[Hook('theme')]
  public function theme(): array {
    // It is necessary to explicitly register the template via hook_theme()
    // because it is added via a module, not a theme.
    return [
      'block__block_content__view_type__basic__full' => [
        'base hook' => 'block',
      ],
    ];
  }

}
+3 −0
Original line number Diff line number Diff line
{{ label }}

<h1>I am a block content template for a specific bundle and view mode!</h1>
Loading