New answers tagged

0

the correct $form_state get value should be *recommended* : $form_state->getValue('key'); or $form_state->getValues()['key']


4

You have to add ajax commands to the ajax response, using the unrendered render array: $response->addCommand(new ReplaceCommand('#form-container', $renderable)); BTW you can only return a Drupal ajax response if you have triggered an ajax request via ajax.js. Outside of form submits you usually do this by adding the class use-ajax to a link. For custom ...


1

Entity forms usually run as first submit handler the entity builder functions. Then following submit handlers can get the processed field values directly from the form object entity: $entity = $form_state->getFormObject()->getEntity(); $field_values = $entity->field_foo->getValue();


3

According to the documentation: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21Select.php/class/Select #empty_option: (optional) The label to show for the first default option. By default, the label is automatically set to "- Select -" for a required field and "- None -" for an optional field.


0

Ok, I found the solution... It was a widget problem!! As I used a custom widget for this paragraph field, I didn't realize that the widget was also handling part of the violation process inside the function flagErrors which was badly overroded. Sorry for this


0

I think I found a good way... feel free to comment: 1-Create an Element which will display the custom widget 2-Move a maximum of the logic from the widget to the element 3-Use the element in the widget


0

On your addConstraint() your second parameter is an empty array - if you do not want to add any options leave that empty or use NULL


0

Here is server-side multiple submit validation implementation available in the module for the Drupal 8 and Drupal 9 https://www.drupal.org/project/pmfs


0

On Drupal 8.9.x you can go with $form['actions']['submit']['#value'] = t('Sign up'); which is working in YOUR_MODULE.module also See https://www.drupal.org/forum/support/theme-development/2016-01-05/how-to-change-submit-button-text-in-registration-form#comment-13530797


0

Not sure this would be the recommended way but I got it working using hook_form_alter. Customers start by filling their shipping address at step 1 (required) and on complete step, I grab this shipping address and copy it to the customer_information address field on guest registration, so the customers don't need to fill the same address again. In case the ...


-1

You need to add namespace Drupal\my_module\Plugin\Validation\Constraint; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator; /** * Validates the MyConstraint . */ class MyConstraintValidator extends ConstraintValidator { /** * {@inheritdoc} */ public function validate($items, Constraint $constraint) ...


0

you can try the next code. You will have to create a new image style which rotates the image and then use it in prerendering on image preview. $form['product_img'] = [ '#type' => 'managed_file', '#description' => 'Upload a PNG or JPG image of the product.', '#multiple' => FALSE, '#required' => TRUE, '#upload_location' => 'public://', ...


4

Nice catch! It does nothing. You can safely remove it. Looks like some kind of forgotten relict. Ah, yeah. The same pattern can be found in core's book module: BookSettingsForm.php. And there is a 12(!) years old open issue to have it removed. [#315176] Clean-up remains of $form['array_filter'] hack with array_filter in book module


1

hook_ENTITY_TYPE_view() is probably the most misunderstood/misused hook in Drupal 8, mainly because it behaves differently than it did in Drupal 7 so you will find many misleading examples which no longer work in the current version of Drupal. In short, hook_ENTITY_TYPE_view() is only called when the entity is being rendered - after the entity has been ...


1

There is a big gotcha with the search form. The answer with the themename_search only works if the search block is enabled or disabled and has never been removed in /admin/structure/block. If in /admin/structure/block you re-add/place it again, the search form ID changes to formsearch. It no longer uses the theme name! And again it only works if this block ...


1

You can do it like this: Add a field to the form to store the user agent. Set permissions for the user agent field to be hidden. You can do this in hook_form_alter or use something like the Field Permissions module. In hook_entity_presave, save the user agent to the field: $user->set('field_text_log_useragent', $_SERVER['HTTP_USER_AGENT']);


4

I finally solved this using the Require on Publish module. It offers an additional checkbox to require only when publishing the node. This module allows fields to be required only when the piece of content is being published or is already published. This can be used when you have fields such as tags or SEO information on your piece of content that editors ...


0

Drupal 8: $query = \Drupal::service('entity.query') ->get('user') ->condition('field_custom', $custom_value) $entity_ids = $query->execute();


Top 50 recent answers are included