What I tried to do is load a node which embeds (views_embed_view()) an Ajax enabled view (Sorting + paging). When viewing the node itself the view works, but loading the node asynchronous and attaching the behaviors for the new part, the ajax sorting does not work. After some investigation, I found out that the Javascript Settings for the Ajax Views are added in function template_preprocess_views_view(&$vars) in theme.inc but it does not get called in that case and so the behaviors are not attached in ajax_view.js:

if (Drupal.settings && Drupal.settings.views && Drupal.settings.views.ajaxViews) {
    $.each(Drupal.settings.views.ajaxViews, function(i, settings) {
      // @todo: Figure out where to store the object.
      new Drupal.views.ajaxView(settings);
    });
  }

Is that the problem?
Any ideas?

Comments

chichilatte’s picture

Status: Fixed » Active

I'm on Drupal 6.22 & Views 6.x-3.0+67-dev, but I had something very similar trying to views_embed_view() an ajax-enabled view inside a template. After a good few hours debugging, it turns out the ajax javascript isn't loading in the header (i suppose understandably, since we're printing the view manually). I managed to fix it by adding the external js files myself in the header, and inserting the required "settings" javascript immediately after the view is printed in the template.

So in my template.tpl.php file i put:


/** HOOK
*/
function mythemename_preprocess_page(&$vars) {
	switch($vars['node']->type) {

		// Embedded AJAX views require manual js injection into the header
		// See http://drupal.org/node/386388
		case 'nodeTypeWhichHasTheEmbeddedAJAXView':
			views_add_js('dependent');
			views_add_js('ajax_view');
			$vars['scripts'] = drupal_get_js();
			break;
	}
}

/** An embedded view doesn't output its javascript settings in the header. 
*	Do it from inside the template file after the page's views have been printed.
*/
function mythemename_get_views_js_settings() {
	// Get a list of all the javascript drupal should be adding to the page.
	// Our missing views ajax settings should be in here.
	$javascript = drupal_add_js(NULL, NULL, 'header');
	// $javascript structure is the one used in /includes/common.inc

	// Go through $javascript and pluck out the views settings.
	// We'll print out only these ones (probably only one, since there's usually only one ajax view on the page).
	foreach ($javascript as $type=>$data) {
		if ($type=='setting') {
			foreach($data as $setting) {
				if (isset($setting['views'])) {
					if (!$js) {
						$js = array('setting' => array());
					}

					$js['setting'][] = $setting;
				}
			}
		}
	}

	if ($js) {
		return drupal_get_js('header', $js);
	}
}

And to add the settings javascript after the embedded view had been printed i added this in the page template itself:


print views_embed_view('my_ajax_view_name');
print mythemename_get_views_js_settings();

Works for me so far, even with a solr search box, cool!

NOTE: If you ever want to debug this kind of thing successfully, i sohh recommend using a combination of the Netbeans debugger, the apache/php xdebug extension and php's xdebug_break() function.

dawehner’s picture

Status: Active » Fixed

@chichilatte
So you use views_embed_view in a page.tpl.php? The problem why this simply can't work is because $scripts is already printed out at that moment. Better use hook_preprocess_page and recalc the $vars['scripts'] like you do here.

@krisdigital

 but loading the node asynchronous and attaching the behaviors for the new part

I assume you use some manual jquery/php code for that. If you would use the drupal ajax system, it should add you the new ajax settings for you. For more information how to do that, there are some presentations from merlinofchaos available.

chichilatte’s picture

Status: Active » Fixed

Ah yep i see, just get the view in the preprocess_page hook in yr template.tpl.php, e.g.

function mymodule_preprocess_page(&$vars) {
	switch($vars['node']->type) {
		case 'nodeTypeWhichHasTheEmbeddedAJAXView':
			$vars['my_view_html'] = views_embed_view('my_ajax_view_name');
			$vars['scripts'] = drupal_get_js();
			break;
	}
}

And in your template you can just <?php print $my_view_html; ?>. Ta.

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

added details