From a lot of things I've been reading, it seems Views could accomplish the following, and that might even be the preferred way of going about this? Anyway, after trying hard for a long time I don't seem able to get this happening without some help -

I have a list of links to Audio nodes being generated by Apache Solr Search Integration. On click, any of these links needs to display the Audio node it links to in a right column block without a page refresh.

I have altered search-result.tpl.php, giving the above-mentioned links to Audio nodes an Ajax class and altering their path so they become links to a View I have created which takes a default argument type of PHP Code:

$nid = arg(2);

Testing those altered links, the View is using Ajax successfully as a page display if I manually insert valid node ids as the 3rd argument in the URL via the browser address bar. I created a block display of this View but I can't figure out how to ID the block or how to write proper Drupal javascript to load the view in the block.

I tried adding a div around the $right var in my page.tpl.php and giving it an id. That doesn't seem to be working. Is there some way to target a Views block with an ID so I can tell Javascript where to put the View? Or should I even be trying to use the Views block display rather than a regular block?

I added this script I found to my script.js, and have been trying to figure out if I can alter it for this purpose. But no dice, despite the fact I know the href attribute it is getting is viable:

/** beginning of file **/

if (Drupal.jsEnabled) {
	$(document).ready(function () {
		$('a.AjaxMe').click(function(){
		var AjaxSRC = $(this).attr('href');
		$('#AjaxTarget').empty().load(AjaxSRC + '#AjaxMe');
		return false;
		});
	});
}

The above script actually works after a fashion to just load nodes in a right column block, but it loads all regions of a whole new page into the target div (displaying a page within a page). It also only displays a single field of the node (none of the default fields).

I've been at this for weeks... any help would be very greatly appreciated, especially if anyone could explain exactly (as opposed to generally) what I would need to learn to figure this out myself.

Thanks very much for any input.

Comments

butler’s picture

Status: Postponed (maintainer needs more info) » Active

Removed...

dawehner’s picture

Status: Active » Postponed (maintainer needs more info)

A bump after one day is considered to be a bad behaviour. Please look at the issue queue and the amount of issues coming in every day. You just waited 24h + 2min

AjaxSRC

What does this page returns? Can you show us this code?

butler’s picture

EDIT 20110519 - changed code below to reflect current state of this wannabe module... please note I have junked the entire approach above in favor of creating a module after finding this excellent seeming tutorial by zzadik:

http://viziontech.co.il/node/6

... and I think I have it almost working - there's definitely something still wrong - nothing now happens on clicking the links. I am pretty sure that my callback function is not getting a node id, or is broken in some other way that I can't understand so far - this is my first time doing anything asynchronous so it is probably something dumb.

dynamic_audio_node.module

<?php
// $Id$

/**
* Valid permissions for this module
* @return array An array of valid permissions for the dynamic_audio_node module
*/

function dynamic_audio_node_perm() {
	   return array('access dynamic_audio_node content');
	}


function dynamic_audio_node_menu() {
	 
	$items = array();
		$items['r_col_audio_node/%'] = array(
			'title' => 'Dynamic Audio Node',
			'page callback' => 'dynamic_audio_node_get_by_node_id',
			'access arguments' => array('access dynamic_audio_node content'),
			'type' => MENU_CALLBACK
			);
	 
		return $items;
	}

function dynamic_audio_node_get_by_node_id($nid){
	$viewName = 'most_recent_audio_node_view'; // The name of the view we are going to load
	 
	// Array of arguments we set for the view. Only one argument in our example. your actual view may require additional arguments which you may need to set -
	//	$args = array($nid);

	$displayId = 'block_1'; // The display id of for the view.

	// Call the views_embed_view function to returned themed view output
	$res = views_embed_view($viewName, $displayId, $nid);
	 
	// create a JSON object. The object will contain a property named “audio_node” that will be set with the themed result of the executed view.
	return drupal_json(array('audio_node'=>$res));
	 
	exit;
	}

function dynamic_audio_node_theme() {
   return array(
      'dynamic_audio_node_javascript' => array(
         'arguments' => array(),
      ),
   );  
}
 
function dynamic_audio_node_init() {
  theme('dynamic_audio_node_javascript');
}
 
