I have a view showing nodes of two different types sorted by date. I want to limit the maximum number of nodes shown of the one type by, say, 25% percent of the total nodes per page.
How would I go about that?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI have a view showing nodes of two different types sorted by date. I want to limit the maximum number of nodes shown of the one type by, say, 25% percent of the total nodes per page.
How would I go about that?
This might be a job for _views_pre_render(). In it, you can go through your views results and modify them to your module's content. The issue here is creating a view result big enough to make your percentages work. For example, let's say you want to have that view of 4 nodes, but only 1 of which is of a certain type. Since you are sorting by date, if the first 4 are all of that type, you grab one but need to keep looking for 3 of the other type.
In a dull ax approach, you could, though, create a view that returned 100, or however many is reasonable on your site based upon what you actually see re mix of nodes, of both types sorted by date and then do something like:
function YOURMODULE_views_pre_render(&$view) {
if ($view->name=='NAMEOFTHEVIEW') {
// drupal_set_message('<pre>' . print_r($view->result[0], true) . '</pre>'); // just the first row
// drupal_set_message('<pre>' . print_r($view->result, true) . '</pre>'); // all of them
$max=1; // hardcoded for example purposes
$total=4; // hardcoded for example purposes
$new_result=array();
foreach ($view->result as $result) {
if ($max && $result->FIELDTHATHASTHENODETYPEINIT==NODETYPEWEARELOOKINGFOR) {
$new_result[]=$result;
$max--; $total--;
} elseif (--$total) {
$new_result[]=$result;
}
}
$view->result=$new_result;
}
// any other views_pre_render()ing here
}
which if my logic gene is in gear today will cycle through your view results and pick out at most $max
nodes of one type and then fill up $new_results
up to $total
with the rest of the view results and then replace the results of the view with that.
The issue, again, is that if all the results are of one node type, or there aren't enough of the other node type to fill up $new_result
, you will have less than $total
results returned.
(One or both of the drupal_set_message()s will/might be helpful for figuring out FIELDTHATHASTHENODETYPEINIT. If there isn't one in your view already, you can put one in and then not display/hide it if necessary.)
hook_views_query_alter
might be another good starting point.
Aug 1, 2012 at 17:08
A simple way will be to:
create a view that will show X amount of 1 type of nodes that is equivalent to the 25% of the total nodes you want to display.
Then add an attachment (not EVA) view and attach it after the first one, but overwrite the filter indicating the content type selected for the attachment view only.
Repeat the step 2 as many times as needed to show the 100% of the total nodes from 25% of different content types.
Note: if you add footer and header HTML or text to the main view when adding the attachment view they will be copy over, but you can overwrite them so they will not show.
End result width a limit of 4 node will be like:
Thank you.