We have a pretty simple use case where we want to calculate a percentage - using a value from each parent row and dividing (using the views expression field) by a total sourced from a COUNT in a separate child display (that is field based, as this is a data table view). Even if we disable all value/field/row divs, we still have the view//display wrapper divs that prevent the expression from functioning - we would need to retheme the view to remove (or write a "raw" views style). It seemed simpler to access the value in views_field_view, so that is what this patch does.

In more detail, it simply extracts the value of the first field of the first row. It runs the value through check_plain(), which works fine for numeric values and calculations - it wouldn't work for non-numeric values, but generally I assume the rendered version is preferable in that case.

I am not sure how common this use case is - hopefully this simple function is useful to others. If we wanted to broaden the functionality, I think the ideal would be to provide a dropdown below the "display" one, and allow the user to select either "all rendered" or a specific field in either rendered or raw variations, combined with a "first row only" checkbox, that repeated the child first row field for all rows of the parent. Combined with a little caching (see subsequent issue) and row level logic, this could allow multiple views_field_view fields and hence multiple fields from child views to be incorporated somewhat seamlessly into the "parent" views, as well as more complex calculations.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

swamiman’s picture

Can this patch be applied to the new stable release?

heyddi’s picture

Hi,

I am trying to use this patch but it seems that I can't manage to make it work. Here is what I am doing:

I have a view (view1) where I aggregate results, it gives me a simple number, lets say "15". So this is the only thing I have in that view.

Then I have another view (view2) with multiple fields. I want to add the result of view1 to a field in view2. So I insert a "Global: view", I look for "view1", and insert it. So I now have in view2, a new field containing the result of view1.

Then I create a "Global: Math expression", I had my formula, let's say [view1] + 10. It should give me a result of "25" (15+10) but it doesn't count the value of view1.

Maybe I didn't understand the purpose of this patch, or maybe I am using it wrong, but if someone has an idea, it would be great!

Thank you

Update: I didn't see the checkbox to "Fetch value of first field of first row", now it gives mon the child view node number, and I can't manage to have something else.

2nd update: I manage to make it work with "count" aggregation, but when I use a sum it returns me the node ID. I am doing the sum of integer fields, it should be right no?

heyddi’s picture

After hours of research, I was not able to understand why it is working when child view is aggregated with count and not working when it is aggregated with sum. Maybe just that the patch is no made to work with sum?

Thank you

blackdotbug’s picture

I would love to be able to grab the results of the first field in the first row, even if it was text. I can see uses for using the results of a child view as a class for a row in the parent view.

jeremymcminn’s picture

Great patch, but I also am having the NID returned when attempting to use a SUM aggregation. Does anyone have any ideas why this is? Works perfect for COUNT, but my use case needs SUM. Would love to get this working.

cthshabel’s picture

This works great for the first field. I was hoping to easily add options for the second field of a view like this:

changing:

$first_row = (array)$view->result[0];

to this:

$second_row = (array)$view->result[1];

But that didn't work as expected..

Any help would be great understanding how the array is collecting this value?

MustangGB’s picture

Component: Miscellaneous » Code
FileSize
3.39 KB

Here is my version, this doesn't try to replace empty values but rather passes back the embeded view to the parent so it's field values can be accessed with a views_php field or custom theme in/of the parent view.

MustangGB’s picture

AlfTheCat’s picture

Status: Needs review » Needs work

Update: I didn't see the checkbox to "Fetch value of first field of first row", now it gives mon the child view node number, and I can't manage to have something else.

I'm not getting the checkbox at all, I updated to the latest dev and cleared caches...

I'm trying this patch to solve the issue of unwanted markup ending up in my view that holds the views field view.
I disabled all markup and style options everywhere, tried master view and page views display as well as using an attachment display...
It always outputs something like:

<div class="view view-xxxxx view-id-xxxxx view-display-id-default view-dom-id-b8989005b1fb63e6cb3cc408facbafa2 contextual-links-region">
        
  
  
      <div class="view-content">
        <div>
      
          xxxxx    </div>
    </div>
AlfTheCat’s picture

I now noticed that I was using the wrong patch, the original one provides the checkbox. However, it now no longer passes on the contextual filter/ argument.

Also, it removes formatting of the field itself. In my case, I am trying to add a user IP address from the ip module to a data export display. It now renders an ip address without the "." and as mentioned, not getting the correct IP due to the contextual filter no longer working.