If I disable the Comment module, I get multiple PHP 7.4 warnings at line 123 and several others in field_views_field_default_views_data():

Trying to access array offset on value of type null in field_views_field_default_views_data()

This happens when I rebuild the cache, for example. From a quick look, entity_get_info('comment') returns NULL if the Comment module is disabled.

Comments

nwellnhof created an issue. See original summary.

damienmckenna’s picture

Category: Bug report » Support request

entity_get_info('comment') is only in modules/comment/views_plugin_row_comment_view.inc, which should only be loaded if a view touches Comment data. Might you still have some views enabled which query Comment data?

kevin morse’s picture

I am also seeing this warning.

Also running PHP 7.4 and also have the Comment module disabled. Is there any way to add more info to the log file to see which View is responsible? I checked all the Views that I created and none should be accessing the Comment module.

damienmckenna’s picture

You might check if the "Recent comments" view is enabled.

trevorbradley’s picture

I similarly had to enable the Comments module on a site that didn't use comments to make this error go away.

The Recent Comments view was disabled.

alfazaz’s picture

I have the same warning on php 7.4.8 with comment module enabled on cache clear :

Notice : Trying to access array offset on value of type null in field_views_field_default_views_data() (line 123 in /home/******/sites/all/modules/views/modules/field.views.inc).

There are other warnings for this file at lines 123, 135, 136, 142, 143 and also

Notice : Trying to access array offset on value of type null in taxonomy_field_views_data_views_data_alter() (line 471 in /home/******/sites/all/modules/views/modules/taxonomy.views.inc).

and also at lines 483 and 484...

There is no change to add to views 7.x-3.24 to make it php 7.4 totally compatible ?

wjackson’s picture

Status: Active » Needs review
StatusFileSize
new3.56 KB

I was able to reproduce this issue on a site without the core comment module installed and installing did not resolve the issue. In my instance, the issue stemmed Corresponding Entity References (cer) module and views attempting to return entity info from field.views.inc.

$entity_info = entity_get_info($entity);

I was able to resolve this by first checking if the results of entity_get_info() is not empty, if not then the script will continue to build using entity data.

tondeuse’s picture

Trying to access array offset on value of type null on lines 123, 135, 136, 142, 143 are resolved with the patch provided in #7. They flare up upon cache clear with PHP 7.4, Drupal 7.72 and Views 7.x-3.24. I would love to be able to pinpoint the origin of this issue and fix it upstream, my local site is pretty simple, but does have a fair amount of custom code.

derekw’s picture

#7 removed a bunch of errors but I'm still getting them for taxonomy.views.inc

Notice: Trying to access array offset on value of type null in /var/www/drupalvm/repository/sites/all/modules/views/modules/taxonomy.views.inc on line 471

Notice: Trying to access array offset on value of type null in /var/www/drupalvm/repository/sites/all/modules/views/modules/taxonomy.views.inc on line 483

Notice: Trying to access array offset on value of type null in /var/www/drupalvm/repository/sites/all/modules/views/modules/taxonomy.views.inc on line 484

Notice: Trying to access array offset on value of type null in /var/www/drupalvm/repository/sites/all/modules/views/modules/taxonomy.views.inc on line 484

kevster’s picture

#7 - that seems to have fixed my issue thanks on a commerce site running 7.72 and views 3.24 on PHP 7.4

I now have another one ref entityreference - very similar

Kashalinka’s picture

Patch #7 worked for me. Am running Drupal 7.74 with PHP 7.4.12. Thanks very much, wjackson.

klausi’s picture

We saw the same PHP warnings when testing with PHP 7.4 and noticed that we had a couple of modules disabled where the fields were still active. We will probably use an update function similar to this to clean that up:

/**
 * Uninstall old recruiter talentpool modules and OG if they are unused.
 */
