I want to render product content type in a custom block with a custom view mode. I have a field product_teaser in my article content type that reference the product that is I want to render in the custom block using advert view mode
The content type to be rendered: "product"
The view mode to be rendered with: "advert"
The product teaser only contains an image and a buy=now button I can easily place using twig. So placing only the image in the render of the custom block should be enough for this to work But I have been trying to render the image for a while and I could not do it.
This is my first approach to do this, Rendering the whole product advert.
public function build() {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
$view_mode = 'advert';
$product_id = $node->get('field_product_teaser')->getValue()[0]['target_id'];
$product = \Drupal::entityTypeManager()->getStorage('node')->load($product_id);
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
$pre_render = $view_builder->view($product, $view_mode);
$render_output = render($pre_render);
return $render_output;
}
return [];
}
Render output is an empty string for some reason. $pre_render has got view mode and node array having all the product fields, everything looks good except that render($pre_render) return an empty spaces string. This is the error being output from this approach "Argument 1 passed to Drupal\Core\Render\Element::isEmpty() must be of the type array, object given"
My second approach was to get the image URI of the product advert and render it manually
$build= array();
//Gets current node to check for which product to be displayed.
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
$product_id = $node->get('field_product_teaser')->getValue()[0]['target_id'];
$product = \Drupal::entityTypeManager()->getStorage('node')->load($product_id);
$product_advert_url = $product->field_teaser->entity->url();
}
$build['image'] = [
'#theme' => 'image_style',
'#style_name' => 'thumbnail',
'#uri' => $product_advert_url
];
return $build;
This is the error being output from this approach "Class name must be a valid object or a string in Drupal\image\Entity\ImageStyle->buildUri()"
How to fix these errors ??, I prefer the first method because it will be easier if I needed to add anything to the product teaser later.