0

In Drupal 9, I am trying to use hook_ENTITY_TYPE_view_alter() to modify the body field of a custom block.

However, I don't know how to access the data in the $build array to change it. This is a partial dump of the $build array. I want to modify the HTML links in the body field.

array(8) {
  ["#block_content"]=> object(Drupal\block_content\Entity\BlockContent)#4946 (30) {
    ["theme":protected]=> NULL 
    ["values":protected]=> array(17) { 
      ["id"]=> array(1) { ["x-default"]=> string(1) "4" } 
      ["revision_id"]=> array(1) { ["x-default"]=> string(1) "4" } 
      ["type"]=> array(1) { ["x-default"]=> string(5) "basic" } 
      ["uuid"]=> array(1) { ["x-default"]=> string(36) "f0c17429-584a-4221-995c-c00f4c41c714" } 
      ["langcode"]=> array(1) { ["x-default"]=> string(2) "en" } 
      ["revision_user"]=> array(1) { ["x-default"]=> NULL } 
      ["revision_created"]=> array(1) { ["x-default"]=> string(10) "1632243859" } 
      ["revision_log"]=> array(1) { ["x-default"]=> NULL } 
      ["revision_default"]=> array(1) { ["x-default"]=> string(1) "1" } 
      ["isDefaultRevision"]=> array(1) { ["x-default"]=> string(1) "1" } 
      ["status"]=> array(1) { ["x-default"]=> string(1) "1" } 
      ["info"]=> array(1) { ["x-default"]=> string(11) "Add Content" } 
      ["changed"]=> array(1) { ["x-default"]=> string(10) "1632457827" } 
      ["default_langcode"]=> array(1) { ["x-default"]=> string(1) "1" } 
      ["revision_translation_affected"]=> array(1) { ["x-default"]=> string(1) "1" } 
      ["reusable"]=> array(1) { ["x-default"]=> string(1) "1" } 
      ["body"]=> array(1) {
        ["x-default"]=> array(1) {
          [0]=> array(3) {
            ["value"]=> string(652) "[…]" 
            ["summary"]=> string(0) "" 
            ["format"]=> string(11) "sbn_toolbar"
          }
        }
      }
    }
  }
}

I can't seem to locate a straightforward answer to this through my searches. The closest I've come to what I am trying to do is this code.

$build['content']['#markup'] = Markup::create('Your text here');

It fails with this error.

sbn_block_content_view_alter(Array, Object, Object) (Line: 539)
Drupal\Core\Extension\ModuleHandler->alter('block_content_view', Array, Object, Object) (Line: 305)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 29)
Drupal\block_content\BlockContentViewBuilder->viewMultiple(Array, 'full', NULL) (Line: 17)
Drupal\block_content\BlockContentViewBuilder->view(Object, 'full') (Line: 187)
Drupal\block_content\Plugin\Block\BlockContentBlock->build() (Line: 105)
Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray->onBuildRender(Object, 'section_component.build.render_array', Object) call_user_func(Array, Object, 'section_component.build.render_array', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'section_component.build.render_array') (Line: 90)
Drupal\layout_builder\SectionComponent->toRenderArray(Array, ) (Line: 86)
Drupal\layout_builder\Section->toRenderArray(Array) (Line: 317)
Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildSections(Object) (Line: 276)
Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildMultiple(Array) (Line: 340)
Drupal\Core\Entity\EntityViewBuilder->buildComponents(Array, Array, Array, 'full') (Line: 282)
Drupal\Core\Entity\EntityViewBuilder->buildMultiple(Array) (Line: 239)
Drupal\Core\Entity\EntityViewBuilder->build(Array) call_user_func_array(Array, Array) (Line: 101)
Drupal\Core\Render\Renderer->doTrustedCallback(Array, Array, 'Render #pre_render callbacks must be methods of a class that implements \Drupal\Core\Security\TrustedCallbackInterface or be an anonymous function. The callback was %s. See https://www.drupal.org/node/2966725', 'exception', 'Drupal\Core\Render\Element\RenderCallbackInterface') (Line: 781)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 372)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 200)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 241)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent{closure}() (Line: 573)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 242)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 132)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object) call_user_func(Array, Object, 'kernel.view', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 163)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 706)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

6
  • 1
    The error message is missing. It's only the stack trace with your hook at the top. Probably a syntax error. If you want to modify the body field then first try to dump $build['body'].
    – 4k4
    Sep 24 at 6:36
  • var_dump($build['body]) gives me the same output as var_dump($build) does above. My question is: how to I modify the body value in $build? Sep 24 at 17:10
  • It would help to see more than a single line of that hook_ENTITY_TYPE_view_alter() implementation. It would also help to see the error message, not the stack trace, as @4k4 said.
    – apaderno
    Sep 26 at 11:41
  • Also, the output of var_dump($build) has been truncated to the part that isn't much helpful.
    – apaderno
    Sep 26 at 11:57
  • My question was edited. I just need an example of using hook_entity_type_view_alter() to modify the body of a custom block. Sep 27 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.