0

So I have links that are created when a page first loads that use a render array that is something like this:

array(
    '#type' => 'link',
    '#title' => html_entity_decode(t($title)),
    '#href' => $href,
    '#post_render' => array('_dva_post_render_ajax_link'),
    '#attributes' => array(
        'class' => array('ajax-link'),
        'rel' => url($href),
    ),
    '#ajax' => array(
        'path' => 'dva/ajax/'. $href,
        'wrapper' => 'content',
        'effect' => 'fade',
        'prevent' => 'mousedown',
        'progress' => array(
        'type' => 'none',
        'message' => '',
        ),
    )
);

These all work as expected and properly run the ajax and change the page content, etc.

However, when the page content also includes one of these links, the links included in the ajax response are just normal links and have no ajax functionality. The links are created using this same render array and they render correctly (that is, their markup appears the same as the links that work)

How can I tell Drupal to attach the ajax event handlers to the new links that have just been added to the DOM?

1

Try using the "use-ajax" class on the link

In addition to using Form API for doing in-form modification, Ajax may be enabled by adding classes to buttons and links.

By adding the 'use-ajax' class to a link, the link will be loaded via an Ajax call.

When using this method, the href of the link can contain '/nojs/' as part of the path. When the Ajax framework makes the request, it will convert this to '/ajax/'.

The server is then able to easily tell if this request was made through an actual Ajax request or in a degraded state, and respond appropriately.

Source: http://api.drupal.org/api/drupal/includes%21ajax.inc/group/ajax/7

e.g:

array(
    '#type' => 'link',
    '#title' => t($title),
    '#href' => $href,
    '#attributes' => array(
        'class' => array('use-ajax'),
        'rel' => url($href),
    ),
);
  • This didn't work. use-ajax seems to be completely ignored. – gregghz Jan 15 '13 at 15:29

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.