0

I want to create an archive page for a custom content type. Essentially list them all on a single page.

I understand that I need to create a module, and implement hook_block_info() and hook_block_view(). What I don't know how to do is to retrieve content so I can loop it out. In wordpress there is query_posts($post_type = mycustomcontenttype). Is there an equivalent in Drupal 7?

I do not want to use the Views module for this.

3
  • 2
    Why you don't want use Views module for it ?
    – yvan
    Commented Aug 26, 2011 at 15:35
  • @cbaigorri - hey i think you have to use the views module for this. I came from working in wordpress; so i understand your need to work that way. You have to do it the drupal way; you can't write Wordpress code in Drupal. Also, there really isn't a way (without a lot of hacking) around it in drupal without using views. And if you have to rework this later you'll end up using views anyways.
    – chrisjlee
    Commented Aug 26, 2011 at 16:52
  • I have nothing against the Views module per se, I just thought perhaps I was missing something obvious as this task seems pretty straight forward.
    – cbaigorri
    Commented Aug 26, 2011 at 20:02

2 Answers 2

1

If you're looking to retrieve $node objects and aren't afraid of formatting the info yourself, node_load_multiple() probably comes closest to solving your problem. The $conditions parameter is supposed to be deprecated, but you could use it to specify content type. If you want to use node_load_multiple() in the manner recommended by the API page, I believe you would just use a simple db_query() to retrieve the node ids (nids) of all content with a given type:

$nids = db_query('SELECT nid FROM {node} WHERE type = my_content_type');

Then you could iterate through the results to create an array of nids to pass to node_load_multiple().

$nid_array = array();
foreach($nids as $nid){
  $nid_array[] = $nid;
}
node_load_multiple($nid_array);

From there you're going to have to iterate through the $node objects returned to create your archive page. Depending on whether you're using path aliases, you might have to retrieve the alias for each node.

drupal_lookup_path('alias',"node/".$node->nid);

All that said, I would do this in Views in a heartbeat.

2
  • Thanks for the walkthrough. I'm starting to understand why most people are using Views.
    – cbaigorri
    Commented Aug 26, 2011 at 20:22
  • Yeah, Drupal makes it easy to perform db queries, but Views makes it even easier to query data and output it in the format that you want. If you're worried about the efficiency of Views, there is an option to output the actual SQL query Views uses. You can even try to change the query.. However, I don't think that's a concern for your case.
    – areynolds
    Commented Aug 26, 2011 at 21:03
0

For such queries, there is the EntityFieldQuery class, and the code to use is similar to the following one:

$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'mycustomcontenttype')
  ->propertyCondition('status', 1)
  ->execute();

$nodes = entity_load('node', array_keys($entities['node']));
return node_view_multiple($nodes, 'teaser');

Instead of entity_load() you can use node_load_multiple() passing the second argument passed to entity_load().

When showing a list of nodes to user, always filter the nodes basing on their "status" field, except in the case the user has the permission of viewing unpublished nodes; in any case, you should check if the user has that permission.
As third-party modules could restrict the permission to view a node to specific users, instead of depending from the value of the "status" field, you should use node_access('view') to verify the user has the permission to see the node, before to visualizing it.

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.