can this Module work with Views module? It would be a pretty cool feature!

Comments

jonskulski’s picture

Status: Active » Closed (works as designed)

It would be The Killer Feature. Unfortunately, to address this in an all-encompassing general way is not easy or efficient. (but I am open to ideas!).

The problem is there now say, 'okay we have a new node now find all the views that node, a part of it or use to display some other content.' So we would end up having to on update or inserts for: taxonomy, comments, users, nodes generate all views html and store them.

In specific cases, this is not a hard problem and I will write up an example to give some direction.

leovarg’s picture

what about for an specific view page! with the path "/frontage", how it should work? i was trying to modify the existing live_update_blog module replacing the path "blog/all"... with "frontpage" But i was not able to make it work.

lelizondo’s picture

Category: feature » support
Status: Closed (works as designed) » Active

any example I could use?

lelizondo’s picture

This is what I've done to make it work (somehow) with views.

First, create a view, let's make it simple, so choose 'Node' in 'Row Style'. I don't know if this is going to work with fields. For filters I have two filters, 'Published' and 'Node Type = Youtube'. Youtube is just a simple content type with videos.

Don't create a page, the default display is enough. Call your view 'vcall', that's how I called it, just remember you'll be using this name later.

Next, you'll have to create two modules, the first one is very simple, all you have to do is implement hook_menu and vcall_page.

I'm calling my module 'vcall', let's see the code of vcall.module:

<?php
// $Id$

function vcall_menu() {
  $items = array();

  $items['vcall'] = array(
    'title' => 'Views Callback',
    'description' => 'Listing of Views Callback.',
    'page callback' => 'vcall_page',
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

function vcall_page() {
  drupal_set_title('Hello World');
  
  $output = '';
  $output .= 'This is a view';
  $output .= views_embed_view('vcall', 'default');
  
  return $output;
}
?>

We are only defining a new menu item, 'vcall', when visiting 'http://example.com/vcall' we'll be loading the 'vcall_page' function and this function will load the 'vcall' view via views_embed_view() (Are you still with me? If not, read this paragraph again :)

Enable the module and visit http://example.com/vcall you should see some content.

Finally, we'll be creating another module, 'live_update_vcall', this one will do the magic. Let's see the code, I actually took it from the blog module.

<?php

/**
 * Implementation of hook_enable
 **/
function live_update_vcall_enable() { 
  // enable our menu_altering
  menu_cache_clear_all();
}

/** 
 * Implementation of hook_menu_alter
 * We're altering the default page callback from the vcall module, remember the page callback was vcall_page
 **/
function live_update_vcall_menu_alter(&$items) { 
  // tell the menu system about our overrides
  $items['vcall']['page callback'] = 'live_update_vcall_page';
}

/**
 * Wrapper for vcall_page_last() to add live_update js
 * There are 2 very important things you should notice, the 'target' inside the $settings array, 
 * it has to be the same one as the one in $output, and the $output needs the real callback from our vcall.module,
 * in our case vcall_page
 **/
function live_update_vcall_page() { 
  // Load live_update js
  $settings = array(
    'target' => '.live-update-vcall-page .node',
    'method' => 'before',
    'effect' => 'fade',
  );

  live_update_initialize('vcall', $settings);

  // Return normal blog page with target div wrapper
  $output = '<div class="live-update-vcall-page">';
  $output .= vcall_page();
  $output .= '</div>';

  return $output;
}

/**
 * On insert of a youtube node, we tell live_update about our new content
 * This is the tricky part, I don't really know if this will work with more than one node type being shown in the view. 
 * As you can see, the first if() is checking if the $node->type == 'youtube' (remember this is my content type), 
 * I think (we should be calling the $view object or something like that but I haven't tried that yet). 
 **/
function live_update_vcall_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { 
  if ($node->type == 'youtube' && $node->status) {  
    switch ($op) { 
      case 'insert': 
        // Use default settings

        $html = node_view(node_load(array('nid' => $node->nid)));
        live_update_update_content('vcall', $html); //
        break;

      case 'update': 
        // Target existing node 
        $settings = array(
          'target' => '#node-'. $node->nid,
          'method' => 'replaceWith', 
        );

        // Update content
        $html = node_view(node_load(array('nid' => $node->nid)));
        live_update_update_content('vcall', $html, $settings);
        break;
    } 
  }
}
?>

Enable the module and then try it.

Finally, if anyone has a better and simpler idea, please let us know, and please, if I'm wrong please correct me..

mbasfour’s picture

subscribing

Hany Albeshry’s picture

Thank you..I have tried it and it works, any idea how to implement this in views with row style: fields?

mbasfour’s picture

i want a views integration that let the views change it's content when there is new content i mean :

when a new node is added to the view or deleted from the view the view updated automatically without refreshing the page

i need this with the default functionality to live update to refresh the node content when i edit it .

Nathaniel’s picture

You might want to try changing this line:

<?php
$html = node_view(node_load(array('nid' => $node->nid)));
?>

To something like this:

<?php
$html = views_embed_view('name_of_view');
?>