0

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?

MORE INFO

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 safety_report this is my full view mode list enter image description here

I use Drupal 8

PHP 7.2

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

  • 1
    Changing view mode to a core mode like teaser does the code work ? – GiorgosK Jul 14 at 6:58
  • 1
    The code is OK. Please add more details in which context you use it and add a full error message. As the question is written now it is impossible to connect the error message to the code. – 4k4 Jul 14 at 8:05
  • 1
    Change entity.manager to entity_type.manager. I'm not sure if that's your problem, but entity.manager has been deprecated, so either way it's worth the change. – Jaypan Jul 14 at 11:23
  • 1
    As I said, use entity_type.manager. And if that doesn't fix your problem, then your problem is somewhere else, as the above code is fine. – Jaypan Jul 14 at 21:39
  • 1
    @Nasser where do you put your code? – Jonh Jul 15 at 5:28
2

Your problem is here:

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);

You have dumped your code raw into a file. You cannot do that - your code is being called before Drupal has bootstrapped. You need to put your code into a function some hook that is called after Drupal has bootstrapped. I don't know what you're trying to accomplish, so I can't give any more info than that.

  • 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. – Nasser Jul 15 at 6:41
  • 1
    The problem isn't in what you are trying to do it, it's when are you trying to do it? If the code you had provided were to work, it would be run on every page load. But it won't work because Drupal is not yet bootstrapped. – Jaypan Jul 15 at 6:43
  • In Drupal, code in modules goes in to functions called 'hooks'. Hooks are functions that are called after certain events. You cannot dump code raw into a file, as Drupal has not loaded yet, so your code will not work. You can read about hooks here: drupal.org/docs/7/creating-custom-modules/… – Jaypan Jul 15 at 6:48
  • I don't know why you just put code in file module :D. – Jonh Jul 15 at 6:59
  • 1
    Some hook you can test your snippet code like hook_preprocess_html, hook_page_attachments_alter, every request your code will be run. – Jonh Jul 15 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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