I am trying to make a block that gets the current node via Plugin Contexts.
But my $node
is always null. Can anyone tell me why?
I am placing the block with the context module if that makes a difference?
<?php
namespace Drupal\my_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Header' block.
*
* @Block(
* id = "header",
* admin_label = @Translation("Header"),
* context = {
* "node" = @ContextDefinition("entity:node", required = FALSE)
* }
* )
*/
class ProductStickyHeader extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
$node = $this->getContextValue('node');
// $node is always null!
return $build;
}
}
Many thanks!