2

In drupal i need to output json fields. So i used views_datasource. In my situation, i need to call same api source to multiple section, but i don't want to create multiple view only for 5 items or 10 items or all items for same content types data. So in views_datasource i need to use limit filter from all of views results like "http://example.com/serve/all-articles?limit=10".

Can anybody help me to do it?

2 Answers 2

0

Ccreate a custom module and implement hook_views_pre_build.

To create the module follow these steps:

In sites/all/modules create a new folder with the name of your module. As you are not contributing this module back to Drupal and it is specific to your project just give it the name of your project and other code tweaks can also live in there.

Let's assume you called your project custom_project and it is at sites/all/modules/custom_project.

Add a file called custom_project.info to it with this content:

name = Custom Project Module     
description = This module is for project related tweaks     
core = 7.x

Add a file called custom_project.module with this content:

<?php
function custom_project_views_pre_build(&$view){
  if ($view->name == "NAMEOFVIEW") {
    if (isset($view->args[0]) && is_numeric($view->args[0])) {
      $view->set_items_per_page($view->args[0]);
    }
  }
}

Now install this module (and in future if you add any hooks then clear the cache)

You call this on a friendly url ("http://example.com/serve/all-articles/10").

If you wish to add a query parameter to the url as in "http://example.com/serve/all-articles?limit=10" then your function will need to inspect drupal_get_query_parameters for that parameter instead of pulling it out of $view->args[0].

ie.:

<?php
function custom_project_views_pre_build(&$view){    
  if ($view->name == "NAMEOFVIEW") {    
    $query_params = drupal_get_query_parameters();    
    if (isset($query_params['limit']) && is_numeric($query_params['limit'])) {    
      $view->set_items_per_page($query_params['limit']);    
    }    
  }    
}

You need to replace NAMEOFVIEW with your views name. You get it like this:

enter image description here

Note that a view has a name, but also a display id. The display id in this case is page. If you add a block it will get a block display id. So the above code will work for all displays that you add to this view, even for blocks etc. To limit it to just the page you will change the one line to this:

if ($view->name == "all_content_of_article" && $view->current_display == "page") {
6
  • Hi @J. Reynolds, Thanx for quick reply. I m new to develop drupal module, can you please help me to setup above functions into .module file? :( sorry for this request...
    – Tazim
    Sep 27, 2015 at 4:00
  • OMG... :) thanx your response. Now i trying to implement your codes. I'll inform you after sometime...
    – Tazim
    Sep 27, 2015 at 8:35
  • Oh Man... You are genius... It's working!!! :) Now i add offset query with your codes myself.
    – Tazim
    Sep 27, 2015 at 9:11
  • Reynonls, If you don't mind, may can i ask a new question?
    – Tazim
    Sep 27, 2015 at 9:12
  • Yes just post it as a new question Sep 27, 2015 at 9:24
0

Is using another display in the same view out of the question? It would be a lot easier than a programmatic approach in my opinion?

You could then have all your base configurations on the view main display and create another display with the single override?

If you did go the custom route, you may have to deal with the more complex view caching or disable the views cache on this view.

2
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
    – Mołot
    Sep 26, 2015 at 17:00
  • 1
    I'd disagree, it says use this method instead of dealing with the headache that could be caused using the programmatic approach. Sep 26, 2015 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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