I'm looking to create a block that renders differently, depending on the content (metadata, really) of the node in which it is embedded.
The “Title” block is a good example of what I'm trying to do, I think. When you put “Page title” in a section (from the Structure menu), it displays the title of the node being displayed.
I'd like to do something similar. The end goal is to read the workflow state data from the node in order to provide large visual clues to readers that the document they are reading is approved/unapproved/et cetera.
There are some other similar ends to which I may put this technique as well, provided I figure it out.
Here's what I have:
/**
* Provides a 'Workflow Block'.
*
* @Block(
* id = "workflow_tag_block",
* admin_label = @Translation("Workflow Block"),
* )
*/
class WorkflowTagBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $node;
public function __construct(array $configuration, $plugin_id, $plugin_definition, $node) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->node = $node;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get /*... something here, I think. I need this to be the node I'm rendering. */
);
}
/**
* {@inheritdoc}
*/
public function build() {
// do things with $this->node;
return [
'#markup' => $this->t('Here Be Dragons! '),
];
}
}
Obviously, there's an important line I don't have complete in there. The most obvious attempt would be: $container->get('request_stack')->getCurrentRequest()->get('node')
. But that returns the WorkflowTagBlock object itself, not the primary content node.
I'm aware that multiple nodes might be rendered on the screen at once (for example, in a list), so I can't just try to snag the node id from the uri or anything like that.
This is done in other places, so surely there's a way to do it. How?