0

this may be a trivial question, but I have no clue to how to get this working. I have this theme 'nexus'. When try the hook

function nexus_preprocess_block(&$variables) 

in the file nexus.theme, it works,

but function nexus_block_view_alter(&$data, $block) doesn't.

Should I put it in a module? I tried creating a module using hello_world example, and put in the the module's controller but no avail.

Any hint would be much helpful.

  • 1
    Try with function nexus_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) – berramou Nov 7 '18 at 16:20
  • 1
    Yes that would need to go into a .module file. Did you enable your hello_world module and then flush all caches? and also change the function name to hello_world_block_view_alter if "hello_world" is your module name? – Leigh Nov 7 '18 at 16:21
  • ah..this worked..somehow I ended up with a controller as given in the helloworld example and it didn't worked out...now I put it as .module file and prefixed the hook with module name, it worked. Thanks. – Code Tree Nov 7 '18 at 16:30
1

In drupal 8 the declaration of hook_block_view_alter changed from hook_block_view_alter(&$data, $block) (Drupal7) to hook_block_view_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) (Drupal 8).
For more info check hook_block_view_alter Documentation page.

hook_block_view_alter be better in custom module not theme, see guide to building basic Drupal 8 module, and flush all caches.

0

While normally the alter hooks are invoked also when they are implemented from the theme used to render the page, there are cases where this doesn't happen. The method that invokes hook_entity_view(), hook_entity_view_alter(), hook_ENTITY_TYPE_view(), and hook_ENTITY_TYPE_view_alter(), EntityViewBuilder::buildMultiple(), invokes only module hooks.

  $this->moduleHandler()->invokeAll($view_hook, [
    &$build_list[$key],
    $entity,
    $display,
    $view_mode,
  ]);
  $this->moduleHandler()->invokeAll('entity_view', [
    &$build_list[$key],
    $entity,
    $display,
    $view_mode,
  ]);

  // ...

  // Allow modules to modify the render array.
  $this->moduleHandler()->alter([$view_hook, 'entity_view', ], $build_list[$key], $entity, $display);

In a theme, you can implement hook_preprocess_block(), but the other hooks require a module.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.