简体   繁体   中英

Loading latest nodes in Drupal sidebar via Ajax (facebook-like)

I would like to have a facebook-like dynamic latest nodes loader in Drupal sidebar working in JQuery. Each time new node is created, the users would be able to see it in a list (similar to facebook) without refreshing the page. Any advice, tutorial links, etc. will be appreciated.

How familiar are you with module development? I will suggest the easier, less instant way. If you really wanted it to be updated only when a new node was added, it would take much more work. If someone wants to describe that, they are more than welcome to.

Really all you need to write is a block ( hook_block ) which inputs some javascript which:

  • Sends an AJAX query to a page your module defines (say /nodes/new)
  • Displays the data in the block (via the ajax callback).
  • Uses set_timeout javascript call to make the call again every so often.

The page will be defined in a hook_menu call with 'type' => MENU_CALLBACK and calling a custom function (my_module_nodes_new).

function my_module_nodes_new() {
  $output = '';
  $result = db_query("SELECT nid FROM {node} WHERE status = 1 LIMIT 5 ORDER BY `created` DESC");
  while($nid = db_fetch_object($result) {
    $node = node_load($nid->nid);
    // Theme the information here and add it to $output
  }

  print $output; //IMPORTANT - do not "return" $output or it will be inside your theme
}

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM