1

I have a link that uses Drupal's use-ajax CSS class to AJAX-load some node teasers. The callback for the link uses arguments to set a range in the Entity Field Query used to return the teasers:

function marketplace_ajax_list_menu() {
  $items = array();
  $items['marketplace_ajax_list_callback/%/%/%'] = array(
    'page callback' => 'marketplace_ajax_list_response',
    'page arguments' => array(1, 2, 3),
    'access callback' => 'user_access',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK
  );
  return $items;
}

function marketplace_ajax_list_response($type='ajax', $collection_nid, $start, $page_limit) {
  if ($type == 'ajax') {
    $output = marketplace_ajax_list_collection_latest_content($collection_nid, $start, $page_limit);
    $commands = array();
    $commands[] = ajax_command_append('#ajax-target', $output);
    $page = array('#type' => 'ajax', '#commands' => $commands);
    ajax_deliver($page);
  }
}

function marketplace_ajax_list_collection_latest_content($collection_nid, $start=0, $page_limit=10) {
  $q = new \EntityFieldQuery();
  $q->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', array('story', 'reader'))
      ->propertyCondition('status', 1)
      ->fieldOrderBy('field_date', 'value', 'DESC')
      ->fieldCondition('field_ref_collection', 'nid', $collection_nid, '=');

  // Get the matching items within the range
  $q->range($start, $page_limit);
  $result = $q->execute();
  $nids = array_keys($result['node']);

  // Make node teasers
  $nodes = node_load_multiple($nids);
  $teasers = node_view_multiple($nodes);
  $build = render($teasers);

  return $build;
}

When the node teasers are appended to the list, I have a jQuery function that updates the arguments in the AJAX link in order to update the range used in the query:

(function ($) {
  var page = 1;
  var page_limit = 10;
  var collection_nid = 12345;

  Drupal.behaviors.marketplace_ajax_list = {
    attach: function(context, settings) {
      page++;  
      var link = '/marketplace_ajax_list_callback/nojs/' + collection_nid +'/' + (page_limit * page) + '/' + page_limit;
      $moreBtn.attr('href', link); // $moreBtn is the AJAX link
    }
  };
})(jQuery);

The link's href changes correctly, with the updated arguments, but the new arguments aren't used in the callback's query - the same arguments are used each time, so the same items appear on each click.

If I take the value that jQuery sets in the href and open the link in another tab, I see the correct items.

So I think the issue has to do with the AJAX behavior getting attached to the link and that's not updated each time the function runs. Is there a command I can add to make that happen?

2

Solved! I found this post - https://drupal.stackexchange.com/a/52788/260 - which answered a very similar question, and that changed everything. This issue is essentially a duplicate of that one.

The fix here in my case was to not just change the link's href but to also update the Drupal.ajax object, like this:

Drupal.ajax['load-more'].options.url = link;

"load-more" is the ID of the AJAX link.

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.