4

I am trying to just invoke callback in my AJAX response.

Currently I have the following response.

   private function responseWithSuccess($message, $status_code = 200)
    {
        $ajax_response = new AjaxResponse();
        $ajax_response->setStatusCode($status_code);
        $response =
            [
                'status' => 'success',
                'message' => $message
            ];

        $ajax_response->setContent(\GuzzleHttp\json_encode($response));
        return $ajax_response;
    }

It returns a response of the specified format, but is it possible to listen for the response text in this case ?

I've found that I have to use the Drupal Command class that is used for communication.

So I created the custom Command, here is code

class CustomCallbackCommand implements CommandInterface, CommandWithAttachedAssetsInterface
{
    use CommandWithAttachedAssetsTrait;
    /**
     * The content for the dialog.
     */
    protected $message;


    /**
     * Constructs a \Drupal\mymodule\Ajax\BootstrapModalCommand object.
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    /**
     * {@inheritdoc}
     */
    public function render()
    {
        return array(
            'command' => 'onCustomCallback',
            'message' => $this->message,
        );
    }
}

And add it to response like that

private function responseWithSuccess($message, $status_code = 200)
{
    $ajax_response = new AjaxResponse();
    $ajax_response->setStatusCode($status_code);
    $response =
        [
            'status' => 'success',
            'message' => $message
        ];
    $ajax_response->addCommand(new CustomCallbackCommand($response));
    //$ajax_response->setContent(\GuzzleHttp\json_encode($response));
    return $ajax_response;
}

But when I try to send the request, an error appears.

Fatal error: Call to a member function getLibraries() on null in /public_html/core/lib/Drupal/Core/Ajax/AjaxResponse.php on line 48

What is wrong with my code ?

So basically the main question is how to call specific callback when a response comes back.

P.S.

I know that I could just override default handlers of the Drupal JS module, but I am looking for a better way.

  • 1
    Please paste the full error. – Kevin Mar 15 '17 at 15:38
  • 1
    Any reason you are attaching assets? This is a good tutorial for AJAX commands (which works - I've made plenty): mike-miles.com/blog/creating-ajax-callback-commands-drupal-8 – Kevin Mar 15 '17 at 15:42
  • I am not attaching any assets, all code I have I posted above) – HelloMufecayo Mar 15 '17 at 15:43
  • @Kevin, haha my bad) This is the result of copy & paste, I copied someone else's code and didn't noticed this interface – HelloMufecayo Mar 15 '17 at 15:46
  • @Kevin please post your comment as an answer for the question, I will accept it. – HelloMufecayo Mar 15 '17 at 15:46
5

For very basic Commands, you likely do not need to implement CommandWithAttachedAssetsInterface or CommandWithAttachedAssetsTrait.

Here is a good blog post tutorial for a custom Command: https://www.mike-miles.com/blog/creating-ajax-callback-commands-drupal-8

Here is another example: https://www.mediacurrent.com/blog/loading-and-rendering-modal-forms-drupal-8

Here are a couple modules that implement Commands for an example:

You are not going to want to override core Drupal JS, as it is easy to add your own and communicate to your plugins or third party plugins as demonstrated above.

  • Awesome, thanks for additional information and examples, appreciate it. Have a great day ! – HelloMufecayo Mar 15 '17 at 15:50
  • 1
    For reference, I pasted a second example that uses OpenModalDialogCommand, which is an example of when CommandWithAttachedAssetsInterface comes in to play. See: api.drupal.org/api/drupal/… – Kevin Mar 15 '17 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.