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
Comment #1
quicksketchIt'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.
Comment #2
Super Druper commentedThe 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 42Here'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:
and
and, finally
and in my custom flag-stories.tpl.php template file, I have:
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
Comment #3
mooffie commentedThe "release notes" mention this, and you did well by removing that line.
That's irrelevant.
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.)
Comment #4
quicksketchThe 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.
So this creates a variable called "top_stories" in your front-page.tpl.php, where you'll print out:
Comment #5
Super Druper commentedNathan, thanks again for your quick response.
I owe you a beer. Your solution worked perfectly.
I really appreciate it.
Comment #6
quicksketchAwesome. :)