Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I get a php fatal error when running drush cron on a drupal where Flag is installed.
Executing: mysql --defaults-extra-file=/tmp/drush_kYQR0n --database=xxxxxx --host=xxxxxx --port=xxxx --silent < /tmp/drush_lvGML4
Executing: mysql --defaults-extra-file=/tmp/drush_TmyFB0 --database=xxxxxx --host=xxxxxx --port=xxxx --silent < /tmp/drush_DVX7iH
Starting execution of comment_cron(). [notice]
Starting execution of dblog_cron(), execution of comment_cron() took [notice]
6.89ms.
Starting execution of field_cron(), execution of dblog_cron() took [notice]
9.29ms.
Starting execution of file_cron(), execution of field_cron() took [notice]
3.98ms.
Starting execution of history_cron(), execution of file_cron() took [notice]
17.56ms.
Starting execution of locale_cron(), execution of history_cron() took [notice]
56.67ms.
Starting execution of node_cron(), execution of locale_cron() took [notice]
0.06ms.
Starting execution of search_cron(), execution of node_cron() took [notice]
481.27ms.
PHP Fatal error: Call to a member function get() on null in /var/www/html/drupal/modules/flag/src/Entity/Storage/FlaggingStorage.php on line 89
Drush command terminated abnormally due to an unrecoverable error. [error]
Error: Call to a member function get() on null in
/var/www/html/drupal/modules/flag/src/Entity/Storage/FlaggingStorage.php,
line 89
Drupal is in 8.2.0 version and Flag on Flag 8.x-4.x-dev (2016-oct-07) version. It happend only when a Flag is created.
Comment | File | Size | Author |
---|---|---|---|
#9 | fatal_error_when-2815633-9.patch | 743 bytes | zaporylie |
#6 | 2815633-fix-drush-fatal-error-v3.patch | 764 bytes | oknate |
Comments
Comment #2
geoffvio CreditAttribution: geoffvio commentedComment #3
oknateError goes away if line 89 is changed to
if ($session = \Drupal::request()->getSession() && $session_flaggings = $session->get('flaggings', [])) {
Comment #4
oknateThis seems to work better:
I started to notice this error on a node edit form. I think instantiating a session in the condition is the issue.
Comment #5
BerdirThere is no functional difference in that change, your second patch can't possibly have a different outcome.
If the error stopped appearing then something else changed.
Comment #6
oknateThe reason I've created several patches is because it seems the session object behaves differently when on the command line. I'm doing a third patch that tests for the existence of the session object (like the first patch).
Even though it's functionally the same, it seems that creating null objects in conditions and then calling methods on those objects throws errors. PHP isn't smart enough to stop processing and you get errors that you can't call a method of a null object. So that's why I moved the session instantiation outside of the condition.
This new patch checks if the session exists, because it seems that it's still trying to call the method of the null object on the command line when running a migration import.
Comment #7
achtonI am seeing this as well, but on the front page of my site for anonymous users. I think it's fair to add some defensive code here (as the patches above do), considering that
Request::getSession()
may return null, in which case the fatal PHP error will be triggered.I've tested the patch in #6 which works for me. I'm running Drupal 8.2.1.
Comment #8
el1_1el CreditAttribution: el1_1el commentedsame here. if i run
drush cron
I get the fatal error. Patch in #6 fixes itComment #9
zaporylieI run across this issue when I was behat-testing nodes with flags. IMHO approach from #3 was just enough but it's missing parenthesis which makes it fail.
For me doesn't matter if #6 or #9 (#3 with parenthesis) gets in - both solves the same problem.
Comment #10
joachim CreditAttribution: joachim as a volunteer commentedI don't understand how the parentheses make a difference between #3 and #9...
Comment #11
zaporylieThat is because of operator precedence. The assignment operator = has a lower precedence than &&
if ($session = \Drupal::request()->getSession() && $session_flaggings = $session->get('flaggings', [])) {}
so without the parenthesis you are effectively doing the following:
if ($session = (\Drupal::request()->getSession() && $session_flaggings = $session->get('flaggings', []))) {}
Comment #13
joachim CreditAttribution: joachim as a volunteer commentedThanks! Braindead maintainer appreciates the explanation :)
And thank you all for the work on the patch.