0

I have several flags like "Sold" "Withdraw" etc which appear in Contextual links. Flagged nodes are checked through node--type.tpl.php and this allows me to add text strings like "Sold" or "Withdrawn", which I theme with CSS to show as large labels.

The problem is that when the end-user flags an item with a Flag toggle, it's not very clear what this flag does. I need that teaser to auto-refresh, so that my labels ("Sold" or "Withdrawn") become visible immediately without the end-user having to reload that page.

I have found this js code , but what it does is slide the node teaser up, which is okey for the "Withdrawn" flag in some views), but is not the correct behaviour for the "Sold" flag. For the "Sold" I need that teaser to refresh itself.

$(document).bind('flagGlobalAfterLinkUpdate', function(event, data) {
      $('#'+data.contentId).hide('');
       // $.growl('Flag', 'The article '+data.contentId + ' has been flagged and hidden.');
    });


Drupal.behaviors.moduleName = function()
{
  $(".flag-action").each(function()
  {
    $(this).click(function()
    {
      $(this).parents(".node").slideUp();
    });
  });
};
0

Step 1 (PHP part):

You need to implement, hook_flag_javascript_info_alter() function, and pass in the refreshed teaset html, so that you can use this in js file.

function yourmodulename_flag_javascript_info_alter(&$info) {
  $content_id = $info['contentId'];
  $node = node_load($content_id);
  $view = node_view($node, 'teaser');
  $rendered = drupal_render($view);
  $info['rendered_node'] = $rendered;
}

Step 2 (JS) Part:

Now upon flagging, you will have the refreshed node html as data.rendered_node. So employing your js code you wrote above:

$(document).bind('flagGlobalAfterLinkUpdate', function(event, data) {
  var node_id = data.contentId;
  var rendered_teaser = data.rendered_node;
  //here you have to check the logic, find the selector that is to be
  //replaced, and then replace that with the 'rendered_node' variable we declared above.

});

Please note that my intention here was to give you a direction and the general logic and not the exact code, so most probably you have to modify the above code to make it work.

UPDATE:

In the step 2 code, you see I got a variable node_id. That is the nid of the content that was flagged. In the html, you must be having some class using that nid, may be "node-765"..where this 765 is the id. look for such a class. Once you have the class name, and say that class is of the form "node-nid-765". So we will check for that like this:

$('node-nid'+node_id).replace(rendered_teaser);

Again please note the class names are not exact and have been provided to give you an example.

  • Thanks. I created the "teaser_refresh.module" and pasted the code from Step 1. Then created the module info file. Then created "teaser_refresh.js" and placed it in the new module's folder (I hope it's not intended for theme's folder). But what now? You say "here you have to check the logic, find the selector that is to be replaced, and then replace that with the 'rendered_node' variable we declared above." But how do I do that? Let's say the selector is .node-flag* (any flagged teaser) And how can I know what to replace 'rendered_node' with? Can you please be more specific? Thanks. – drupalina Oct 7 '13 at 2:14
  • I updated the code. please have a look. – Neo Oct 7 '13 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.