#StackBounty: #forms #password Password protected page with a form submits for me fine but for others redirects them back to the passwo…

Bounty: 50

I have a custom coded page (using a page-xxx.php template). This page defined in the /Pages area of the WP admin has password protection.

The content in my custom template is a form and so that is naturally what you see once past the PW prompt I’ve setup. The issue is that for some users (not me, I’ve never been able to recreate the issue) is that when they have completed the form, upon submitting it, instead of reloading the same page with a success msg it reloads the same page but it doesn’t seem to remember that they have already got past the password prompt.

can anyone assist troubleshooting this? I don’t know how a password protected WP paeg is functioning (via session vars/cookies?) but I had assumed that once past it, you could reload it/submit a form on it and you wouldn’t need to then get past the PW again.

Can anyone clarify this?


Get this bounty!!!

#StackBounty: #8 #forms #media How can I auto-rotate an image on file upload in a custom form?

Bounty: 100

I’m using the FAPI to provide the user an upload field for an image. The field is:

$form['product_img'] = [
  '#type' => 'managed_file',
  '#description' => 'Upload a PNG or JPG image of the product.',
  '#multiple' => FALSE,
  '#required' => TRUE,
  '#upload_location' => 'public://',
  '#upload_validators' => [
    'file_validate_is_img' => [],
    'file_validate_extensions' => ['png jpg jpeg']
  ]
];

A lot of the users are uploading images taken with their mobile device, resulting in the image in landscape.

The solutions I’ve seen involve the default file upload widget, but I need something that will automatically rotate the image after the upload.


Get this bounty!!!

#StackBounty: #8 #forms Tracking how long it takes a user to submit a node edit form?

Bounty: 100

For a quiz node, I want to track how long it takes a user to submit a node form (the time from Drupal generating the form until the time that the user presses submit), and I want to store this duration in a field on my node.

Use case

Each node of content type Quiz is a 10 question quiz. I want to track the time that users spend filling out the node in order to track their improvement in speed over time.

I do NOT want to track the time spent on the website in general; for that, I already use Google Analytics with scroll tracking, and there is also the Time Spent module.

What I tried

On my node, I added an Integer field, Duration. My thought was that I could calculate the interval from the timestamp when the form was generated until the time the node was saved by adding code in hook_node_presave():

  $original_time = ??? // How do I get the time when the node form was generated? 
  $current_time = Drupal::time()->getCurrentTime();
  $duration = $current_time - $original_time;

Where I’m stuck is I don’t know how to get the time when the node form was created.

I tried to add a Datetime field to my node with a Default date of Current date, but the current date is not set until the node is saved, so that doesn’t work.

So, how can I track the time from generating a node edit form until the time the user presses submit?


Get this bounty!!!

#StackBounty: #functions #users #forms #permissions Add new user : make the fields First Name and Last name required

Bounty: 50

I’m searching how to make First Name and Last name fields required when we add a new user. Right now only username and Email fields are required.

I found a way by adding class="form-required" for the first and last name fields on the file user-new.php.

But I’m looking for a method with adding code on function.php and not touch to the WordPress Core.

Thanks.


Get this bounty!!!

#StackBounty: #8 #views #forms formBuilder()->getForm() outputs everything except the <form> tag in a view?

Bounty: 100

When displaying my entity edit form with Drupal::formBuilder()->getForm($form); (for a flagging attached to a node) in a View that is set to show Content with the Default display, I noticed the <form> tag itself is missing:

<form class="flagging-myflag-edit-form flagging-form" data-drupal-selector="flagging-myflag-edit-form-iq8dn9lwifq" action="/flag/details/edit/MYFLAG/52" method="post" id="myform-myflag-edit-form--iq8dn9lWIfQ" accept-charset="UTF-8">
Everything between the form tags is printed properly.
</form>

The <form> tag appears on the node page correctly, but it does not appear in the view.

So somehow, Views is filtering out the <form> tag, but I need to keep this tag.

This is the code I am using:

function MYMODULE_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  if ($node->getType() == 'MY_CONTENT_YTPE') {
    if ($display->getComponent('my_flagging_form')) {
      $flag = Drupal::service('flag')->getFlagById('MY_FLAG');
      $flagging_entity = Drupal::service('flag')->getFlagging($flag, $node);
      $form = Drupal::service('entity.manager')
        ->getFormObject('flagging', 'edit')
        ->setEntity($flagging_entity);
      $build['my_flagging_form'] = Drupal::formBuilder()->getForm($form);
    }
  }
}

According to twig debugging, the custom form is being output through core/modules/system/templates/form.html.twig, but there’s no form tag when output by Views (the tag does appear on the node page).

This is form.html.twig:

<form{{ attributes }}>
  {{ children }}
</form>

