I have different view blocks on my front page and they contain number of contents such as images, articles, news etc.

I've managed to get the block list and the block object for each item in that list but how do I fetch the nodes of a view block and generate a link to it programmatically?

$blocks = \Drupal::entityTypeManager()->getStorage('block')->loadByProperties(['theme' => $theme->getname(), 'region' => 'content']);
$block_list = array_keys($blocks);

$firstblock = $block_list[0]; // views_block__latest_image_galleries_block_1
// fetch the node list and create the link for first node for $firstblock here.

1 Answer 1


Since this is a view block, you might be better of directly accessing the view:

$results = views_get_view_result('my_view', 'block1')

Where block1 is the Views display ID. Leave it blank to utilize the default display.


$node = $results[0];
$url = $node->toUrl()->toString();
$link = \Drupal::l('My link', $node->toUrl());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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