#StackBounty: #html #forms #input #radio-button Multiple values in radio input within form with vanilla HTML

Bounty: 250

I am aiming to create a form to handle disabled JavaScript experience for a small component on my website. Currently I have the following form:

<form method="GET" action="https://mywebsite.com/somedirectory/">

    <input type="radio" id="uid1" name="someParam" value="fruity" />
    <label for="uid1">Fruit</label>

    <input type="radio" id="uid2" name="someParam" value="veggie" />
    <label for="uid2">Vegetable</label>

    ...other radio options

    <input type="submit" value="Submit" />
</form>

Clicking on either of the radio options and then on the submit button will result in:

option 1: https://mywebsite.com/somedirectory/?someParam=fruity
option 2: https://mywebsite.com/somedirectory/?someParam=veggie

How can I add another value for each of the radio options? Say I would like to pass someOtherParam which is unique for each option and I would like to get this as output for my options:

option 1: https://mywebsite.com/somedirectory/?someParam=fruity&someOtherParam=apple
option 2: https://mywebsite.com/somedirectory/?someParam=veggie&someOtherParam=pepper

What I have tried is:

<input type="radio" id="uid1" name="someParam" value="fruity&someOtherParam=apple" />
<input type="radio" id="uid2" name="someParam" value="veggie&someOtherParam=pepper" />

However, the & symbol is converted to %26 inside the link and feels too hacky. Is there a better way to achieve this? Also, is there a way to make sure the Submit button is only enabled once a radio option is selected?

P.S. I am aiming for pure HTML experience with no Javascript involved. Is that possible?


Get this bounty!!!

#StackBounty: #views #nodes #forms #comments #9 How to add comment form to a view of nodes in Drupal 9?

Bounty: 50

I’m using views to display the fields of nodes. I prefer to use fields because it allows me to use relationships (bring in author profile picture etc.). Adding the Content: Comments field shows the list of comments as well as the comment form for each node. However, when hiding and rewriting the result/and or using the replacement pattern for Content: Comments, the comment form disappears and only the comment list remains.

What methods can I use to display the comment form for each node in a list of nodes view when using fields?


Get this bounty!!!

#StackBounty: #8 #forms Better code to output a pdf downloaded by the browser

Bounty: 50

In a submit function of a custom form, I would like to output a pdf which can be downloaded by the user/browser when clicking on ‘Get PDF’.

The following code is doing the job only once, because after clicking on the ‘Get PDF’ button, all the buttons of the form does not work anymore. Clicking on any of them doesn’t produce any effect.

Any Idea?

  static public function submitPdf(array &$form, FormStateInterface $form_state) {            
    if ($pdf_content = my_function_to_output_a_pdf_as_string()) {            
      $response = new Response($pdf_content);            
      $disposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'my_pdf.pdf');            
      $response->headers->set('Content-type', "application/pdf");            
      $response->headers->set('Content-Disposition', $disposition);            
      //$response->headers->set('Content-Transfer-Encoding', 'binary');            
      $response->headers->set('Content-length', strlen($pdf_content));            
      $form_state->setResponse($response);  
      //Thanks to kiamlaluno, it is working slighty better if I remove the following line (setRebuild())          
      //$form_state->setRebuild(TRUE);            
    }            
  }            


Get this bounty!!!

#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!!!