DataCommand.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Drupal\Core\Ajax;
  3. /**
  4. * An AJAX command for implementing jQuery's data() method.
  5. *
  6. * This instructs the client to attach the name=value pair of data to the
  7. * selector via jQuery's data cache.
  8. *
  9. * This command is implemented by Drupal.AjaxCommands.prototype.data() defined
  10. * in misc/ajax.js.
  11. *
  12. * @ingroup ajax
  13. */
  14. class DataCommand implements CommandInterface {
  15. /**
  16. * A CSS selector string for elements to which data will be attached.
  17. *
  18. * If the command is a response to a request from an #ajax form element then
  19. * this value can be NULL.
  20. *
  21. * @var string
  22. */
  23. protected $selector;
  24. /**
  25. * The key of the data attached to elements matched by the selector.
  26. *
  27. * @var string
  28. */
  29. protected $name;
  30. /**
  31. * The value of the data to be attached to elements matched by the selector.
  32. *
  33. * The data is not limited to strings; it can be any format.
  34. *
  35. * @var mixed
  36. */
  37. protected $value;
  38. /**
  39. * Constructs a DataCommand object.
  40. *
  41. * @param string $selector
  42. * A CSS selector for the elements to which the data will be attached.
  43. * @param string $name
  44. * The key of the data to be attached to elements matched by the selector.
  45. * @param mixed $value
  46. * The value of the data to be attached to elements matched by the selector.
  47. */
  48. public function __construct($selector, $name, $value) {
  49. $this->selector = $selector;
  50. $this->name = $name;
  51. $this->value = $value;
  52. }
  53. /**
  54. * Implements Drupal\Core\Ajax\CommandInterface:render().
  55. */
  56. public function render() {
  57. return [
  58. 'command' => 'data',
  59. 'selector' => $this->selector,
  60. 'name' => $this->name,
  61. 'value' => $this->value,
  62. ];
  63. }
  64. }