0

I want to add flag counter of content in views field. Authenticated users can flag(bookmark) contents. I want to know how many users flaged a content, And I want to add this in views field(display the number of times a node is flagged by a certain flag by Users).

Notice; I'm using "Search API Index" in content part of views

How can I do it?


UPDATE Question after using Vicky8888 method;

I added below code in global php field of views

   <?php
   $flag = flag_get_flag('bookmarks');
   if ($flag) {
  print "Number of Flags:";
  print $flag->get_count($node->nid);
      }
    ?>

this code work in tpl.php file of content type, But It's showing "0" in views. I got several errors in picture too.

enter image description here

after that I using JS code of Vicky8888.

Drupal.behaviors.your-theme = {

 attach: function (context, settings) { 

 //add your available class below
    jQuery('.view-id-top_users.view-display-id-page .views-field-php .flag-link-toggle').click(function(){

        jQuery(document).ajaxComplete(function() {
         location.reload();
        });
       });
      }
       };

I cleared "your-theme" and added "adaptivetheme", and cleared classes and added ".view-content .views-field.views-field-php .field-content .flag-link-toggle".

after that I using this code in separate file and added

scripts[]=scripts/extracode.js

in adaptivetheme.info file.

I cleared cache and run cron too. but It didn't work too.

1 Answer 1

0

Use these two modules drupal like and flag simultaneously

Go to /admin/structure/flags create flag ,and configure it properly.

Go to your view add Global: PHP field

 <?php
print flag_create_link('bookmarks',$node->nid);

 $counts = flag_get_counts('node', $node->nid);
if(isset($counts['bookmarks'])){
 echo $counts['bookmarks'] .' Likes';
 }else{
      echo"no user flaged a content";
     }


 ?>

And at last put this code in your themes js file

Drupal.behaviors.your-theme = {

 attach: function (context, settings) { 

 //add your available class below
    jQuery('.view-id-top_users.view-display-id-page .views-field-php .flag-link-toggle').click(function(){

        jQuery(document).ajaxComplete(function() {
         location.reload();
        });
       });
      }
       };

To add the flag link in your content type use code:

   <?php
    global $user ,$base_url;
    if(!$user->uid) {
   $flag_link = flag_flag_link($flag, $action, $content_id);


   $flag_url = $flag_link['href'];
    if(isset($flag_link['query']['token'])) {

    $flag_url .= '?'.$flag_link['query']['token'];
}

?>

  <a href="<?php echo $base_url;?>/user/login?destination=<?php echo      current_path();?>">MY-LINK</a>
6
  • unfortunately It's didn't work for me. but when I'm using below code, It's work but always shows "0" flag. <?php $flag = flag_get_flag('bookmarks'); if ($flag) { print "Number of flag"; print $flag->get_count($node->nid); } ?> . This Code work when I using It in tpl.php file of content type but It didn't work in Views. I mean this code in tpl.php file shows number of flags but in views only shows "0".
    – Hamed
    Commented Apr 29, 2016 at 8:34
  • i have the solution and i knows why its showing 0
    – user52318
    Commented Apr 29, 2016 at 9:13
  • Dear @vicky8888 I edited my question
    – Hamed
    Commented Apr 29, 2016 at 11:41
  • use the exact code whatever i editted and then let me know,forget your code whatever you written for some time
    – user52318
    Commented Apr 29, 2016 at 12:20
  • if still it dont work then it may b issue of search api or it may be doing some mistake while configuring modules
    – user52318
    Commented Apr 29, 2016 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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