Skip to main content
1 of 3
Devin
  • 29
  • 6

AJAX SettingsCommand Usage

Class Documentation: SettingsCommand

I'm trying to figure out how to use this Ajax Command correctly. I must be confused from the documentation, so I'm hoping someone can shed some light where I'm mistaken.

__construct(array $settings, $merge = FALSE)

array $settings: An array of key/value pairs of JavaScript settings.
bool $merge: Whether the settings should be merged into the global drupalSettings.

Given the array already in drupalSettings: ['drupalSettings']['myData'] = [1,2,3,4,5];
And the following ajaxCallback I would like to replace the 'myData' array with [2, 4]:


function ajaxCallback(array &$form, FormStateInterface $form_state)
{
  $response = new AjaxResponse();
  $response->addCommand(new SettingsCommand(['myData' => [2, 4]]));
  return $response;
}

I'm testing the state of 'myData' in JavaScript using the following:


setInterval(function() { console.log(settings.myData); }, 5000);

The Problem: After the ajaxCallback is executed settings.myData reports in the console the same as before the ajaxCallback.

Devin
  • 29
  • 6