SettingsCommand.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Drupal\Core\Ajax;
  3. /**
  4. * AJAX command for adjusting Drupal's JavaScript settings.
  5. *
  6. * The 'settings' command instructs the client either to use the given array as
  7. * the settings for ajax-loaded content or to extend drupalSettings with the
  8. * given array, depending on the value of the $merge parameter.
  9. *
  10. * This command is implemented by Drupal.AjaxCommands.prototype.settings()
  11. * defined in misc/ajax.js.
  12. *
  13. * @ingroup ajax
  14. */
  15. class SettingsCommand implements CommandInterface {
  16. /**
  17. * An array of key/value pairs of JavaScript settings.
  18. *
  19. * This will be used for all commands after this if they do not include their
  20. * own settings array.
  21. *
  22. * @var array
  23. */
  24. protected $settings;
  25. /**
  26. * Whether the settings should be merged into the global drupalSettings.
  27. *
  28. * By default (FALSE), the settings that are passed to Drupal.attachBehaviors
  29. * will not include the global drupalSettings.
  30. *
  31. * @var bool
  32. */
  33. protected $merge;
  34. /**
  35. * Constructs a SettingsCommand object.
  36. *
  37. * @param array $settings
  38. * An array of key/value pairs of JavaScript settings.
  39. * @param bool $merge
  40. * Whether the settings should be merged into the global drupalSettings.
  41. */
  42. public function __construct(array $settings, $merge = FALSE) {
  43. $this->settings = $settings;
  44. $this->merge = $merge;
  45. }
  46. /**
  47. * Implements Drupal\Core\Ajax\CommandInterface:render().
  48. */
  49. public function render() {
  50. return [
  51. 'command' => 'settings',
  52. 'settings' => $this->settings,
  53. 'merge' => $this->merge,
  54. ];
  55. }
  56. }