Hello,

I have an embedded View on my frontpage, which returns a filtered display of Node:Type 'Is One Of' "Story", showing the first 10 Story nodes:

<?php $view = views_get_view('top_stories');
print views_build_view('embed', $view, NULL, true, 10);?>

My "Story" node type has a custom "node-stories.tpl.php" template, in which I have inserted <?php print flag_create_link('stories', $node->nid); ?> to flag on or off the Story node.

When viewing the Story node on it's own, the add/remove flag AJAX fires off without a problem. However, that same Story node, when displayed in an embedded View, doesn't fire off the AJAX when the add/remove flag link is clicked on. Instead, the entire page is refreshed and the "Flagged Message" is displayed in the body of the page.

When looking at the page source with the embedded View, I cannot find the javascript code for the flag module
<script type="text/javascript" src="/drupal5/sites/all/modules/flag/theme/flag.js"></script>, which is found on the standalone Story node page.

How can I get the AJAX to work with an embedded view?

I'm using Drupal 5.9, Views 5.x-1.6, and Flag 5.x-1.0-beta4

Any pointer is much appreciated!

Comments

quicksketch’s picture

It's important to know where you're embedding this view. Is it in a node.tpl.php, a module, template.php or some other file? Most notably, if you're embedding it in a page.tpl.php, Flag can't add the necessary JavaScript to the page (since the JS has already been printed at the top of the file). You should also update to beta6 as we're not going install an old version of the module to test, since it might be a bug that's already been fixed since beta4.

Super Druper’s picture

The view is embedded in my page-front.tpl.php

I upgraded my version of Flag to beta4. Upon visiting the front page, I got the following error:

Fatal error: Call to undefined function flag_add_extra_js() in C:\wamp\www\drupal\sites\all\themes\mytheme\flag-stories.tpl.php on line 42

Here's some more background on my setup. I followed the custom flag guide by mooffie ("How to show images instead of text"), as well as the instructions found in the theme/README.txt.

In my template.php, I included the following code:

<?php
function _phptemplate_variables($hook, $vars = array()) {

switch ($hook) {

        ...

  	case 'flag':
	  phptemplate_preprocess_flag($vars);
	  break;
  }

  return $vars;
}
?>

and

<?php
function phptemplate_preprocess_flag(&$vars) {
  $image_file = path_to_theme() .'/' . 'images/flag-' . $vars['flag_name_css'] . '-' . ($vars['action'] == 'flag' ? 'off' : 'on') . '.gif';
  // Uncomment the following line when debugging.
  //drupal_set_message("Flag is looking for '$image_file'...");
  if (file_exists($image_file)) {
    $vars['link_text'] = "<img src='http://localhost/drupal/$image_file'/>";
  }
} 
?>

and, finally

<?php
function phptemplate_flag($flag, $action, $content_id, $after_flagging = FALSE) {
    return flag_phptemplate_adapter($flag, $action, $content_id, $after_flagging);
  }
?>

and in my custom flag-stories.tpl.php template file, I have:

<?php
  if ($setup) {
    drupal_add_css(drupal_get_path('module', 'flag') .'/theme/flag.css');
    drupal_add_js(drupal_get_path('module', 'flag') .'/theme/flag.js');
  }
  flag_add_extra_js($flag, $action, $content_id, $after_flagging);
?>
<div class="flag-wrapper flag-<?php echo $flag_name_css; ?>" style="display:inline;">
  <a href="<?php echo $link_href; ?>" title="<?php echo $link_title; ?>" class="flag <?php echo $action; ?>-action <?php echo $after_flagging ? $last_action : ''; ?>">
     <?php echo $link_text; ?>
  </a>
  <?php if ($after_flagging): ?>
    <div class="flag-message flag-<?php echo $last_action; ?>-message">
      <?php echo $message_text; ?>
    </div>
  <?php endif; ?>
</div>

After removing the flag_add_extra_js($flag, $action, $content_id, $after_flagging); bit, the above error was corrected. However, the AJAX still does not work in my embedded view, now using the beta4 version.

How would you get Flag to add the necessary JavaScript to a custom page (such a my page-front.tpl.php), when it works fine in other standard pages?

Thanks

mooffie’s picture

I got the following error:
Fatal error: Call to undefined function flag_add_extra_js()

The "release notes" mention this, and you did well by removing that line.

I followed the custom flag guide by mooffie ("How to show images instead of text"), as well as the instructions found in the theme/README.txt.

That's irrelevant.

The view is embedded in my page-front.tpl.php

Yep, that's the problem. Nathan explained it: "if you're embedding it in a page.tpl.php, Flag can't add the necessary JavaScript to the page (since the JS has already been printed at the top of the file)."

However, I don't have an ellegant solution right now. (I chimed in only to focus the discussion.)

quicksketch’s picture

The only way you can add a flag to your page.tpl.php file and have the JS added to the page is to manually add in the JS in your _phptemplate_variables() after calling the function to have the view printed.

function _phptemplate_variables($hook, $vars = array()) {
  switch ($hook) {
    case 'page':
      if (drupal_is_front_page()) {
        $view = views_get_view('top_stories');
        $vars['top_stories'] = views_build_view('embed', $view, NULL, true, 10);
        $vars['scripts'] = drupal_get_js();
      }
      break;
  }
  return $vars;
}

So this creates a variable called "top_stories" in your front-page.tpl.php, where you'll print out:

  print $top_stories;
Super Druper’s picture

Nathan, thanks again for your quick response.

I owe you a beer. Your solution worked perfectly.

I really appreciate it.

quicksketch’s picture

Status: Active » Fixed

Awesome. :)

Status: Fixed » Closed (fixed)

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