Guest User

Untitled

a guest
Jan 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. <?php
  2. /** * @file * Contains Drupalajax_exampleAjaxExampleForm */
  3.  
  4. namespace Drupalajax_exampleForm;
  5.  
  6. use Drupalajax_exampleAjaxAjaxExampleCommand; use DrupalCoreAjaxAjaxResponse;
  7. use DrupalCoreAjaxCommandInterface;
  8. use DrupalCoreAjaxChangedCommand;
  9. use DrupalCoreAjaxCssCommand;
  10. use DrupalCoreAjaxHtmlCommand;
  11. use DrupalCoreAjaxInvokeCommand;
  12. use DrupalCoreFormFormBase;
  13. use DrupalCoreFormFormStateInterface;
  14.  
  15. class AjaxExampleForm extends FormBase{
  16.  
  17. /**
  18. * @param array $form
  19. * @param FormStateInterface $form_state
  20. *
  21. * @return array
  22. */
  23. public function buildForm(array $form, FormStateInterface $form_state) {
  24.  
  25. $form['submit'] = array(
  26. '#type' => 'button',
  27. '#value' => 'Login',
  28. '#attached' => array(
  29. 'library' => array(
  30. 'ajax_example/ajaxCustom','core/jquery')),
  31. '#ajax' => array(
  32. 'callback' => 'Drupalajax_exampleFormAjaxExampleForm::ajaxCallOwn',
  33. 'event' => 'click',
  34. 'progress' => array(
  35. 'type' => 'throbber',
  36. 'message' => 'ajaxOwn',
  37. ),
  38. ));
  39. }
  40.  
  41. public function ajaxCallOwn(array &$form, FormStateInterface $form_state)
  42. {
  43. $ajax_response = new AjaxResponse();
  44.  
  45. //$ajax_response->addCommand(new HtmlCommand('#edit-user-name2--description', "changedDessrition!!!!"));
  46.  
  47. $ajax_response->addCommand(new InvokeCommand(NULL,'changeDescription'));
  48. return $ajax_response;
  49. }
  50.  
  51. <?php
  52.  
  53. /**
  54. * @file
  55. * Contains Drupalajax_exampleAjaxAjaxExampleCommand
  56. */
  57.  
  58. namespace Drupalajax_exampleAjax;
  59.  
  60. use DrupalCoreAjaxCommandInterface;
  61.  
  62. class AjaxExampleCommand implements CommandInterface{
  63.  
  64. /**
  65. * @var string
  66. */
  67. protected $blub;
  68. // Constructs a SystemBlubCommand object.
  69.  
  70. /**
  71. * Constructs an SlideDownCommand object.
  72. * @param string $blub
  73. */
  74.  
  75. public function __construct() {
  76. $this->blub = "changed!!!!";
  77. }
  78.  
  79. public function render() {
  80. return array(
  81. 'command' => 'changeDescription',
  82. 'method' => NULL,
  83. 'blub' => $this->blub,
  84. );
  85. }
  86. }
  87.  
  88. (function ($, Drupal) {
  89.  
  90. /**
  91. * Command to Slide Down page elements before removing them.
  92. *
  93. * @param {Drupal.Ajax} [ajax]
  94. * @param {object} response
  95. * @param {string} response.blub
  96. * @param {number} [status]
  97. */
  98.  
  99. Drupal.AjaxCommands.prototype.changeDescription = function (ajax, response, status) {
  100.  
  101. alert("asdfasdgf");
  102. $("#edit-user-name--description").remove();
  103. $("#edit-user-name--description").html(response.blub);
  104.  
  105. };
  106.  
  107. })(jQuery, Drupal);
  108.  
  109. ajax_example:
  110. version: 1.x
  111. js:
  112. js/ajaxCustom.js: {}
  113. dependencies:
  114. - core/drupal
  115. - core/drupal.ajax
  116. - core/jquery
Add Comment
Please, Sign In to add comment