2
  • I've created a views block that loads all nodes of a certain content type.
  • I've added the Content: Has taxonomy term ID contextual filter

This works great. In the preview area if I change the value in the "preview with contextual filters" input field it changes the nodes that load below according to which taxonomy term ID I input.

How can I have this same functionality on a normal Drupal page?

I will have a list of links on the page with an href value of the taxonomy term ID. When I click a link, I need it to load all nodes that have been associated with that particular taxonomy term into an empty div using AJAX.

I've read somewhere on here that if the nodes just contain some basic text then using a pure AJAX solution might not be the best but rather accomplish the same thing using views.

  • Which version of Drupal are you using? – Aram Boyajyan Mar 1 '16 at 18:16
1

Here is the javascript I ended up using to load a views block using AJAX and pass in a contextual filter from the href value of given link on page.

function getInfo(args) {

  $.ajax({
    url: Drupal.settings.basePath + 'views/ajax',
    type: 'post',
    data: {
      view_name: 'agent_lookup',
      view_display_id: 'agent_lookup_block', //your display id
      view_args: args,
    },
    dataType: 'json',
    success: function (response) {
        if (response[1] !== undefined) {
        var viewHtml = response[1].data;
        $('#ajax-target').html(viewHtml);
        //Drupal.attachBehaviors(); //check if you need this.
      }

    },
        error: function(data) {
         alert('An error occured!');
        }
  });
}




$('.ajax_button').once().click(function(e){
    e.preventDefault();
    var the_id = $(this).attr('href');
    noSlashes = the_id.replace(/\//g,'');
    getInfo(noSlashes);
});
0

Sounds like removing the contextual filter and instead exposing the taxonomy term filter in your view could be what you need.

Or for a more complicated solution, this could work well.

http://www.stevepolitodesign.com/blog/load-content-drupal-ajax-without-coding

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.