Skip to content

Instantly share code, notes, and snippets.

@dsnopek
Last active September 20, 2018 15:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dsnopek/b4e0bc096b216d63a96d17559484dc51 to your computer and use it in GitHub Desktop.
Save dsnopek/b4e0bc096b216d63a96d17559484dc51 to your computer and use it in GitHub Desktop.
Drupal 8 block annotation with context example
/**
* @Block(
* id = "my_block",
* context = {
* "node" = @ContextDefinition("entity:node")
* }
* )
*/
class MyBlock extends BlockBase implements ContextAwarePluginInterface {
/**
* {@inheritdoc}
*/
public function build() {
// The render array.
$build = [];
$node = $this->getContextValue('node');
// @todo Do something with the $node to affect the $build array
return $build;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment