I'm trying to create a function similar to views_embed_view() but which produces a render array instead of a string. I've poked around in the views source and done some experiments to create one but I reach a dead end inside $view->preview(). (I'm not sure how to dereference $this->display_handler->preview().) Any help appreciated.

Comments

dawehner’s picture

Category: support » feature

Views doesn't support it at the moment.

I doubt you will be able to implement this without breaking views and many plugins.

keithm’s picture

Category: feature » support

Thanks for your quick response. I appreciate it.

chx’s picture

Category: support » feature

I suspect the category revert was not intentional.

merlinofchaos’s picture

Do you want something along the lines of this:

array(
  '#type' => 'view',
  '#view' => 'viewname',
  '#arguments' => array(x, y, z),
  '#other modifiers' => 'foo',
);

If so, that's really more like a render element that could be defined in hook_element info that would then render into a view, yes?

keithm’s picture

merlinofchaos: The context is a D7 port of the viewfield module, which exposes a view as an entity field.

Your suggestion would work but the emitted render array strikes me as too high-level since there's very little that could be done to modify it, which to me is the primary reason to expose data as renderable in the first place.

In my original request, I was hoping to get Views to produce something like (assuming a list display):

array(
  '#theme' => 'item_list',
  '#items' => array(
    array(
      '#type' => 'link', 
      '#title' => 'title 1', 
      '#href' => 'node/1',
    ),
    array(
      '#type' => 'link', 
      '#title' => 'title 2', 
      '#href' => 'node/2',
    ),
    array(
      '#type' => 'link', 
      '#title' => 'title 3', 
      '#href' => 'node/3',
    ),
  ),
);
dawehner’s picture

You could write a style plugin which gives you something like this but it will be really hard to implement this as part of views itself.

merlinofchaos’s picture

Status: Active » Closed (won't fix)

Yeah, what you want in #5 is going to be very very difficult, because it would require a pretty low level redesign of how views are built. And I'm not sure I want to rebuild Views that way, because I'm not quite sold on what we've done with render arrays in general. There are parts of it I like, but parts of it are maddening.

plach’s picture

@merlinofchaos:

Would having at least the main building blocks of the views-view.tpl.php template as keys of a render array be a good compromise? This would allow to move attachments and the view content around the page, for instance. I guess this would cover at least the major use cases.

onequad’s picture

I am also looking for a way to move attachment's output to another part of a page in hook_page_alter(). Any suggestion would be appreciated. ^^

strae’s picture

Hi guys, i need to get the view's renderable array becose I need to insert it into an array that get rendered throught drupal_render.. there is a way?

jmlavarenne’s picture

Issue summary: View changes

The way have done this is to add the rendered view to a #markup element in a render array.

$node->content['renderered_view']['#markup'] = views_get_view('view_name')->render();