Loading core/modules/block_content/src/Hook/BlockContentHooks.php +1 −1 Original line number Diff line number Diff line Loading @@ -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; Loading core/modules/block_content/tests/modules/block_content_theme_suggestions_test/block_content_theme_suggestions_test.info.yml 0 → 100644 +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 core/modules/block_content/tests/modules/block_content_theme_suggestions_test/block_content_theme_suggestions_test.module 0 → 100644 +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(); } } core/modules/block_content/tests/modules/block_content_theme_suggestions_test/src/Hook/BlockContentThemeSuggestionsTestHooks.php 0 → 100644 +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', ], ]; } } core/modules/block_content/tests/modules/block_content_theme_suggestions_test/templates/block--block-content--view-type--basic--full.html.twig 0 → 100644 +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
core/modules/block_content/src/Hook/BlockContentHooks.php +1 −1 Original line number Diff line number Diff line Loading @@ -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; Loading
core/modules/block_content/tests/modules/block_content_theme_suggestions_test/block_content_theme_suggestions_test.info.yml 0 → 100644 +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
core/modules/block_content/tests/modules/block_content_theme_suggestions_test/block_content_theme_suggestions_test.module 0 → 100644 +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(); } }
core/modules/block_content/tests/modules/block_content_theme_suggestions_test/src/Hook/BlockContentThemeSuggestionsTestHooks.php 0 → 100644 +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', ], ]; } }
core/modules/block_content/tests/modules/block_content_theme_suggestions_test/templates/block--block-content--view-type--basic--full.html.twig 0 → 100644 +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>