eddy147
eddy147

Reputation: 4983

Drupal: retrieve data from multiple node types in views 2?

...or, in other words, how to create a simple join as I would do in SQL?

Suppose I want the following information:

Just as an example:

His full name is in a (content profile) node type 'name_and_address' and his hobbies are in 'hobbies'.

In SQL, I would link them together by node.uid. I've seen a bit about using relationships, but that goes with user-node-refs. I just want the same user from one content-type and the other.

Now how could I get his name and his hobbies in 1 view?

Upvotes: 0

Views: 1679

Answers (3)

mikewaters
mikewaters

Reputation: 3806

You could use views_embed_view() in your template files to manually specify where they appear (and by extension render one view right below another). You could override this function in a custom module (modulename_embed_view($name, $display_id)) in order to selectively edit what data is allowed out to the page.

Ex):

function modulename_embed_view($name, $display_id) {
  if (strcmp($_GET['q'], 'node/123') === 0) {
    $view = views_get_view($name);
    $view2 = views_get_view('second view');
    $output = $view['some element'] . $view2['element'];
  }
  return $output;
}

I know that this is very much a hack - I just wanted to show how one might use php to manually render and modify views in your template files.

Upvotes: 0

AndrewL
AndrewL

Reputation: 136

Look at the relationships section of the view. This allows you to relate (ie join) different types of content (ie tables). It's not especially intuitive to someone used to SQL, but this video explains much of it. http://www.drupalove.com/drupal-video/demonstration-how-use-views-2s-relationships

Upvotes: 0

Jeremy French
Jeremy French

Reputation: 12167

There is a how to here does this do the job?

If not...

Views can be extended with custom joins, filters etc. If you are lucky there will be a module for this already. Some modules even provide their own views plugins.

You can write your own views plugins, although the documentation is a little fragmented.

The other thing that should be noted is that views isn't always the answer. Sometimes writing a custom query and display handler will do what you want with much less hassle.

Upvotes: 2

Related Questions