SettingsCommand.php 1.63 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Ajax\SettingsCommand.
6 7 8 9 10 11 12 13
 */

namespace Drupal\Core\Ajax;

/**
 * AJAX command for adjusting Drupal's JavaScript settings.
 *
 * The 'settings' command instructs the client either to use the given array as
14
 * the settings for ajax-loaded content or to extend drupalSettings with the
15 16
 * given array, depending on the value of the $merge parameter.
 *
17
 * This command is implemented by Drupal.AjaxCommands.prototype.settings()
18
 * defined in misc/ajax.js.
19 20
 *
 * @ingroup ajax
21 22 23 24 25 26
 */
class SettingsCommand implements CommandInterface {

  /**
   * An array of key/value pairs of JavaScript settings.
   *
27 28
   * This will be used for all commands after this if they do not include their
   * own settings array.
29 30 31 32 33 34
   *
   * @var array
   */
  protected $settings;

  /**
35
   * Whether the settings should be merged into the global drupalSettings.
36 37
   *
   * By default (FALSE), the settings that are passed to Drupal.attachBehaviors
38
   * will not include the global drupalSettings.
39
   *
40
   * @var bool
41 42 43 44 45 46 47 48
   */
  protected $merge;

  /**
   * Constructs a SettingsCommand object.
   *
   * @param array $settings
   *   An array of key/value pairs of JavaScript settings.
49
   * @param bool $merge
50
   *   Whether the settings should be merged into the global drupalSettings.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
   */
  public function __construct(array $settings, $merge = FALSE) {
    $this->settings = $settings;
    $this->merge = $merge;
  }

  /**
   * Implements Drupal\Core\Ajax\CommandInterface:render().
   */
  public function render() {

    return array(
      'command' => 'settings',
      'settings' => $this->settings,
      'merge' => $this->merge,
    );
  }

}