InsertCommand.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Drupal\Core\Ajax;
  3. /**
  4. * Generic AJAX command for inserting content.
  5. *
  6. * This command instructs the client to insert the given HTML using whichever
  7. * jQuery DOM manipulation method has been specified in the #ajax['method']
  8. * variable of the element that triggered the request.
  9. *
  10. * This command is implemented by Drupal.AjaxCommands.prototype.insert()
  11. * defined in misc/ajax.js.
  12. *
  13. * @ingroup ajax
  14. */
  15. class InsertCommand implements CommandInterface, CommandWithAttachedAssetsInterface {
  16. use CommandWithAttachedAssetsTrait;
  17. /**
  18. * A CSS selector string.
  19. *
  20. * If the command is a response to a request from an #ajax form element then
  21. * this value can be NULL.
  22. *
  23. * @var string
  24. */
  25. protected $selector;
  26. /**
  27. * The content for the matched element(s).
  28. *
  29. * Either a render array or an HTML string.
  30. *
  31. * @var string|array
  32. */
  33. protected $content;
  34. /**
  35. * A settings array to be passed to any attached JavaScript behavior.
  36. *
  37. * @var array
  38. */
  39. protected $settings;
  40. /**
  41. * Constructs an InsertCommand object.
  42. *
  43. * @param string $selector
  44. * A CSS selector.
  45. * @param string|array $content
  46. * The content that will be inserted in the matched element(s), either a
  47. * render array or an HTML string.
  48. * @param array $settings
  49. * An array of JavaScript settings to be passed to any attached behaviors.
  50. */
  51. public function __construct($selector, $content, array $settings = NULL) {
  52. $this->selector = $selector;
  53. $this->content = $content;
  54. $this->settings = $settings;
  55. }
  56. /**
  57. * Implements Drupal\Core\Ajax\CommandInterface:render().
  58. */
  59. public function render() {
  60. return [
  61. 'command' => 'insert',
  62. 'method' => NULL,
  63. 'selector' => $this->selector,
  64. 'data' => $this->getRenderedContent(),
  65. 'settings' => $this->settings,
  66. ];
  67. }
  68. }