Use AjaxHelperTrait::isAjax() to fix modal behavior for non-Ajax requests

Created on 15 September 2025, about 2 months ago

Problem/Motivation

Currently, when the Email Contact Link formatter is configured with modal: true, the ContactController doesn't properly distinguish between Ajax and non-Ajax requests. This can lead to unexpected
behavior when users access the contact form directly via URL or when JavaScript is disabled.

The controller attempts to display a modal dialog regardless of request type, which doesn't work properly for non-Ajax requests and can result in poor user experience or functionality issues.

Steps to reproduce

  1. Configure an Email Contact field formatter with modal: true
  2. Access the contact form directly via URL (non-Ajax request)
  3. Observe that the modal logic is still triggered even though it's not an Ajax request

Proposed resolution

Implement \Drupal\Core\Ajax\AjaxHelperTrait::isAjax() in ContactController to properly check request type and handle modal display logic:

  • If modal setting is true AND request is Ajax → display form in modal
  • If modal setting is true AND request is NOT Ajax → display form as regular page
  • If modal setting is false → display form as regular page

Implementation steps:

  1. Add use Drupal\Core\Ajax\AjaxHelperTrait; to ContactController
  2. Include the trait in the class: use AjaxHelperTrait;
  3. Modify ContactController::content() to use $this->isAjax() check
  4. Update modal condition to: if (!empty($field_settings['modal']) && $this->isAjax())
  5. Add test coverage for non-Ajax modal behavior

Remaining tasks

  • Update ContactController to use AjaxHelperTrait
  • Modify modal logic in content() method
  • Add test method testModalRedirectWhenNotAjax()
  • Verify all existing tests continue to pass
  • Manual testing of modal behavior in both Ajax and non-Ajax contexts

User interface changes

When the modal setting is enabled but the request is not Ajax (e.g., direct URL access or JavaScript disabled), the contact form will display as a regular page instead of attempting to show in a modal dialog. This provides better
accessibility and graceful degradation.

API changes

None. This change is internal to the ContactController and doesn't affect any public APIs.

Data model changes

None.

🐛 Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

🇵🇹Portugal saidatom Lisbon

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

No activities found.

Production build 0.71.5 2024