Problem/Motivation
When switching an old D7 website to PHP 7.4 I get the following notices at several points.
Notice: Trying to access array offset on value of type null in entity_extract_ids() (line 8000 of /home/kwbirds/public_html/includes/common.inc).
Notice: Trying to access array offset on value of type null in entity_extract_ids() (line 8001 of /home/kwbirds/public_html/includes/common.inc).
It seems that whenever a taxonomy term is rendered in a custom view mode it comes with empty entity_keys and this causes the lines 8000 and 8001 in includes/common.inc to through the above notices. The same notices are produced in the "Manege Display" page of the same content type.
This might have to do with Display Suite through which the custom view modes are created, but the issue would remain that the entity_extract_ids function can not cope with empty entity keys under PHP 7.4.
Lines 8000 and 8001 of common.inc:
$id = isset($entity->{$info['entity keys']['id']}) ? $entity->{$info['entity keys']['id']} : NULL;
$vid = ($info['entity keys']['revision'] && isset($entity->{$info['entity keys']['revision']})) ? $entity->{$info['entity keys']['revision']} : NULL;
As a temporary workaround and trying to understand what is happening I replaced the above two lines with the following which avoids the notices and also displays some information about the missing entity keys:
if (!empty($info['entity keys'])) {
$id = isset($entity->{$info['entity keys']['id']}) ? $entity->{$info['entity keys']['id']} : NULL;
$vid = ($info['entity keys']['revision'] && isset($entity->{$info['entity keys']['revision']})) ? $entity->{$info['entity keys']['revision']} : NULL;
}
else {
$id = '';
$vid = '';
$testmsg = $entity_type . " Empty entity keys";
echo htmlspecialchars($testmsg, ENT_QUOTES, 'UTF-8');
}
Here the taxonomy term is rendered in the header of a view (look for the text "term Empty entity keys"):
http://www.kuwaitbirds.org/birds/conservation-status/vulnerable
And here two taxonomy term reference fields are shown in custom view modes too:
http://www.kuwaitbirds.org/birds/levant-sparrowhawk
Issue fork drupal-3166668
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
Neeraj420 CreditAttribution: Neeraj420 as a volunteer commented@arx-e Have you got any solution for this issue?
Comment #3
arx-e CreditAttribution: arx-e commentedI made this patch where I got rid of the "if .. else" by nesting the ternary operator statements. I tested it i a couple of Drupal 7 sites and it applies cleanly and works to avoid the related error messages.
Comment #4
arx-e CreditAttribution: arx-e commentedComment #5
Neeraj420 CreditAttribution: Neeraj420 as a volunteer commented@arx-e
Though I have not done a detailed testing, however issue was resolved once changes were made directly to common.inc file. I wish if this can be committed into the main branch of Drupal core.
Comment #6
vlad.dancerHey @arx-e.
> It seems that whenever a taxonomy term is rendered in a custom view mode it comes with empty entity_keys
The same thing for me too.
Thanks for the patch. The code looks good to me.
I've tested on Drupal 7.78 under PHP 7.4.10, the patch applied without problems.
Let's push it further!
Comment #7
arx-e CreditAttribution: arx-e commentedThis is the same patch against Drupal 7.79.
Comment #8
Myddna CreditAttribution: Myddna commentedHi!
Just tested the patch in #7 against 7.80 and it applied OK.
Comment #9
arx-e CreditAttribution: arx-e commented@Myddna
That shouldn't be OK!
The common.inc file has change between versions 7.79 and 7.80 and 6 lines have been added before this point (line 8013 is now line 8019).
So a new patch should be created which I will try to do ASAP.
Comment #10
arx-e CreditAttribution: arx-e commentedThis one is against Drupal 7.80
Comment #11
MaxMendez CreditAttribution: MaxMendez commentedFixed my problem the patch #10, thanks for your work
Comment #12
akhilavnairHi,
Patch #10 works fine. Could you please merge this to Drupal 7.80.
Comment #14
mcdruidThanks for the patch.
However, I'd prefer not to see the ternaries wrapped in more ternaries.
How about something simple like this patch?
Also, this should ideally have tests. They could be very basic.
Comment #15
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedRTBC + 1
Comment #16
arx-e CreditAttribution: arx-e commented@mcdruid As you can see in the original post that was my first though and try. But my experience with code and best practices is very limited and I didn't know that nested ternaries are something unwelcome. I wet with them as they resulted in less code/changes.
And I don't have an inkling of how one would add tests. Sorry!
Comment #18
joseph.olstadPrefer patch 14 after comparing with the MR.
Thanks
Comment #21
akhilavnairComment #22
jenlamptonConfirming latest patch in #14 still applies cleanly and resolves the issue on PHP 7.4.
I'm not sure of the status of the MR. Are all the commits showing here just rebases from 7.x?
Comment #23
mcdruid...yes, looks like it.
I'd still like to see some (very basic) tests for this.
Comment #24
alrueden CreditAttribution: alrueden as a volunteer commentedPatch at #14 works for me (albeit the line numbers have changed now).
Acquia is updating all hosted D7 sites to PHP 7.4 on October 4, 2021. Any chance this could get committed before that happens? I'd love to not have a core patch!
Comment #26
igorbarato CreditAttribution: igorbarato at Twel commentedI updated the MR with the latest Drupal core updates and tested them.
Comment #27
Bhanu951 CreditAttribution: Bhanu951 as a volunteer commentedConfirming latest MR in #25 applies cleanly and resolves the issue on PHP 7.4.
+1 from me for RTBC.
Comment #28
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commentedWhile I was experimenting with a possible test to this issue I found out that the problem is a little bit different as it is presented in the issue summary.
I was able to simulate the notice (somehow playing with display_suite settings) and it was not caused by empty
entity keys
, but the whole empty$info
variable. See longer decription below:------------
The if condition in the issue summary does not only cover this one possibility, it could also be a case where the whole
$info
variable is empty:This code will reach the else block if
entity keys
are empty OR if the$info
variable is empty. The variable will be empty everytime you callentity_get_info()
with a name of non-existing entity.I was not able to simulate the problem with empty
entity keys
and while digging into it, it seems like that the functionentity_get_info()
always set the content ofentity keys
:So even in case that the returned entity has empty
entity keys
, this function will set at least the array with empty revision and empty bundle. Only way how this could be empty would be if thedrupal_alter()
called afterwards will unset it and that is unlikely to happen...-----------
So my testing revealed that the display suite is (in some circumstances) calling the
entity_get_info()
with the$entity_type = 'term'
. This will cause the$info
variable to be NULL, because the correct entity type for taxonomy terms is'taxonomy_term'
.There is a clear Display Suite bug - they are changing the
$entity_type
variable for the entity retrieval from the$vars
array, but also passing the changed variable to theentity_extract_ids()
without setting it back.------------
But anyway, I think it would be good to sanitize this also on the Drupal core side, because this situation is not handled correctly and the NULL value is used as an expected array later (without check what
entity_get_info()
returned). This can cause issues in PHP 7.4+.So if we check the
$info
variable, it will create a better code (from my point of view). See my patch proposal. What do you think?Comment #30
mcdruidI think this looks good, thank you!
Is there an issue filed for the problem in Display Suite described in #28?
Comment #31
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedRTBC again, let's get this in!
Comment #32
poker10 CreditAttribution: poker10 at ActivIT s.r.o. commented@mcdruid Yes, it seems like it is: #3196015: Degradation: Re-apply the fix for entity_extract_ids in ds_entity_variables
Comment #34
mcdruidThank you everybody!