I try to render an HTML data from my node view mode but unfortunately, I have some small progress. I did some searching and modification on the others code Here. I have this now.
$nid = 145; $entity_type = 'node'; $view_mode = 'pdf'; $view_builder = \Drupal::service('entity.manager')->getViewBuilder($entity_type); $storage = \Drupal::service('entity.manager')->getStorage($entity_type); $node = $storage->load($nid); $build = $view_builder->view($node, $view_mode); var_dump($build); die();
But when I run it I got this error
TypeError: Argument 1 passed to Drupal\Core\Routing\CurrentRouteMatch::getRouteMatch() must be an instance of Symfony\Component\HttpFoundation\Request, null given, called in C:\wamp64\www\executive-coatings\docroot\core\lib\Drupal\Core\Routing\CurrentRouteMatch.php on line 127 in Drupal\Core\Routing\CurrentRouteMatch->getRouteMatch() (line 99 of core\lib\Drupal\Core\Routing\CurrentRouteMatch.php).
As I did step by step checking I got that just issue is with
$build = $view_builder->view($node, $view_mode);
What is wrong with this code? Am I in the right way to render HTML data from a view mode?
The Idea behind this is to get node data according to a specific view mode, then render the data to HTML format and use that HTML data in a PDF function to generate the related PDF file.
I simply use this code in one of my custom modules, that is ecc_pdf module.
as you can see I simply set a valid ID to load valid data for the test process.
My node type name is
this is my full view mode list
I use Drupal 8
For checking that I have any issue with other codes or not, I create a new basic site and apply this part of code inside a custom module but I get the same error again.
This is my new custom test module code, This is my full module code for test.mudole file.
use Drupal\node\Entity\Node; use Drupal\taxonomy\Entity\Term; use Drupal\views\ViewExecutable; use Drupal\views\Views; use Drupal\Core\Render; $nid = 1; $entity_type = 'node'; $view_mode = 'teaser'; $builder = \Drupal::service('entity_type.manager')->getViewBuilder($entity_type); $storage = \Drupal::service('entity.manager')->getStorage($entity_type); $node = $storage->load($nid); $build = $builder->view($node, $view_mode); $output = \Drupal::service('renderer')->render($node); var_dump($node);
This my info file
name: ECC TEST description: For creating the custom TEST package: Custom type: module version: 1.0 core: 8.x
As I said I create just a new site, and I don't have any other file in my module, I just have .mudole and .info.yml file that I mention above. And the Article node that I test just is Drupal default.
This is the full error that logged for me