Skip to main content
Tweeted twitter.com/StackDrupal/status/1560778852544151557
removed "Drupal" from the title
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

how to run Drupal `AjaxResponse` How do I AjaxResponse on a custom ajaxAJAX call?

Became Hot Network Question
added 30 characters in body
Source Link
Yuseferi
  • 22.2k
  • 22
  • 118
  • 218

I have some custom Javascripts. at the part of code I want to leverage Drupal AjaxResponse but looks like it's only working when there Triggered by Drupal Form's ajax element. ( my ajax call verb is POST)

I mean when I can this endpoint from js, I see the response in browser network, but it does not work and not applied.

Response:

[{"command":"alert","text":"just to test trigger Drupal command"}]

Route:

picture_park.preset_selector:
  path: '/admin/media/picture-park/preset-selector'
  defaults:
    _title: 'Preset Selector Picture Park images as Media'
    _controller: '\Drupal\media_entity_remote_picture_park\Controller\MediaController::presetSelector'
  requirements:
    _permission: 'Remote Image Picture Park: Create new media'

Controller method:

  public function presetSelector(): AjaxResponse
  {
    $response = new AjaxResponse();
    $response->addCommand(new AlertCommand("just to test trigger Drupal command"));

    return $response;
  }

JS:

  function postData(data) {
    return jQuery.post("/admin/media/picture-park/preset-selector?_wrapper_format=drupal_ajax", {
      "data": data,
      "js": true,
      "_drupal_ajax": 1,
    })
  }

So, what I'm looking for is, how can I make Drupal AjaxResponse runs and get executed when the endpoint is called from a custom js?

I have some custom Javascripts. at the part of code I want to leverage Drupal AjaxResponse but looks like it's only working when there Triggered by Drupal Form's ajax element.

I mean when I can this endpoint from js, I see the response in browser network, but it does not work and not applied.

Response:

[{"command":"alert","text":"just to test trigger Drupal command"}]

Route:

picture_park.preset_selector:
  path: '/admin/media/picture-park/preset-selector'
  defaults:
    _title: 'Preset Selector Picture Park images as Media'
    _controller: '\Drupal\media_entity_remote_picture_park\Controller\MediaController::presetSelector'
  requirements:
    _permission: 'Remote Image Picture Park: Create new media'

Controller method:

  public function presetSelector(): AjaxResponse
  {
    $response = new AjaxResponse();
    $response->addCommand(new AlertCommand("just to test trigger Drupal command"));

    return $response;
  }

So, what I'm looking for is, how can I make Drupal AjaxResponse runs and get executed when the endpoint is called from a custom js?

I have some custom Javascripts. at the part of code I want to leverage Drupal AjaxResponse but looks like it's only working when there Triggered by Drupal Form's ajax element. ( my ajax call verb is POST)

I mean when I can this endpoint from js, I see the response in browser network, but it does not work and not applied.

Response:

[{"command":"alert","text":"just to test trigger Drupal command"}]

Route:

picture_park.preset_selector:
  path: '/admin/media/picture-park/preset-selector'
  defaults:
    _title: 'Preset Selector Picture Park images as Media'
    _controller: '\Drupal\media_entity_remote_picture_park\Controller\MediaController::presetSelector'
  requirements:
    _permission: 'Remote Image Picture Park: Create new media'

Controller method:

  public function presetSelector(): AjaxResponse
  {
    $response = new AjaxResponse();
    $response->addCommand(new AlertCommand("just to test trigger Drupal command"));

    return $response;
  }

JS:

  function postData(data) {
    return jQuery.post("/admin/media/picture-park/preset-selector?_wrapper_format=drupal_ajax", {
      "data": data,
      "js": true,
      "_drupal_ajax": 1,
    })
  }

So, what I'm looking for is, how can I make Drupal AjaxResponse runs and get executed when the endpoint is called from a custom js?

Source Link
Yuseferi
  • 22.2k
  • 22
  • 118
  • 218

how to run Drupal `AjaxResponse` on custom ajax call?

I have some custom Javascripts. at the part of code I want to leverage Drupal AjaxResponse but looks like it's only working when there Triggered by Drupal Form's ajax element.

I mean when I can this endpoint from js, I see the response in browser network, but it does not work and not applied.

Response:

[{"command":"alert","text":"just to test trigger Drupal command"}]

Route:

picture_park.preset_selector:
  path: '/admin/media/picture-park/preset-selector'
  defaults:
    _title: 'Preset Selector Picture Park images as Media'
    _controller: '\Drupal\media_entity_remote_picture_park\Controller\MediaController::presetSelector'
  requirements:
    _permission: 'Remote Image Picture Park: Create new media'

Controller method:

  public function presetSelector(): AjaxResponse
  {
    $response = new AjaxResponse();
    $response->addCommand(new AlertCommand("just to test trigger Drupal command"));

    return $response;
  }

So, what I'm looking for is, how can I make Drupal AjaxResponse runs and get executed when the endpoint is called from a custom js?