function theme_dynamic_audio_node_javascript() {
  drupal_add_js(drupal_get_path('module', 'dynamic_audio_node') . '/dynamic_audio_node.js');
}

dynamic_audio_node.js

// $Id$
Drupal.behaviors.dynamic_audio_node = function (context) {
  $('a.AjaxAudioNodeLink:not(.AjaxAudioNodeLink-processed)', context).click(function () {
    // This function will get exceuted after the ajax request is completed successfully
    var updateAudioNode = function(data) {
      // The data parameter is a JSON object. The “audio_node” property is the list of audio_node items that was returned from the server response to the ajax request.
      $('#DivAudioNode').html(data.audio_node);
    }
    $.ajax({
      type: 'POST',
      url: this.href, // Which url should be handle the ajax request. This is the url defined in the <a> html tag
      success: updateAudioNode, // The js function that will be called upon success request
      dataType: 'json', //define the type of data that is going to get back from the server
      data: 'js=1' //Pass a key/value pair
    });
    return false;  // return false so the navigation stops here and not continue to the page in the link
}).addClass('AjaxAudioNodeLink-processed');
}

An Export Of most_recent_audio_node_view - The View I'm Trying To Embed -

$view = new view;
$view->name = 'most_recent_audio_node_view';
$view->description = 'Most Recent Audio Node View';
$view->tag = '';
$view->view_php = '';
$view->base_table = 'node';
$view->is_cacheable = FALSE;
$view->api_version = 2;
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
$handler = $view->new_display('default', 'Defaults', 'default');
$handler->override_option('arguments', array(
  'nid' => array(
    'default_action' => 'default',
    'style_plugin' => 'default_summary',
    'style_options' => array(),
    'wildcard' => 'all',
    'wildcard_substitution' => 'All',
    'title' => '',
    'breadcrumb' => '',
    'default_argument_type' => 'node',
    'default_argument' => '',
    'validate_type' => 'none',
    'validate_fail' => 'not found',
    'break_phrase' => 0,
    'not' => 0,
    'id' => 'nid',
    'table' => 'node',
    'field' => 'nid',
    'validate_user_argument_type' => 'uid',
    'validate_user_roles' => array(
      '2' => 0,
      '3' => 0,
    ),
    'override' => array(
      'button' => 'Override',
    ),
    'relationship' => 'none',
    'default_options_div_prefix' => '',
    'default_argument_fixed' => '',
    'default_argument_user' => 0,
    'default_argument_php' => '',
    'validate_argument_node_type' => array(
      'audio' => 'audio',
      'page' => 0,
      'story' => 0,
    ),
    'validate_argument_node_access' => 1,
    'validate_argument_nid_type' => 'nid',
    'validate_argument_vocabulary' => array(),
    'validate_argument_type' => 'tid',
    'validate_argument_transform' => 0,
    'validate_user_restrict_roles' => 0,
    'validate_argument_php' => '',
  ),
));
$handler->override_option('filters', array(
  'type' => array(
    'operator' => 'in',
    'value' => array(
      'audio' => 'audio',
    ),
    'group' => '0',
    'exposed' => FALSE,
    'expose' => array(
      'operator' => FALSE,
      'label' => '',
    ),
    'id' => 'type',
    'table' => 'node',
    'field' => 'type',
    'relationship' => 'none',
  ),
));
$handler->override_option('access', array(
  'type' => 'none',
));
$handler->override_option('cache', array(
  'type' => 'none',
));
$handler->override_option('items_per_page', 1);
$handler->override_option('row_plugin', 'node');
$handler->override_option('row_options', array(
  'relationship' => 'none',
  'build_mode' => 'full',
  'links' => 1,
  'comments' => 0,
));
$handler = $view->new_display('block', 'Block', 'block_1');
$handler->override_option('block_description', '');
$handler->override_option('block_caching', -1);
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->override_option('path', 'r_col_audio_node/%');
$handler->override_option('menu', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));
$handler->override_option('tab_options', array(
  'type' => 'none',
  'title' => '',
  'description' => '',
  'weight' => 0,
  'name' => 'navigation',
));

It would be great if I could get something like this working and contribute-able... there are so many people out there trying to figure this out and it seems like such a common thing to need to do. Thanks anyone.

dawehner’s picture

