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['#attributes'] = [



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.


For anyone needing help on this, as the information out there seems to be either related to forms only or seems rather over convoluted... ajax callback can be added to normal links, by defining the link as outlined above (in the question) - but adding the '/nojs/' prefix to the Url defining portion of the code and adding the class 'use-ajax' in the attributes section of the code e.g. :

// Define the link.
$url = Url::fromUserInput('/nojs/ajax_call_url');
$the_link = Link::fromTextAndUrl('Submit Application',$url);
$the_link = $the_link->toRenderable();

$the_link['#attributes'] = [

// Add the link to your render array. 
$output['link1'] = array(

Now in the 'name of your module'.routing.yml file add the route to where your ajax callback function is... as '/nojs/' is used (as it has to be by the looks of things) it means your path must be defined with the prefix '/ajax/' followed by the path of your choice e.g.:

path: '/ajax/call_ajax_url'

the _controller attribute in the routing.yml file, will be defined to point to where your ajax callback command is defined... e.g. will point to whatever object and function holds the

    $response=new AjaxResponse();
    $response->addCommand( new HtmlCommand("#current-msg",$message);
    return $response;

Hope this helps someone - as it took a while for me to find the info necessary to resolve this issue.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.