Problem/Motivation
Drupal 9.4.8.
Just learning how to use events. Following tutorials online.
Getting error:
Call to undefined method Drupal\core_event_dispatcher\Event\Entity\EntityViewEvent::setBuild()
when I use setBuild() in my entity view subscriber class file.
Steps to reproduce
Followed these instructions:
Drupal 8/9: Using Hook Event Dispatcher instead of Drupal Hooks
This is my EntityViewSubscriber.php:
<?php
namespace Drupal\sbn\EventSubscriber;
use symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\hook_event_dispatcher\HookEventDispatcherInterface;
/**
* Entity view subscriber class.
* From here: https://medium.com/@jayprakash01/drupal-8-9-using-hook-event-dispatcher-instead-of-drupal-hooks-cf5b6bbbb98a
*/
class EntityViewSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public static function getsubscribedEvents() {
return [
HookEventDispatcherInterface::ENTITY_VIEW => 'entityView'
];
}
/**
* Our callback function entityView().
*/
public function entityView($event) {
$build = $event->getBuild();
$build['new_renderable'] = [
'#type' => 'markup',
'#markup' => 'Hello India! Drupal 9 has been launched!!'
];
$build = $event->setBuild($build);
#kint($event); die();
}
}
Getting error that setBuild() is an undefined method.
What am I doing wrong?
Error: Call to undefined method Drupal\core_event_dispatcher\Event\Entity\EntityViewEvent::setBuild() in Drupal\sbn\EventSubscriber\EntityViewSubscriber->entityView() (line 33 of modules/custom/sbn/src/EventSubscriber/EntityViewSubscriber.php).
Drupal\sbn\EventSubscriber\EntityViewSubscriber->entityView(Object, 'hook_event_dispatcher.entity.view', Object)
call_user_func(Array, Object, 'hook_event_dispatcher.entity.view', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'hook_event_dispatcher.entity.view') (Line: 45)
Drupal\hook_event_dispatcher\Manager\HookEventDispatcherManager->register(Object) (Line: 103)
Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler->Drupal\hook_event_dispatcher\{closure}(Array, Object, Object, 'full') (Line: 76)
Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler->Drupal\hook_event_dispatcher\{closure}(Object, 'hook_event_dispatcher') (Line: 106)
Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler->invokeAllWith('entity_view', Object) (Line: 83)
Drupal\hook_event_dispatcher\HookEventDispatcherModuleHandler->invokeAll('entity_view', Array) (Line: 289)
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: 772)
Drupal\Core\Render\Renderer->doCallback('#pre_render', Array, Array) (Line: 363)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 201)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 241)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 564)
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: 174)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 81)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 19)
Drupal\sbn\Middleware\Redirect->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
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: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 709)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)