New answers tagged

0

You could add a fieldset for each person, something like this should work: for ($i = 0; $i < $num_names; $i++) { $form['names_fieldset']['person_' . $i] = [ '#type' => 'fieldset', '#title' => $this->t('Person ' . $i), ]; $form['names_fieldset']['person_' . $i]['name'] = [ '#type' => 'textfield', '#title' => $this->...


1

try to print it this way it gives you more control : <div id="candidature_spontanee" class="form-contact candidature_spontanee"> {# 1.use the attribute to set the form action and other attributes #} <form{{ attributes }}> {{ title_prefix }} {{ title_suffix }} {# 2.print the form elment using : {{ element.elements.key }} #} <div ...


1

Based on @Jaypan answer and @FLY comment I made a FieldWidget. This indeed removed the need for the hook_managed_file_process and hook_element_info_alter. This also made it possible to use the widget on a per field basis. First make your custom FieldWidget in [MODULE]/src/Plugin/Field/FieldWidget: namespace Drupal\[MODULE]\Plugin\Field\FieldWidget; use ...


0

The AJAX updates I wanted was to provide some progress updates which I found out I can achieve form api #ajax['progress']. I guess if I had mentioned that in the original question it would have made life a bit simpler :) : first create a url for the ajax progress update to call: function awsImage_menu() { $items = []; $items['path/to/page'] = [ '...


1

In replacement to your "Learn more" add "Custom text" field after "Field_program_id" in your views. In "Text" add "a" tag like <a href="/search?nodeid={{field_program_id}}" class="button">Learn more</a> Take a look in "Replacement patterns" for available fields. Read description there too for a guidance. (this is in Drupal 8) It is the almost ...


0

AFAIK forms are tied to operations at entity-type level, so you don't have any hook to only alter the form for a particular bundle. Since what you want is to inject certain services for a given bundle what you can do is to implement a chain of responsability pattern through service collectors. This allows you to inject only the service collector to your form ...


0

Use hook_form_alter to set the disabled property TRUE for the form field: $form['field_name']['#disabled'] = TRUE; Please refer to: Is it possible to hide fields on node edit form?


0

Use hook_form_alter to set the disabled property TRUE for the form field: If its required to Disable the field only while editing only then, function module_name_form_alter(&$form, &$form_state, $form_id){ if($form_id == 'form_id_name'){ if (arg(2) == 'edit') { #Use the URL parts $form['field_name']['#disabled'] = TRUE; } } } ...


0

Remove the 'wrapper' => 'first' that exist in the example_select. Set '#ajax'=> 'wrapper' elements to 'first', actually this is the id of element that will be updated, in your case <div id="first"> that you create using the prefix and suffix in $form['example_select2'] This is an extra step, for some reason it will work but you'll get an error ...


0

The below code is working well forme. public function myAjaxCallback(array &$form, FormStateInterface $form_state) { if ($selectedValue = $form_state->getValue('example_select')) { $arr = array('1' => 'Nice way', '2' => 'Good way'); $form['example_select2']['#options'] = $arr; } $form_state->setRebuild(TRUE); $response = new ...


0

Here's the example from the examples module found here use Drupal\Core\Form\FormStateInterface; /** * Implements the ajax demo form controller. * * This example demonstrates using ajax callbacks to add people's names to a * list of picnic attendees. * * @see \Drupal\Core\Form\FormBase * @see \Drupal\Core\Form\ConfigFormBase */ class AjaxAddMore ...


1

As with most things you are learning. The answer is often simpler than first thought. To resolve my issue I was able to use hook_form_alter and implement my own submission handler. From here I was able to disable the redirect to prevent the page reloading. Once redirect was disabled then my block plugin was able to get access to the form and use the ...


2

You can pass down your region's ID as parameters to your route. For example: <a class="use-ajax" data-dialog-options="{&quot;width&quot;:550, &quot;dialogClass&quot;: &quot;my-region-class&quot; }" data-dialog-type="modal" href="/route/to/custom/form/this-region">...</a> As explained here you can add arguments from the ...


2

To use state for checkboxes element, you should target the specific element by JQuery selector, all elements they have the name="field_select_shipping" with value attribute different so try to target the input with both attributes try with the following to show adresse_home only if home and colissimo checkbox checked: $form['input']['adresse_home'] = [ ...


0

Your approach is right but you have the wrong syntax. $form['input']['adresse_home'] = [ '#type' => 'textfield', '#title' => 'Adresse', '#required' => false, '#states' => [ 'visible' => [ ':input[name^="field_select_shipping"]' => [['checked' => 'true'], ['value' => 'home']], // Omitted the rest. ], ]; ...


0

If the form field is empty you need to stop propagation of the parent form. This should be what you need. countryValidation = function() { // Lets use a function so we can check the field $country = $('#country'); if ($country.val() === '') { return false; } else { return true; } } $('#user-number-value').on('focus', ...


0

You can use wrapper_attributes property. $form['example'] = [ '#type' => 'textfield', '#title' => $this->t('Example'), '#wrapper_attributes' => ['class' => ['wrapper-class']], ];


1

Drupal 8 doesn't cache Ajax forms anymore, so you can't get properties of the form object set in the initial form build. see Bypass form caching by default for forms using #ajax. Forms are only cached as rendered form. Put the entity id in a hidden form element to load the entity before saving. Add the entity cache tag to the form if you use default values ...


0

You are trying to store state values into a protected variable in the class. That will not work. You will have to store state values into the form. So instead of using $this->step and other class properties to store values use $form_state->set('step', $value); to persist values between submissions.


0

I think you just need to adjust your submission handler; don't include the fieldset "options_settings". I use the getValue() method and pass it an array of values, the form key and the index of its value. if ($file_id = $form_state->getValue(['header_background', '0'])) { $file = \Drupal::entityTypeManager()->getStorage('file')->load($file_id)...


0

Streamlining the above answer. To switch quantity textbox to be a select field... function THEME_form_alter(&$form, $form_state, $form_id) { switch ($form_id){ case 'views_form_commerce_cart_form_default': $select = array('1' => '1 ','2' => '2 ','3' => '3 ','4' => '4 ','5' => '5 ','6' => '6 ','7' => '7 ','8' => '8 ','...


0

I'm the OP: This is not a full answer, but a fix: Forcing the session to start with $_SESSION['something'] = 1; in the form build seems to cure this problem. If you can provide better answers please do.


0

You can find it under drupal documentation at Database API Section, for example for insert query check it here $connection = \Drupal\Core\Database\Database::getConnection() ; $connection->insert('mytable')->fields([ 'title' => 'Example', 'uid' => 1, 'created' => REQUEST_TIME, ]) ->execute();


3

$form['#validate'] will not work as that form "This form works by submitting the hash and timestamp to the user.reset" aka redirects you to the user.reset from what you can do is something like temp replace this form with a custom one though. however i don't really see the point /** * Implements hook_form_alter(). */ function yourmodule_form_alter(&...


0

'array('value' => ':input[name="confirm_password"]') This means the value equals the literal string ":input[name="confirm_password"]". As far as I know, #states can't check if the value of two fields are the same. I can do this with JavaScript, potentially, but then the JavaScript code would managing some states and the PHP code would manage other ...


2

We can look at Drupal\node\NodeTypeForm.php for an example. We see the name and type fields defined like: $form['name'] = array( '#title' => t('Name'), '#type' => 'textfield', // ... ); $form['type'] = array( '#type' => 'machine_name', // ... '#machine_name' => array( 'exists' => [ 'Drupal\node\Entity\NodeType', '...


0

You can implement all of this conditional logic using the Webform contrib module: http://www.drupal.org/project/webform Once you install the module, it provides its own documentation within your Drupal site, and even has links to excellent tutorial videos by module maintainer Jacob Rockowitz. You could also Google "Drupal Webform conditional" and find ...


Top 50 recent answers are included