0

The goal is very straightforward. I need a form to limit each user to 1 submission per day and 3 per week.

What I found:

Webform: The "Form settings" in webform has the "Per user submission limit" setting. It does not allow me to make more than 1 validation. I'm not able to have both per day and per week in the same form.

Entityform: I found people use hook function on node limit on Drupal 8. This may work on Drupal 7 Entityform but I don't know how to write that syntax.

Rule: It may possible to use Rule module but I don't see any connection with period of time validation.

////// PS: My question is about the limit on Webform(or Entityform) submission for authenticated users.

2
  • If no contrib module provides the functionality you want you probably need to code something yourself. Which makes this a too broad question as it doesn't contain any code or reproducible problem we can fix. Or maybe submit a feature request to Webform.
    – leymannx
    Mar 23 '20 at 16:07
  • @leymannx Thanks for your advice.
    – CocoSkin
    Mar 24 '20 at 13:09
1
+50

Make a custom module to implement hook_node_access, i will post some code based on a user date field that compares 'today' with this user-date-field and return access deny if dates comparison goes beyond 'today', and another with today+7 days, you will have to adapt this code to your needs; please add in user profile a date field, this field it has to be updated by rules every time a user adds content of your 'special content-type', maybe you will need 2 user-date-fiels, one for each 'special-contentype' if you need help with this last comment please ask.

// replace HOOK with yourmodulename and field machine names 
function HOOK_node_access($node, $op, $account) {

  $type = is_string($node) ? $node : $node->type;

  global $user;

  // check if not anonymous
  if($user->uid!=0){

    //check if not administrator role, admin will have allways access
    $user_role = '';
    if (in_array('administrator', $user->roles)) {
      // do fancy stuff
      $user_role = 'administrator';
    }

    if (!$user_role == 'administrator') { 

      $user_logged = user_load($user->uid);
      $user_date_field = $user_logged->YOUR_USER_DATE_FIELD[LANGUAGE_NONE][0]['value'];

      // today date in UNIX date stamp
      $today_int = date('U');

      // your user_date_field in UNIX time stamp
      $date_int = (int)strtotime($user_date_field);

      // check if 'today' is > than user_date_field
      $diff = $date_int - $today_int;
      $diff = round($diff/86400); // diff in days betwen 2 dates

      // today comparison
      if ($diff >= 0 ) {
        // code... user do have access to add/edit content


      } else {
        // code... retunr access deny 'today' add/edit 
        if ($type == 'YOUR_CONTENT_TYPE_MACHINE_NAME') {
          // if options are update ore cerate content, return access deny
          if ($op == 'update' OR $op == 'create') {
            return NODE_ACCESS_DENY;

          }
        }
      }

      //  1 week comparison
      $diff7 = $diff + 7;

      if ($diff7 >= 0 ) {
        // code... user do have access to add/edit content

      } else {
        // code... retunr access deny 'today +7 days' add/edit
        if ($type == 'YOUR_CONTENT_TYPE_MACHINE_NAME_7') {
          // if options are update ore cerate content, return access deny
          if ($op == 'update' OR $op == 'create') {
            return NODE_ACCESS_DENY;

          }
        }
      }
    }
 }
}

NOTE, if you do not want to add fields to user profile form, it could be done by adding variables to logged-user by HOOK_entity_property_info_alter(&$info), if this is needed please ask.

EDIT:

please specyfy in your question that is about webforms, so, above code do not apply, i'll leave it, could be usefull for someone else.

what you can do is still keep 'user_date_field', install the following mudules: flags, rules and panels.

I will explain only ‘today’ settings, same thing for ‘1 week settings’, only in this last case you will need another field (or user-variable) to store how many webforms has submitted this user in current week.

Steps:

• enable rules, rules scheduler, flag & panels modules.

• make a user flag ‘user-posted-a-webform’

• in rules: 

• make a component, unflag current user (it will be invoked later).

• make a rule, flag user when submit a webform, update 'user_date_field' with site-current-date

• in panels make a page and add your webform, add a rule (inside panles content section) make it visible only if user is NOT flagged.

• Return to your rule, add an action “schedule component evaluation”, choose your component, inside action setting, on ‘scheduled evaluation date’ enter ‘+1 day’.

• Done

COMPONENT unflag-user

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

5
  • hi, I'm looking for webform or entityform submission. Does the hook_node_access works on these forms or just regular node(page)? I need validations on one of these forms for authenticated users. Should the date field on the form submission instead of the user profile?
    – CocoSkin
    Mar 31 '20 at 11:22
  • i updated my answer, i hope it helps.
    – pinueve
    Mar 31 '20 at 17:41
  • Thanks, I will try to test that over the weekend.
    – CocoSkin
    Apr 2 '20 at 17:01
  • Can you provide some instructions about how to 'make a component, unflag current user"? What is the setting in Rules "Add new component"?
    – CocoSkin
    Apr 6 '20 at 14:14
  • done, you need to add your flag before making the component, if you have more questions, just ask, regards.
    – pinueve
    Apr 6 '20 at 16:27

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.