I would like to programmatically render a poll created with the Poll module, and place it in custom place of my template.

Do you have any idea?

closed as too broad by leymannx, kiamlaluno Dec 6 '18 at 18:18

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


Drupal has a pretty standard way of loading and displaying entities.

Loading entities is like:

$entity_object = \Drupal::service('entity_type.manager')

Getting render arrays is like:

$entity_render_array = \Drupal::service('entity_type.manager')
  ->view($entity_object, 'VIEW_MODE');

If you're trying to render in a different template, e.g. in a node template, you can add the render array to the $variables array in a template_preprocess_node function like:

 * Implements template_preprocess_node().
function MY_THEME_preprocess_node(&$variables) {
  $variables['my_entity_render_array'] = $entity_render_array;

Then just render in your template like {{ my_entity_render_array }}


Here is step by step insctuction how to get and render polls programaticaly

// Here we try to get poll entity with id 1
$poll_entity = \Drupal::entityTypeManager()->getStorage('poll')->load(1);

// Create view builder
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('poll');

// Using view builder to prepare our poll to be rednered
$full_output = $view_builder->view($entity);

// Here we make render of our entity
$poll_html_output = render($full_output);
  • 2
    Hey, you shouldn't really render things in php like this, you should render the render array in twig. – sonfd Dec 6 '18 at 17:24

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