CssCommand.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace Drupal\Core\Ajax;
  3. /**
  4. * An AJAX command for calling the jQuery css() method.
  5. *
  6. * The 'css' command will instruct the client to use the jQuery css() method to
  7. * apply the CSS arguments to elements matched by the given selector.
  8. *
  9. * This command is implemented by Drupal.AjaxCommands.prototype.css() defined
  10. * in misc/ajax.js.
  11. *
  12. * @see http://docs.jquery.com/CSS/css#properties
  13. *
  14. * @ingroup ajax
  15. */
  16. class CssCommand implements CommandInterface {
  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. * An array of property/value pairs to set in the CSS for the selector.
  28. *
  29. * @var array
  30. */
  31. protected $css = [];
  32. /**
  33. * Constructs a CssCommand object.
  34. *
  35. * @param string $selector
  36. * A CSS selector for elements to which the CSS will be applied.
  37. * @param array $css
  38. * An array of CSS property/value pairs to set.
  39. */
  40. public function __construct($selector, array $css = []) {
  41. $this->selector = $selector;
  42. $this->css = $css;
  43. }
  44. /**
  45. * Adds a property/value pair to the CSS to be added to this element.
  46. *
  47. * @param $property
  48. * The CSS property to be changed.
  49. * @param $value
  50. * The new value of the CSS property.
  51. *
  52. * @return $this
  53. */
  54. public function setProperty($property, $value) {
  55. $this->css[$property] = $value;
  56. return $this;
  57. }
  58. /**
  59. * Implements Drupal\Core\Ajax\CommandInterface:render().
  60. */
  61. public function render() {
  62. return [
  63. 'command' => 'css',
  64. 'selector' => $this->selector,
  65. 'argument' => $this->css,
  66. ];
  67. }
  68. }