New answers tagged

1

The first point is the only straightforward way. A regular node edit form has already Ajax form elements. You can check the "ajax" subarray of drupal-settings-json delivered to the browser. You should see your own Ajax callback here as well: "ajax": { "edit-field-article-add-more": { "callback": [ &...


0

How do I adapt this to open off-canvas dialog from javascript/jquery? I've tried the Drupal.dialog script which only opens normal dialogs. I've also seen the Drupal.offCanvas {} script - which is largely undocumented. Any pointers please?


1

You can use a private tempstore. See for example http://karimboudjema.com/en/drupal/20190315/saving-temporary-values-form-private-tempstore-drupal-8


1

What is best practice to pass the parameters to the controller is opinion based and depends on the use case, so I try to cover all possible approaches. In general, there are three other ways to pass parameters to controllers, query string, POST data and SESSION. You could serialize the arrays for the query string, but easier would be POST or SESSION. See How ...


0

The way core Drupal does this is to use '' for the first parameter of form_set_error(). So in the example you give, form_set_error('', 'error_message); would set the error message for the form, not tied to any specific form element. This is an undocumented feature of form_set_error(), but it's a feature that core Drupal has used forever, over many different ...


2

There are two errors in the code shown in the question. It's only the form builder that can add options to form elements. Differently, you will get the error you quoted. An invalid selection has been identified. Please contact the site administrator. What the AJAX callback does is simply returning the form element that needs to be changed. In your case, ...


1

Found out there is some very Webform-specific logic involved (@see WebformSubmission entity using a custom theme and render logic). My custom rendering is confusing webform's logic. I switched to Webform's own renderer, and enforced the admin theme after my custom rendering. function mymodule_webform_options_alter(array &$options, array &$element, $...


0

For Drupal 8/9 this works $form['field_first_deposit']['#access'] = false;


1

"filled" won't work for date elements, but you can check if it is not empty with: ['!value' => '']


0

You can do something like this: in your new module my_prerenders in my_prerenders/src/ add a new class: namespace Drupal\my_prerenders; use Drupal\Core\Security\TrustedCallbackInterface; class MyPreRenders implements TrustedCallbackInterface { /** * @inheritDoc */ public static function trustedCallbacks() { return ['...


2

I figured it out, hopefully this will help someone else with a similar question. Instead of doing a $formstate->setRebuild(); in the submitForm method, I changed it to $form_state->setRedirect( 'sliker_drupal_backend.browser', [], [ 'query'=>[ 'logic'=>$form_state->getValue('logic'), 'items'=>$form_state-&...


0

Found a solution that works for me. This form above is a modal form rendered as Iframe. So in my .module file. I had to adjust the action attribute itself: /** * Implements hook_preprocess_HOOK(). */ function freitag_layout_visibility_control_preprocess_form(&$variables) { if (isset($variables['attributes']['id']) && $variables['...


0

In this approach, the hook is fired for all forms To avoid extra firing of the hook, use this approach (module name and form id) function MYMODULE_user_register_form_alter(&$form, Drupal\Core\Form\FormStateInterface $form_state, $form_id){ //do your alteration }


1

The solution that worked for me was adding a link element within the actions array $form['actions']['google_search'] = [ '#type' => 'link', '#title' => $this->t('Google Search'), '#url' => Url::fromUri('https://www.google.com/search?whatever'), '#attributes' => ['target' => '_blank', 'class'=> 'button'], ]; ...


0

an other solution (I had to loop on inputs) : $form['propositions_fieldset'][$i]['add_date_de_pose'] = array( '#type' => 'submit', '#value' => t('Ajouter une date de pose'), '#submit' => array('ajax_date_de_pose_add_more_add_one'), '#name'=>$i, '#ajax' => array( 'callback' => 'ajax_date_de_pose_add_more_callback', 'wrapper' =&...


0

I've written a tutorial to explain how to do this: https://www.jaypan.com/tutorial/drupal-form-api-ajax-form-and-results-same-page


0

If working with Drupal contact forms (and not Webform), it would be something like: // Replace HOOK with 'yourThemeName' or 'yourModuleName' depending on where you put this code function HOOK_form_alter(&$form, FormStateInterface $form_state, $form_id) { if($form_id == "your_contact_form_id") { $form["subject"]["...


2

Built in is only recommendation. But there are several contrib modules like https://www.drupal.org/project/password_policy Use such a module. If you have created your own custom form, just use the usual public function validateForm(array &$form, FormStateInterface $form_state) method to implement your rules.


4

Since Drupal 8 the form object instantiated with buildForm() is not preserved between the request rendering the form and the first Ajax request. So be prepared that buildForm() is called again and has to produce the exact same result. When you get data from $form_state this is not the data you expect from the first buildForm() because this is never cached. ...


-1

Set default value for the form either in settings or you can do it via hook_form_alter(). Then set the filed as disabled.


0

I believe you are looking for Entity Operations. Soul you'd have to load the enity by ID, and then use the getoperations methods. Looking at your code though, I would suggest you use EntityTypeManager to instead load your entities with a EntityListController or something equivalent, it's all baked into core.


Top 50 recent answers are included