Status: Active » Postponed (maintainer needs more info)

Are you sure this.href has the right url?

If you know this you could jump into dynamic_audio_node_get_by_node_id and look whether it has the right $nid.

But there is another problem in your code


	$args = array($nid);  // Array of arguments we set for the view. Only one argument in our example. your actual view may require additional arguments which you may need to set

Views_embed_view does not expect an array of arguments. You should use it more like this

views_embed_view($name, $display_id, $arg1, $arg2, $arg3);

Hopefully this fixes your problem

butler’s picture

Thanks so much dereine. There was a comment on the tutorial re: not passing an array to views_embed_view() which I had forgotten about. Unfortunately this is still not working but that needed fixing anyway. I now have:

$res = views_embed_view($viewName, $displayId, $nid);

Now trying to try to figure out how I can test this.href - the links as rendered look like I think they should look:

/?q=r_col_audio_node/226

... so trying to figure out if that is somehow being mangled or I'm misunderstanding how to use hook_menu().

Anyway thanks much again.

butler’s picture

Still getting nowhere. Via javascript alerts I was able to determine that this.href is correct... but still can't figure out how to debug the php callback function.

Tried putting this at bottom of my node.tpl.php:

<?php
  print '<pre>';
  var_dump(get_defined_vars());
print '</pre>';
?>

... and, though the page loads, var_dump() shows nothing at all - as though there were no defined vars for the page... I can only imagine that this means something like that there are no defined vars for the document in its current state. Maybe meaning that $nid is not getting a value and that that breaks the page in some state where there are no vars?

Anyone got any idea or direction to suggest re: how I can figure out what's going on? It seems like this is so close to working but... ???

dawehner’s picture

$nid is probably not availible on the node.tpl.php

You could use $node->nid instead.
Instead of writing stuff directly to the node.tpl.php it is better to add a new variable via hook_preprocess_node.

butler’s picture

Thanks for the new reply dereine... I am becoming convinced my problem lies within this function, or perhaps the View, although it seems to work fine on its own. I tried just setting $nid here like:

	$nid = 221;

	// Call the views_embed_view function to returned themed view output
	$res = views_embed_view($viewName, $displayId, $nid);

... and it broke the entire site (blank/white screen on refreshing any page)! Tried setting a variable with arg[1] and replacing $nid with that (blank/white screen on refreshing any page), tried replacing $nid with arg[1] in views_embed_view() instead, result: back to no action on click.

The url of this.href is definitely there as arg[1]. Anyone have any idea why trying to set a variable with this should cause my module to hang the whole site? Am I not understanding, in terms of scope, how/where arg[1] is available?

dawehner’s picture

How are $viewName, $displayId defined? Probably yes but take sure again.

Such things are hard to solve without any kind of local reproducablability.

butler’s picture

$viewName, $displayId are just set statically here (here's the whole callback function):

function dynamic_audio_node_get_by_node_id($nid){
	$viewName = 'most_recent_audio_node_view'; // The name of the view we are going to load
	 
	// Array of arguments we set for the view. Only one argument in our example. your actual view may require additional arguments which you may need to set -
	//	$args = array($nid);

	$displayId = 'block_1'; // The display id of for the view.

	// Call the views_embed_view function to returned themed view output
	$res = views_embed_view($viewName, $displayId, $nid);
	 
	// create a JSON object. The object will contain a property named “audio_node” that will be set with the themed result of the executed view.
	return drupal_json(array('audio_node'=>$res));
	 
	exit;
	}
butler’s picture

Wondering if this could have to do with the initial page being a page of search results via Apache SOLR Search Integration and Apache SOLR Facetbuilder. There is a fair amount of Ajax going on on this page without this... autocomplete, etc. Also Apache SOLR Facetbuilder uses Views to define facets, so loading this other Audio Node View - could all this be competing somehow... ?

Again, any replies or insights greatly appreciated.

esmerel’s picture

Status: Postponed (maintainer needs more info) » Active
kars-t’s picture

Status: Active » Fixed

Hi

I am closing this issue to clean up the issue queue. Feel free to reopen the issue if there is new information and the problem still resides. If not please make sure you close your issues that you don't need any more.

Maybe you can get support from the local user group. Please take a look at this list at groups.drupal.org.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.