EDIT: I tried to render it based on the comment of @Ales:

  $form = Drupal::service('entity.manager')
    ->getFormObject('flagging', 'edit')
    ->setEntity($flagging_entity);
  $form_to_render = Drupal::formBuilder()->getForm($form);
  $renderer = Drupal::service('renderer');
  $build['remember_form']['#markup'] = $renderer->render($form_to_render);

This gives me the same output as my original code; the form is there, but the <form> tag is not…


Get this bounty!!!

#StackBounty: #8 #forms formBuilder()->getForm() outputs everything except the <form> tag?

Bounty: 100

When displaying my form with Drupal::formBuilder()->getForm($form); I noticed the <form> tag itself is missing:

<form class="flagging-myflag-edit-form flagging-form" data-drupal-selector="flagging-myflag-edit-form-iq8dn9lwifq" action="/flag/details/edit/MYFLAG/52" method="post" id="myform-myflag-edit-form--iq8dn9lWIfQ" accept-charset="UTF-8">
Everything between the form tags is printed properly.
</form>

The form still submits even without the <form> tag, but on the actual entity edit page, there is a <form> tag, so I would like to have the same tag on my custom display.

This is the code I am using:

function MYMODULE_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  if ($node->getType() == 'MY_CONTENT_YTPE') {
    if ($display->getComponent('my_flagging_form')) {
      $flag = Drupal::service('flag')->getFlagById('MY_FLAG');
      $flagging_entity = Drupal::service('flag')->getFlagging($flag, $node);
      $form = Drupal::service('entity.manager')
        ->getFormObject('flagging', 'edit')
        ->setEntity($flagging_entity);
      $build['my_flagging_form'] = Drupal::formBuilder()->getForm($form);
    }
  }
}

According to twig debugging, the custom form is being output through core/modules/system/templates/form.html.twig, but there’s no form tag…

This is form.html.twig:

{#
/**
 * @file
 * Default theme implementation for a 'form' element.
 *
 * Available variables
 * - attributes: A list of HTML attributes for the wrapper element.
 * - children: The child elements of the form.
 *
 * @see template_preprocess_form()
 *
 * @ingroup themeable
 */
#}
<form{{ attributes }}>
  {{ children }}
</form>

EDIT: I think the <form> tag is getting removed by sanitation.

Reason: in hook_form_alter(), I tried this:

  $form['beginning']['#markup'] = "HI MOM <form id='flagging-remember-edit-form'>";
  $form['beginning']['#weight'] = -20;
  $form['end']['#markup'] = "BYE MOM</form>";
  $form['end']['#weight'] = 100;

And I see HI MOM and BYE MOM but no <form> tag.

Still, I’m confused why this is happening; the form array is returned by formBuilder(), so shouldn’t Drupal go ahead and make a form out of it?

EDIT 2: I tried to render it based on the comment of @Ales:

  $form = Drupal::service('entity.manager')
    ->getFormObject('flagging', 'edit')
    ->setEntity($flagging_entity);
  $form_to_render = Drupal::formBuilder()->getForm($form);
  $renderer = Drupal::service('renderer');
  $build['remember_form']['#markup'] = $renderer->render($form_to_render);

This gives me the same output as my original code; the form is there, but the <form> tag is not…


Get this bounty!!!

#StackBounty: #javascript #forms #checkbox #material-ui #react-hook-form Material UI + React Form Hook + multiple checkboxes + default …

Bounty: 100

I am trying to build a form that accommodates multiple ‘grouped’ checkboxes using react-form-hook Material UI.

The checkboxes are created async from an HTTP Request.

I want to provide an array of the objects IDs as the default values:

defaultValues: { boat_ids: trip?.boats.map(boat => boat.id.toString()) || [] }

Also, when I select or deselect a checkbox, I want to add/remove the ID of the object to the values of react-hook-form.

ie. (boat_ids: [25, 29, 4])

How can I achieve that?

Here is a sample that I am trying to reproduce the issue.

Bonus point, validation of minimum selected checkboxes using Yup

boat_ids: Yup.array() .min(2, "")


Get this bounty!!!

#StackBounty: #7 #forms #datetime How to limit form submission per user per day and week?

Bounty: 50

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.


Get this bounty!!!

#StackBounty: #javascript #pdf #forms How to render a fillable PDF form on the web

Bounty: 50

I have a PDF that has a fillable form on it. You can see an example of such a PDF here. When I open this link in chrome, I’m guessing it uses the built in PDF viewer and I’m able to fill in the dropdowns and textarea inputs.

I want to replicate this behavior on a site. My goal is to load the PDF on a site, let the users fill in the form, submit it, and then upload the modified PDF to our servers.

Can anyone recommend a library that can accomplish this? I’ve looked at a few libraries, including mozillas PDF.js, but I haven’t found something that works.


Get this bounty!!!