Steps to reproduce:

  1. Install Drupal using the standard profile.
  2. Add a text field to articles.
  3. Create at least one article and one basic page.
  4. Create a view that shows all content, and displays fields. Add the text field you created above as one of the fields in the view.
  5. Turn on aggregation for the view. The result is you get an AJAX error while editing the view, and a whitescreen when accessing the view after it is saved: "Fatal error: Cannot access empty property in field.attach.inc on line 325".

I believe this is mostly a bug in Drupal core - see #1161708: Problem with function _field_invoke when used on a content type with no field instance. However, I started working on this as a Views bug before I realized that. Plus, it makes sense to have an issue for it in the Views queue since that's where people will find it.

I think it is possible to patch Views to fix it; I'm attaching the patch I worked on before I came across the core bug. This patch seems to work, but probably not worth applying it to Views unless the core bug doesn't get fixed soon.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

David_Rothstein’s picture

Title: Fatal error when using aggregation and a field is not atttached to all entities in the view » Fatal error when using aggregation and a field is not attached to all entities in the view
dawehner’s picture

Status: Active » Needs work

Oh wow this totally makes sense, things can be quite easy somtimes.
First i thought this is related to http://drupal.org/node/1417520 but it's not.

+++ b/modules/field/views_handler_field_field.incundefined
@@ -770,7 +770,7 @@ class views_handler_field_field extends views_handler_field {
+    if (empty($values->_field_data[$this->field_alias]) || empty($values->_field_data[$this->field_alias]['entity']) || !isset($values->_field_data[$this->field_alias]['entity']->{$this->definition['field_name']})) {

A comment would be really helpful here. What kind of "edge" cases can appear. At least for the one you added it's actual a real logical and reproducable reason.

Yuri’s picture

This fatal error is still happening.

Pimmy’s picture

Same here: For me it happens when I try to aggregate on fields which do not have value.

I've been struggling with this about a year ago and eventually managed to work around this bug by adding default value of 0 (zero) to all fields participating in the view.

Now however I cannot do this due to complexity with 'Field Collections' and other content, so I would really need help with fixing this.

Thanks,

Pimmy

karangi’s picture

I narrowed it down to a view that always gave the following ajax pop up error:

An AJAX HTTP error occurred. HTTP Result Code: 200 Debugging information follows.
Path: xxxx/admini/structure/views/view/general/preview/ block/ajax
StatusText: OK
ResponseText: Fatal error: Cannot access empty property in "path to module \ modules\ fielthfield.attach.inc on line 325"

So I removed and redid the view and it solved the problem.

Pimmy’s picture

patch makes the error disappear, but aggregation still fails to work for columns containing empty results.
See example: http://drupal.org/node/1451894

redoing the view makes no difference.

joerch’s picture

Version: 7.x-3.x-dev » 7.x-3.5

Same problem with a commerce-kickstart installation.
Glad to know a little more about the problem now.

For me it happens when I try to aggregate on fields which do not have value.

Seems to be the same here. Need to review this.

dkdnt’s picture

Version: 7.x-3.5 » 7.x-3.x-dev

I have this error disabled aggregation in views (D7.19)

"Fatal error: Cannot access empty property in field.attach.inc on line 325"

and that when I refresh other site or add content:

Notice: Undefined index: w _field_invoke_multiple() (linia 310 z ... /modules/field/field.attach.inc).

patch not help :/

These errors pop up when printing field and displays the node does not contain the field.
This code in the page.tpl.php makes these errors:

$g_field_name = field_get_items('node', $node, 'field_name');
$v_field_name = field_view_value('node', $node, 'field_name', $g_field_name[ 0 ]);

print render($v_field_name);

but this will avoid page--node-type-with-field_name.tpl.php

sorry for my English;)

JMOmandown’s picture

Not that it is specific to this module, but back to David_Rothstein's point. This is not a bug inherent to views, we experience this on a production site with the commerce module when trying to pass information to another entity that is using the same field as an existing field (another field instance).

GemVinny’s picture

I am also getting the same issue, redoing the view didn't fix it for me either.

Has anyone had any joy?

Thanks
Gem

GemVinny’s picture

I worked out what my issue was!

I didn't set the filter criteria to the content type so it was bringing back all content types. Obviously this is going to cause issues because it's bring back text as well as numbers...

So I just disabled Aggregation added in the filter criteria to filter only that content type, enabled aggregation and it worked.

Hopefully this will help someone else.

Gem

Anonymous’s picture

Thanks David_Rothstein, you're a hero. The patch attached to the original posting works like a charm.

Applies for views with different content types and aggregation and usage of a field which is not part of all involved content types... very specific case... the more thankful I am about your patch! :)

David_Rothstein’s picture

Thank you, @Shnapoo! Glad it helped.

Hopefully the root cause (#1161708: Problem with function _field_invoke when used on a content type with no field instance) can be fixed in Drupal core too.

Sabareesh’s picture

@David_Rothstein : Your path works well and solves this issue. Great work David_Rothstein!

Sabareesh’s picture

Status: Needs work » Closed (fixed)

Based on #12 and my own experience I am closing this issue.

David_Rothstein’s picture

Status: Closed (fixed) » Needs work

Thanks, @Sabareesh. But this patch hasn't been committed yet as far as I know, so the issue should remain open.

Sabareesh’s picture

@David_Rothstein : Yeah I forgot that and closed this ticket, but thanks again for your work!

Exploratus’s picture

Worked for me. Thanks!

femtox77’s picture

@David_Rothstein : Worked for me. Thanks!

neorg’s picture

Thanks @David_Rothstein

Patch in original pors worked for me.

Problem occurred on MERCI module https://drupal.org/project/MERCI White Screen of Dead ( WSOD )
Error in apache log:
PHP Fatal error: Cannot access empty property in /var/www/[DOMAINNAME]/html/modules/field/field.attach.inc on line 324

Drupal core: 7.23
Views: 7.x-3.7

I really wonder why this patch isn't still committed in the Views module.

dawehner’s picture

Status: Needs work » Fixed

Added a line of documentation for myself and committed the initial patch.

Drupal 8 looks way different so this don't have to be ported.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

rudiedirkx’s picture

This is not a Views bug. It's a bug in _field_invoke_get_instances() that doesn't check if the field exists in the bundle, so the results is sometimes array(NULL), which is obviously incorrect. Adding an array_filter() fixes the problem, in the right place, not in Views.

return array_filter($instances);

but that's core, so that's never gonna change...

David_Rothstein’s picture

Yes, this is a core bug - see #1161708: Problem with function _field_invoke when used on a content type with no field instance which I linked to in the original issue report.

rudiedirkx’s picture

Oh wow I completely missed that. My bad. I created a core bug that I'll now close.

more770’s picture

Issue summary: View changes

I have the same error, but I can't solve it with David_Rothstein patch.
Any other solution?
I'm using Drupal 7.21