function recruiter_jobiqo_update_7238() {
  if (!module_exists('og')) {
    // Will also uninstall dependent modules that are disabled.
    drupal_uninstall_modules(['og']);
    // OG might have left behind a field, clean that up.
    if (field_info_field('og_membership_request')) {
      field_delete_field('og_membership_request');
    }
  }
  if (!module_exists('recruiter_talentpool')) {
    $fields = ['field_talentpool_ats_node', 'field_talentpool_resume_ref'];
    foreach ($fields as $field) {
      if (field_info_field($field)) {
        field_delete_field($field);
      }
    }
  }
  field_purge_batch(1000);
}
solideogloria’s picture

Version: 7.x-3.24 » 7.x-3.x-dev
Category: Support request » Bug report
StatusFileSize
new549 bytes

Rather than putting everything inside an if block, I think it looks cleaner to use continue;. Marking RTBC, since the code is functionally the same.

solideogloria’s picture

Status: Needs review » Reviewed & tested by the community
klausi’s picture

I think the patch only covers up a problem in your system: you have active fields that refer to not existing entity types.

Would it make sense to write a watchdog() message before the "continue;"? Like "Field @field_name refers to not existing entity type @type. You might want to remove this broken field."

solideogloria’s picture

I debugged to see which it was. The fieldname is comment_body and the entity is comment. The Comment module was previously installed but now is not.

