0

I would like the users of my site to choose a specific number of nodes. After this they should see, to which taxonomy term these nodes belong to.

I have already found the flag module as well as flag limiter module. I have put the limit to 12 (This is the amount of nodes the user should be able to choose).

Now I would like that the user is redirected to a new page (where he can see the belongig taxonomy terms to his flagged nodes - this I can manage to create with views), after he flagged the 12th node.

Could anyone help me, how to achieve this?

Thanks, Tabea

1

You can able to detect limit has reached using hook_flag_flag hook exposed by flag module..

Flag Limiter module uses the same logic to detect the limit as you can see the below code

function flag_limiter_flag_flag($flag, $entity_id, $account, $flagging) {
  // Get the flag limits.
  $flag_limit = variable_get('flag_limiter_' . $flag->name . '_value', 0);
  $peritem_limit = variable_get('flag_limiter_' . $flag->name . '_peritem_value', 0);

  if ($flag_limit) {
    $user_flags = flag_limiter_get_user_flags($account->uid, $flag->fid);
    if (count($user_flags) > $flag_limit) {
      $unflag = flag('unflag', $flag->name, $entity_id, $account, TRUE);
      if ($unflag) {
        drupal_set_message(t("You've already signed up @num times", array('@num' => $flag_limit)), 'error');
      } else {
        drupal_set_message(t('Fail limiter operation'), 'status', FALSE);
      }
    }
  }

  if ($peritem_limit) {
    $flagging_data = flag_get_counts($flag->entity_type, $entity_id, TRUE);
    if ($flagging_data[$flagging->flag_name] > $peritem_limit) {
      $unflag_item = flag('unflag', $flag->name, $entity_id, $account, TRUE);
      if ($unflag_item) {
        drupal_set_message(t("The event already signed up @num times", array('@num' => $peritem_limit)), 'error');
      } else {
        drupal_set_message(t('Fail limiter operation'), 'status', FALSE);
      }
    }
  }
}

You can implement same hook in your custom module to redirect user to the page you want.. You can use drupal_goto function to redirect user..

function YOURMODULENAME_flag_flag($flag, $entity_id, $account, $flagging) {
  if ($flag_limit) {
    $user_flags = flag_limiter_get_user_flags($account->uid, $flag->fid);
    if (count($user_flags) >= $flag_limit) {
      drupal_goto('path');
    }
  }
}

writing custom module instructions ..

  • Create a folder called "custom" inside sites/all/modules

  • Create a folder called "myflaglimit" inside sites/all/modules/custom/

  • Create a file called "myflaglimit.info" inside "myflaglimit" folder create above.

  • Put in below code and save file

Below code goes into myflaglimit.info file..

name = My Flag Limit
description = Provides a redirect on reaching limit.
core = 7.x
package = Custom
dependencies[] = flag_limiter

Your logic goes into .module file

  • Create a file called myflaglimit.module file inside myflaglimit folder you have create with below code
  • You extend drupal function using hooks which are nothing but functions which starts with modulename_hook

use below code inside module file

<?php
function myflaglimit_flag_flag($flag, $entity_id, $account, $flagging) {
  if ($flag_limit) {
    $user_flags = flag_limiter_get_user_flags($account->uid, $flag->fid);
    if (count($user_flags) >= $flag_limit) {
      drupal_goto('path');
    }
  }
}
  • Thanks a lot. Unfortunately I am beginner with drupal. Could you please tell me, in which file I have to load this code-function or if I have to create a new file (in this case: in which folder do I have to upload it?). – Tabea Apr 18 '14 at 13:26
  • Updated answer with full details :-) – Anil Sagar Apr 18 '14 at 13:36

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.