I am learning Drupal 7 for my new project.
Here is what I want to do
- create a new content type called 'vide开发者_StackOverflow中文版o'
- create a new module called 'video'
- make http://domain.com/video accessible.
- when a user access /video, then execute a query that queries video rows from video table (created by 'video' content type'), then display queries with a custom template.
I have learned how to do #1 ~ #3, but I am a little bit confused with #4.
let's assume that my custom template's filename is 'video.tpl.php'
in the video.tpl.php, do I write php functions to query video rows?
I don't think that is a good practice. Instead, I want to write a module and call a function in the module when the video.tpl.php is loaded.
How do I do it?
For 4, you first need to create a module, implement hook_menu(), define a menu item for 'videos' with a page callback. If you don't know how to do that, there are likely already a lot of questions about that). Inside the page callback, you need to do 3 things.
Load the nids, something like
$nids = db_query("SELECT nid FROM {node} WHERE type = 'video' ORDER BY created DESC")->fetchCol();
Load the nodes.
$nodes = node_load_multiple($nids);
Build them.
return node_view_multiple($nodes);
But again, you should only do this if you want to learn the API. Views will all of this do for you, you just need to click it together.
You should use views
Views is definitely the way to go. you could write a module, but it would be like re-inventing the wheel. Learn Views and you will use it again and again.... tutorials:
a views tutorial
Another views tutorial
精彩评论