I use a combination of Block plugins and panels to populate the layouts of the display modes of various content types.
Here's an example:
class MyBlockPlugin extends BlockBase
{
/**
* {@inheritdoc}
*/
public function build()
{
$node = \Drupal::routeMatch()->getParameter('node');
$profile_pic = ImageStyle::load('350x300')->buildUrl($node->field_image->entity->getFileUri());
return [
'#theme' => 'mytwigtheme',
'#name' => $node->title->value,
'#headshot' => $profile_pic
];
}
}
It certainly works but I'm wondering if this is a bad path to travel because I'll have multiple block plugins loading and accessing the same node. I suppose I could access the node with a preprocess block hook but I'm assuming that will load for every block as well.
If the performance cost is trivial then I'm not too worried about redundancy. Is my method okay? If not, what would be a better way for a block plugin to access the data of the current node it appears on?
Update: Just another thought. Is me using a twig template making this a non issue as long as it's been loaded before?