1

I'm looking for a module that makes it possible to attach an existing view to a specific node. I've looked at eva, but it seems only to allow connecting a view to all entities of a specific content-type. I wrote my own module, that can do this, and it works, but I would like to use a module that is well tested and maintained. Does anyone have any suggestions?

* EDIT *

I have installed the views reference module and this seems to be what I want, but when I do print_r($content) in my node--type.tpl.php file, the only thing related to this field is the following:

[field_attached_view] => Array (
                        [und] => Array (
                            [0] => Array (
                               [view_id] => products_view:first_display
                               [arguments] => sample/all/jeans
                             )
                         )
                     )

I have managed to load and display the view by something like this:

  $view_id = $node->field_attached_view[$node->language][0]['view_id'];
  $parts = explode(':',$view_id);

  $args = explode('/',$node->field_attached_view[$node->language][0]['arguments']);
  $view = views_get_view($parts[0]);
  $view->set_display($parts[1]);
  $view->set_arguments($args);
  $view->execute();
  echo $view->render();

But, there must be a cleaner way of doing this?

Thanks in advance.

5
  • It is very strange because normally your view should return some results .. Try to do this: hide($content['field_attached_view']); print render($content); print render($content['field_attached_view']);
    – nourcy
    Dec 13, 2011 at 14:19
  • @nourcy - Tried that and still the view isn't displayed=( Dec 13, 2011 at 14:29
  • Ok a last thing and if it still doesn't work I don't know: Try to remove your argument to see all results of your view
    – nourcy
    Dec 13, 2011 at 14:34
  • @nourcy - Tried to remove the arguments completly, with no luck. Very strange, if you look at my above code I use the supplied arguments and the machine_name & display_name to load the view and then everything works, so it seems like the call to render doesn't do anything with the data in field_attached_view. Dec 13, 2011 at 14:41
  • And if you inspect your html with firebug or something there is nothing?
    – nourcy
    Dec 13, 2011 at 14:47

3 Answers 3

3

You can use the View reference module.

Defines a field type View reference which creates a relationship to a Views display and allows the view to be displayed as the content of the field.

5
  • This seems to be exactly what I was looking for, I can now attach a view from within my nodes edit form, but then in my node--type.tpl.php file how do I render the view? Dec 13, 2011 at 9:28
  • View reference is a field so you can configure where you want to see your view. For theaming your view, use the normal way in your view configuration.
    – nourcy
    Dec 13, 2011 at 10:18
  • I have a view that displays fine when going to it's normal path. Now I have connected this view to one of my nodes, but it doesn't show up when going to the nodes url? In the nodes template I use echo render($content); to display it's output. What am I doing wrong here? Dec 13, 2011 at 11:25
  • You have to check if: your view returns some results ; your reference field is displayed in your content type display (admin/structure/types/manage/page/display). try to look what you have in your $content before rendering with a dpm for example (Devel module) and tell us after
    – nourcy
    Dec 13, 2011 at 13:42
  • I have updated my post. Dec 13, 2011 at 13:55
2

Just create a view as a block. In the block settings you can configure it to display it on "Only the listed pages" and display in the region you want.

1
  • Yes, that would work I guess, but I don't want editors to have to go in en editing the view each time they would like to connect a view to a specific node. I don't think I would like editors touching the views at all. They should only have the choice to attach a view from within the nodes edit form or similar. Dec 12, 2011 at 22:49
1

You can use Context:

  1. Create a Block Display in your View.
  2. Create a new context.
  3. Use the specific node path as a Rule for that Context. Example: node/123
  4. "Drag & Drop" the Block Display you created in step #1 to one of the regions available in your node page.

You could also do more complex things with Views, such as attaching a View to all the nodes of a particular content type when a user with a certain role is active, just to name an example. As a plus, Contexts are exportable, and you can package and deploy them to your production sites using Features. If you decide to use Context you will be using a method widely used by the larger Drupal community.

1
  • I will try this in the morning, thank you very much. Dec 12, 2011 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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