NickOpris
NickOpris

Reputation: 559

How to get attached files outputted by views for given node id (programatically) - Drupal

I am working with a module. I have a node id. That node id is a page and it has 2 or more view blocks on it. The results of views are lists of nodes with file attachments.

I need to get all the files attached to the nodes outputted by the view on this particular page.

I work in a module file and I can supply the nid of type page where these views sit.

I could not find much help so I decided to ask for what would be the best way to approach this.

I am new to Drupal so I will appreciate a great explanation on how to approach this.

Upvotes: 0

Views: 824

Answers (1)

AKS
AKS

Reputation: 4658

If I understand your question correctly, you need to print the View with arguments set. Grab the view's machine name and the display id (In Drupal 6 Views 2, hover your mouse pointer on the displays list (Block, Page, etc) and in the URL, you will see something like "block_4" or "page_1". That's the display ID. In D7 Views 3, you can set the display ID in advanced field set in right pane).

If the View's machine name is "my_view" and display id is "block_1", you can print the view in node.tpl.php or anywhere using the below code.

<?php
print views_embed_view('my_view', 'block_1', array(arg(1)));
?>

arg(1) contains the node ID. If you are putting the code in a node.tpl.php or in a it's variation, arg(1) is always the node ID. If it goes to a page.tpl.php, make sure that arg(1) is numeric at least.

In the View, you will have to add an argument (Contextual filter, in D7 words) to make the View get the argument correctly. You can also set the contextual filter to get the argument automatically from the URL as well.

Upvotes: 0

Related Questions