Problem/Motivation

The documentation for the render array example in AJAX Example is incorrect. It says that there's a nojs path on the callback, but Drupal doesn't actually magically change the path variable for us.

Proposed resolution

This example really doesn't have a non-JS fallback, since it uses AJAX commands.

Change the documentation to explain what actually happens.

Remove {nojs} from the callback path.

Amend whatever code deals with nojs.

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Mile23 created an issue. See original summary.

marvil07’s picture

With this patch, nojs fallback is working for me.
It may be related with the route default, not entirely sure, but the change points to it.

Status: Needs review » Needs work
jlbellido’s picture

Issue tags: +Needs reroll

The last patch doesn't apply anymore

$ git apply --index 0001-Issue-2933639-Make-ajax_example.library-depend-on-dr.patch
error: patch failed: ajax_example/ajax_example.routing.yml:81
error: ajax_example/ajax_example.routing.yml: patch does not apply
jlbellido’s picture

Issue tags: +drupaldevdays
jlbellido’s picture

Rerolled the attached patch and fix the errors in the tests.

earthday47’s picture

I agree.

I think also in the 8.7.x world, the form handler/controller method wouldn't even use the "$nojs" parameter and instead detect the wrapper from the request, something like this. I took this from search_api module

use Drupal\Core\EventSubscriber\AjaxResponseSubscriber;
use Symfony\Component\HttpFoundation\RequestStack;

...

    // If this is an AJAX request, just remove the row in question.
    if ($success && $this->getRequest()->request->get(AjaxResponseSubscriber::AJAX_REQUEST_PARAMETER)) {
      $response = new AjaxResponse();
      $response->addCommand(new RemoveCommand("tr[data-field-row-id='$field_id']"));
      return $response;
    }
    // Redirect to the index's "Fields" page.
    $url = $search_api_index->toUrl('fields');
    return $this->redirect($url->getRouteName(), $url->getRouteParameters());

  • marvil07 committed 3cca75f on 8.x-1.x
    Issue #2933639 by jlbellido, marvil07: AJAX link render array example...
marvil07’s picture

Status: Needs review » Fixed

@jlbellido, thanks for the new patch and fix.
@earthday47, that sounds interesting, I investigated a bit, and added a new issue with a lot more details, see #3046262: Consider ajax detection approach.

I have added the latest patch to 8.x-1.x.

rpayanm’s picture

Issue tags: -Needs reroll

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.