I have a situation where I would like to define a href link and when it is clicked, for it to trigger an ajax response such as sending back some information which can then be shown on the page...
Part 1. Creating the link... So I've created the link via:
$url = Url::fromUserInput('#');
$nxt_button=Link::fromTextAndUrl('Submit Application',$url);
$nxt_button=$nxt_button->toRenderable();
$nxt_button['#attributes'] = [
];
$output['link1']=array(
"the_link"=>$nxt_button,
);
Now in the '#attributes' I assume i could put something like 'callback' => 'name of function to be run' ... but, if the function to be run resides in another object say 'ajax_test_script.php' how would I do that?
Part 2. Say the server side responding code, was in a different class called 'ajax_test_scirpt' and the function to call is 'start' ... If I was sending back information via:
$response=new AjaxResponse();
$response->addCommand(<<what goes here>>);
return $response;
And these lines of code were inside the function 'start'... What would I put as the input parameter for addCommand, if I wanted the response to behave such that it could call the javascript function outlined below, which would be attached to the page (e.g call readMessage passing in the parameters needed by the readMessage function):
(function($, Drupal) {
/**
* Add new command for reading a message.
*/
Drupal.AjaxCommands.prototype.readMessage = function(ajax, response, status){
// Place content in current-msg div.
$('#current-msg h2').html(response.subject);
$('#current-msg p').html(response.content);
// Remove from unread list.
$('#msg-' + response.mid).remove();
// Add message to read list.
$('#read-msgs').append('<li>' + response.subject + '</li>');
}
})(jQuery, Drupal);
Any help is appreciated.