1

How can I programmtically create a View on node(s) that displays the content from a specific query?

For example I would like to display the following:

$args = array(':nid' => $node->nid);
$results = db_query('SELECT USER_ID FROM USER INNER JOIN node ON USER.OFFICE_ID =
  node.title AND node.nid = :nid', $args);

Essentially, I would like to grab the 'nid' from the current node and I would like the View to display all 'users' associated with the current node, as the query would return.

I have been able to achieve this through a custom module. But I have been requested to use Views instead.

EDIT:

I've done what Johnathan Elmore suggested and got the View to display the correct information if I manually enter the 'nid' in the 'Preview with contextual filters:' space. However, if I go to the node itself, it will not display the query content.

For example:

I have nid == 123 with url alias 'office-123', if I enter 123 in 'Preview with contextual filters:', it will display the correct query information. However, if I go to 'mysite/office-123' nothing is displayed.

How can I get this View to display when I go to the node url?

  • bump for edit... – chlong Apr 26 '12 at 18:42
4

You can create a View block to show on the nodes, and use a contextual filter (Content: Nid) in the View to figure out which node to reference.

Then you can display the related user (user reference field?) for that node in the block.

In case you are using pathauto to auto alias your node URL's, you can setup the Contextual Filter field (Content: Nid) to determine the Nid from the URL:

contextual filter for referencing specific node

| improve this answer | |
  • johnathan, thanks for the help, but would you know of a way to programmatically add my sql query to a view without using the views ui? I have 200+ nodes I need to do this for and doing all of them by hand is out of the question – chlong Apr 24 '12 at 18:32
  • You should be able to use this answer to create a block which will work automatically for each nodes. It will derive the nid from the node's URL and generate a list of users that pertain to that node. Are you trying to programmatically change the view as it's genererated? My answer basically generates a View that is works equivalent to the SQL you gave. – Johnathan Elmore Apr 24 '12 at 20:45
  • If I may, you seem to have a very poor understanding of what Views does, and how it does it. I think you could save yourself A LOT of hassle reading the documentation or a good tutorial about Views. It would lead you do a greater level of abstraction, and factorize your code to make it more efficient and easy to maintain and extend. That's the whole point of the API. No offence meant : if you're able to make what you did with code, you'll find it easier with Views. – Countzero Apr 25 '12 at 8:31
  • @JohnathanElmore, I tried your suggest and played around with the options quite a bit, but I am unable to get the View block to work correctly. More specifically, the query seems to be empty with your options. When you say contextual filters do you mean it under 'Advanced' or do you mean the line with 'Preview with contextual filters:'? – chlong Apr 25 '12 at 15:56
  • You configure the contextual filters first under the Advanced area. Then to test the results you enter in something into the Preview contextual filters box, otherwise the query won't run. In your case you would enter a node id or URL path to the node – Johnathan Elmore Apr 25 '12 at 16:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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