SettingsCommand example

/** * {@inheritdoc} */
  protected static $modules = ['node', 'ajax_test', 'ajax_forms_test'];

  /** * Verifies the Ajax rendering of a command in the settings. */
  public function testAJAXRender() {
    // Verify that settings command is generated if JavaScript settings exist.     $commands = $this->drupalGetAjax('ajax-test/render');
    $expected = new SettingsCommand(['ajax' => 'test'], TRUE);
    $this->assertCommand($commands$expected->render());
  }

  /** * Tests AjaxResponse::prepare() AJAX commands ordering. */
  public function testOrder() {
    $expected_commands = [];

    // Expected commands, in a very specific order.     $asset_resolver = \Drupal::service('asset.resolver');
    
'command' => 'restripe',
      'selector' => '#page-title',
    ];

    $this->assertEquals($expected$command->render());
  }

  /** * @covers \Drupal\Core\Ajax\SettingsCommand */
  public function testSettingsCommand() {
    $command = new SettingsCommand(['key' => 'value'], TRUE);

    $expected = [
      'command' => 'settings',
      'settings' => ['key' => 'value'],
      'merge' => TRUE,
    ];

    $this->assertEquals($expected$command->render());
  }

  /** * @covers \Drupal\Core\Ajax\OpenDialogCommand */


    // Prepend a command to merge changes and additions to drupalSettings.     if (!empty($settings)) {
      // During Ajax requests basic path-specific settings are excluded from       // new drupalSettings values. The original page where this request comes       // from already has the right values. An Ajax request would update them       // with values for the Ajax request and incorrectly override the page's       // values.       // @see system_js_settings_alter()       unset($settings['path']);
      $response->addCommand(new SettingsCommand($settings, TRUE), TRUE);
    }

    $commands = $response->getCommands();
    $this->moduleHandler->alter('ajax_render', $commands);

    return $commands;
  }

}
Home | Imprint | This part of the site doesn't use cookies.