Using the following (from #12) worked:

    if (field_info_field('comment_body')) {
      field_delete_field('comment_body');
    }
solideogloria’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new812 bytes

Added watchdog message as a warning. I was a bit unsure whether to do notice or a warning.

rasikap’s picture

Status: Needs review » Reviewed & tested by the community

Patch #17 works for me.

oadaeh’s picture

The wording of this is odd: "...refers to not existing entity type..."
I think it would read better to use: "...refers to a nonexistent entity type..."
or: "...refers to an entity type that does not exist..."

solideogloria’s picture

Ah, yeah I just copied #15's suggestion

argiepiano’s picture

Patch #17 helped me debug and fix this issue. In my case, instances of the field were still in use in other existing entity types, so deleting the field was not an option. The nonexistent entity type was defined by a module that was no longer installed. The solution for me consisted in finding the instance entry in the SQL table field_config_instance and manually removing it. I realize acting directly on the database is not recommended, but I couldn't find any other solution. field_info_instance did not return anything, and therefore field_delete_instance did not work.

solideogloria’s picture

StatusFileSize
new811 bytes

Improved log message wording.

solideogloria’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new811 bytes

Decided it's better with emphasized variables (% instead of @). Also, I got the comment # correct this time.

solideogloria’s picture

To address the concerns in #21, I found that I needed to call field_read_instances() directly and use include_inactive.

The following code works to fix the underlying data issue (by deleting the broken field instance), AND it only deletes the field if the last instances where it was used were just deleted.

$instances = field_read_instances(array('field_name' => $field['field_name'], 'entity_type' => $entity), array('include_inactive' => TRUE));
if (!empty($instances)) {
  foreach ($instances as $instance) {
    field_delete_instance($instance, TRUE);
  }
}

The parameters $field['field_name'] and $entity variables are the same as in the place where this patch applies. In other words, the values shown in the watchdog warning message can be substituted into the code directly and run.

If someone else can verify the above code, we could include the code in the watchdog message or something.

ckoharj’s picture

#23 gives another error now when caches are cleared
Field comment_body refers to nonexistent entity type comment. You might want to remove this broken field.

Error goes away if Comments module is enabled so it seems best solution is to just enable Comments module

solideogloria’s picture

@ckoharj It is supposed to give that warning. This is because there is a data issue that you have to fix manually. Read the comments, and read #24 for the data fix.

ckoharj’s picture

Thanks @solideogloria but I don't understand how to remove that broken field
Field comment_body refers to nonexistent entity type comment. You might want to remove this broken field.

solideogloria’s picture

Use the Devel module, go to /devel/php, and run the following code:

$field_name = 'comment_body';
$entity = 'comment';

$instances = field_read_instances(array('field_name' => $field_name, 'entity_type' => $entity), array('include_inactive' => TRUE));
if (!empty($instances)) {
  foreach ($instances as $instance) {
    field_delete_instance($instance, TRUE);
  }
}

Make sure to disable or remove the Devel module on production sites after you are done doing this.

ckoharj’s picture

@solideogloria thank you for your time.
That fixed it

Kashalinka’s picture

Patch #24 worked for me. Thanks, solideogloria. Your work is much appreciated.

solideogloria’s picture

Do people think that the code in #28 should be included anywhere? Maybe as a hyperlink/action in the log messages?

damienmckenna’s picture

It could be added as a documentation page, with the error message linking to that page?

solideogloria’s picture

I created a new documentation page.

https://www.drupal.org/docs/7/modules/views/data-fix-field-field-refers-...

Once it has been reviewed, I can update the patch so the log message points to it.

damienmckenna’s picture

Status: Needs review » Needs work

The docs page has been published, thank you for putting that together!

solideogloria’s picture

Status: Needs work » Needs review
StatusFileSize
new957 bytes
new694 bytes
adalbertov’s picture

Status: Needs review » Reviewed & tested by the community

Hello Just checked the latest patch (#35). It was properly pointing to the right URL in the comments and at least for me the doc seems ok, so I'm moving the issue to RTBC

renatog’s picture

Status: Reviewed & tested by the community » Needs work

We need to change the array to the new syntax

array('%field_name' => $field['field_name'], '%type' => $entity),

Using

[
  'key' => 'value'
]
solideogloria’s picture

Status: Needs work » Reviewed & tested by the community

Drupal 7 still supports older versions of PHP that are pre-5.4. This module doesn't specify php=5.4 in the info file, so it still should support those older versions.

damienmckenna’s picture

Agreed - there are a lot of Drupal 7 sites stuck on PHP 5.3 that can't move off it, breaking compatibility for a minor change like this doesn't make sense.

damienmckenna’s picture

StatusFileSize
new879 bytes
new990 bytes

I wonder would it be better to list the node ID for the docs page, in case the URL changes? That would make it https://www.drupal.org/node/3200406.

damienmckenna’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thank you.

Status: Fixed » Closed (fixed)

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

headbank’s picture

I assume the code snippet in #28 could also be run using drush php-eval or drush php-script?

If so maybe worth mentioning that as an option in the documentation page, to save installing Devel module.

Just a thunk...

solideogloria’s picture

I think that's obvious. Feel free to put the code all one one line and run using Drush.

loopy1492’s picture

I just downloaded a fresh views 7.x-3.x-dev for our PHP 8 upgrade and I am getting this after updates:

Warning: Trying to access array offset on value of type null in file_field_views_data_views_data_alter() (line 45 of /var/www/docroot/sites/all/modules/contrib/views/modules/file.views.inc).
Warning: Trying to access array offset on value of type null in file_field_views_data_views_data_alter() (line 57 of /var/www/docroot/sites/all/modules/contrib/views/modules/file.views.inc).
Warning: Trying to access array offset on value of type null in file_field_views_data_views_data_alter() (line 58 of /var/www/docroot/sites/all/modules/contrib/views/modules/file.views.inc).
Warning: Trying to access array offset on value of type null in file_field_views_data_views_data_alter() (line 58 of /var/www/docroot/sites/all/modules/contrib/views/modules/file.views.inc).

Drupal 7.8.9.

damienmckenna’s picture

@loopy1492: Please open a new issue for that. Thank you.

dangur’s picture

And for those who need a modern PHP array syntax with brackets:

$field_name = 'comment_body';
$entity = 'comment';

$instances = field_read_instances(['field_name' => $field_name, 'entity_type' => $entity], ['include_inactive' => TRUE]);
if (!empty($instances)) {
  foreach ($instances as $instance) {
    field_delete_instance($instance, TRUE);
  }
}