I've tried to theme a portion of a node by adding some values in to it using
- hook_theme and a twig template
I've used hook_node_view to add a few variables and then I have included the theme specified in hook_theme and printed it in twig template.
Here is the code; nothing is working like nothing.
function content_statistics_node_view(EntityInterface $node, EntityDisplay $display, $view_mode) {
$node_count = some function called;
foreach ($node_count as $key => $value) {
$node_statistics = array(
'#theme' => 'content_statistics_list',
'#letter' => $value->letter,
'#word' => $value->word,
'#sentence' => $value->sentence,
);
}
}
$node->content['content_statistics_list'] = array(
'#markup' => drupal_render($node_statistics)
);
}
/**
* Implements hook_theme().
*/
function content_statistics_theme($existing, $type, $theme, $path) {
return array(
'content_statistics_list' => array(
'variables' => array('letter' => NULL, 'word' => NULL, 'sentence' => NULL),
'template' => drupal_get_path('module', 'content_statistics') . '/templates/' . 'content-statistics-list',
),
);
}
In content-statistics.html.twig, I have printed {{ letter }}
but it is not showing anything on the node page. It is also causing an error.
Warning: Attempt to modify property of non-object in content_statistics_node_view()
\Drupal::service('renderer')->renderRoot()
instead of deprecateddrupal_render()
.