New answers tagged

0

My workaround/solution. massageFormValues() is still called twice by Drupal, but my logic in it will be executed just once, after the form validation is complete: MyFieldWidgetClass.php: class MyFieldWidget extends WidgetBase implements ContainerFactoryPluginInterface { ... ... public function massageFormValues(array $values, array $form, ...


2

It seems ultimately to be down to this code in EntityFormDisplay::processForm(): // Assign the weights configured in the form display. foreach ($this->getComponents() as $name => $options) { if (isset($element[$name])) { $element[$name]['#weight'] = $options['weight']; } } This method is a #process callback for the form which runs after form ...


0

I got it working, i´m sharing this if anybody stumbles across the same, in the end it was trivial with using ReplaceCommand and InvokeCommand function module_field_widget_paragraphs_form_alter(&$element, &$form_state, $context) { switch ($element['#paragraph_type']) { case 'paragraph_events': // Add the button only if the paragraph-...


0

I did not find similar function either. Looks like it does not exist. Though, here is how it can be achieved in Drupal 8/9 forms: $entity = $this->entityTypeManager->getStorage('node')->create([ 'type' => 'article' ]); $form_state->set('entity', $entity); /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */ $...


1

It is fine to use both. If you are not using the Contact Storage module, it should be very easy to convert your contact forms to webforms.


0

Your issue is here: $firstName = $form_state->getValue('firstName'); You need to retrieve the first name values using the tree:


0

Drupal's form API already implements CSRF protection. If your form is not using the form API, you should convert it to do so, and you will automatically have the protection you need. If your form is using the form API, and there are no tokens in the markup, then check custom code/templates/etc that might be removing it. If you're not going to use Drupal's ...


0

#tree Creates a hierarchy of values in the submit handler of a form. Compare: public function buildForm(array $form, FormStateInterface $form_state) { $form['container'] = [ '#type' => 'container', ]; $form['container']['some_value'] = [ '#type' = >'textfield', ]; // Submit buttons etc. not shown return $form; } This will create ...


0

#tree denotes a hierarchy in the form, you still need valid and logical PHP to make it work, e.g. // In the build... $form['parent'] = [ '#type' => 'container', '#tree' => TRUE, ]; for($counter = 0; $counter < $rowCount; $counter++){ $form['parent'][$counter] = [ 'firstname' => ['#type' => 'input', ...], 'lastname' => ['#type'...


1

Build render elements instead of HTML markup: $form['markup1'] = [ '#type' => 'container', '#attributes' => [ 'class' => ['rating_reminder'], ], 'image' => [ '#type' => 'responsive_image', '#responsive_image_style_id' => 'big', '#uri' => $order->campaign_id->entity->product_id->entity->image->...


0

The code shown in the question doesn't work for two reasons. The foreach loop is overriding $form['tables'] each time, with the effect that $form['tables']['#markup'] contains only the last table name found from $drugis_postgis_connection->tableNames() A fieldset element doesn't use #markup You need to use code similar to the following one. $tables = $...


0

If you overwrite $form['tables'] on each iteration, you're only going to end up with one tables on the form. Each element on the $form needs to have a unique name. Try something like this: foreach ($drugis_postgis_connection->tableNames() as $key => $table) { $form['table' . $key] = [ /** ... **/ ]; } If you want only one fieldset, then ...


-2

Your custom entity needs to specify a "label" key in the annotation, then the autocomplete will use the field you specify to search on. e.g.: /** * @ContentEntityType( * id = "example", * * entity_keys = { * "id" = "id", * "uuid" = "uuid", * "revision" = "...


0

The URL alias page it's not a view so, there is no way to add new filter via UI, but you can add a new filter programmatically: Here is an article explaining how to do so DRUPAL 8 ADD ANOTHER FILTER TO URL ALIASES PAGE you need to adjust a little bit the code provided in the article it's for path field and you need nid field !


1

Is there any way to set this in a form_alter? Or should I just validate the checkbox state via JS? You could replace #required by setting a custom validation handler in the form alter hook, if you want to generate the error message server-side: $form_state->setErrorByName('field_tos_accept', $error_message); When I add '#required_error', this should - in ...


0

The approach suggested by @4k4 might be correct, I did not test How do I display a modal dialog on page load, with working title and dialog options, using OpenModalDialogCommand? What I did: Displayed a link and using JavaScript on load trigger the link, which eventually open the modal dialog. This is not a good solution for me.


0

A contrib module isn't required. $form['field_domain_access']['your_option_key']['#disabled' => TRUE];


4

In an Ajax request the form is rebuilt from scratch. This was often asked here why the form build method is called two or even three times. It seems like the entity is rebuilt as well when the form is submitted via Ajax: Drupal\Core\Entity\EntityForm::afterBuild /** * Form element #after_build callback: Updates the entity with submitted data. * * ...


0

If you use the formtarget HTML attribute on your image button, submit will happen in a new browser tab. Here is a basic example: public function buildForm(array $form, FormStateInterface $form_state) { $form['search'] = [ '#type' => 'textfield', '#title' => $this->t('Search for'), '#required' => TRUE, ]; $form['image_button'] = ...


2

You can create a custom Form in MyCustomFormA.php <?php namespace Drupal\MY_MODULE\Form; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; /** * Class MyCustomFormA. */ class MyCustomFormA extends FormBase { /** * {@inheritdoc} */ public function getFormId() { return 'my_custom_form_a'; } ..... } Same way you ...


2

Yes, you can define as many forms in a module as you need. If you want to include two forms into one page, you can do so by using the form builder in your controller to call both forms and build a render array. Instead of using _form in your *.routing.yml file, you would instead use _controller, and define a controller callback rather than a form callback. ...


Top 50 recent answers are included