1

In this case, ‘Volume’ is a text field of type integer. Using the Views module, I want to display only those articles which have the highest value in the volume field.

I am able to do it using contextual filter but I have to provide a default value like 6. Please see the attached screenshot.

Is there any way in views, to detect the highest value of the field and use that as a filter?

enter image description here

2
  • If you bounty this, I will give you a working solution. There is no way using the views UI, but I know of a workaround solution using php.
    – No Sssweat
    Commented Mar 1, 2016 at 11:26
  • @Suresh Alagar, did you find a solution? I am quite interested if you did. Thanks.
    – Toki
    Commented Jun 22, 2016 at 15:16

2 Answers 2

0

I fear you will need a subquery for that. You can do that in a custom module.

/**
 * Implements hook_views_query_alter().
 */
function MODULE_NAME_views_query_alter(&$view, &$query) {
  $max = db_query('select max(YOUR_TABLE_COLUMN) from YOUR_TABLE')->fetchField();
  $query->add_where(0, db_and()->condition('TABLE.YOUR_FIELD_NAME', $max));
}

The tricky part here is to create your select query and find the correct field name/alias. Have a look at the query output of your view to find the correct parts.

Original answer (before clarification)

Instead of a contextual filter you can use a sort on the 'Volume' field. Sort descending. That way the articles with the highest 'volume' are at the top.

If you want to show only articles with a number higher than X you could add a regular filter for that.

2
  • No, actually my requirement is to show only the list of articles who has highest value in "Volume" filed. Right now the 6 is the highest. So, only top two articles should be shown. If the highest is 8, then only the list of articles having 8 in volume should be displayed. Others should not get displayed. Commented Feb 28, 2016 at 7:09
  • I changed my answer and your initial question in accordance to your comment. If you need more help you should provide an export of your view and maybe a bounty.
    – dmsmidt
    Commented Mar 1, 2016 at 12:10
1

Well, I made it in a tricky way. I created a first view, with just two fields in it: Volume and Issue. Once hidden both, I rewrited them in a custom text field, in a way like this: <<p>Magazine nr. [field_issue] / [field_volume]</p> Ordered by Volume DESC and Issue DESC. Showing just 1 item. So I have always displayed the last issue. Then I created another view, to display my articles as I wanted, and I added 2 contextual filters: Volume and Issue. Finally I embedded the second view in the first as a field, passing from first to second the values of the two fields. So I get the view as attached. Of course if you don't want to display the "Magazine nr. etc." you can hide it by css.screenshot of result

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.