UID base field override configs can still have old default value callbacks

Created on 20 June 2020, over 4 years ago
Updated 20 December 2023, 11 months ago

After upgrading from Drupal 8.9 to Drupal 9.0 I get the two following error/warning messages when trying to create new content using some (not all) of my content types, among them the native β€œpage”-type:

Error message:

The website encountered an unexpected error. Please try again later. Error: Call to a member function getAccountName() on null in Drupal\node\NodeForm->form() (line 155 of core/modules/node/src/NodeForm.php).

Warning message:

call_user_func() expects parameter 1 to be a valid callback, class 'Drupal\node\Entity\Node' does not have a method 'getCurrentUserId' in Drupal\Core\Field\FieldConfigBase->getDefaultValue() (line 397 of core/lib/Drupal/Core/Field/FieldConfigBase.php).

Does anyone know what causes this?

Root cause

  • This issue may affect sites that were originally built with an earlier Drupal 8 version than 8.6.x and which were upgraded to Drupal 9.
  • The reason is that Node::getCurrentUserId method was deprecated in 8.6.x and was removed in 9.0.0. The replacing method in Drupal 9 is Node::getDefaultEntityOwner
  • Configuration entities that are still having a reference to the removed method will trigger this bug.
  • Note that other entity types than Nodes (e.g. Media) can also be affected.

Workaround

The following workaround steps are for Node entities but should be quite well applicable for other Entity types as well.

1. You want to identify those Node content types that are affected by this issue.

2. You can either a) do this by trial and error i.e. try to create new nodes for each content type on your site and see which content types are affected OR b) you can export your configuration and search for 'getCurrentUserId' to get a list of configuration files that are affected. The following steps use approach b) using Drush command line tool on Linux.

3. Export all configuration from command line using drush config:export

4. Navigate to your config/sync directory.

5. Identify the affected configuration files using

grep -R "getCurrentUserId" *

. The files matching this search will be like core.base_field_override.node.CONTENT_TYPE.uid.yml, for example core.base_field_override.node.page.uid.yml. As mentioned above, other entity types than nodes might also be affected.

6. For each affected configuration file, first take a backup of the configuration file. Then, use your favorite text editor and replace default_value_callback: 'Drupal\node\Entity\Node::getCurrentUserId' with default_value_callback: 'Drupal\node\Entity\Node::getDefaultEntityOwner' in each affected configuration file. If your site is affected by some other Entity type than Node, you will need to modify this find / replace according to your entity type.

7. Once you have edited all affected configuration you can double check with grep -R "getCurrentUserId" * that you did not miss any affected configuration file.

8. Once you have fixed all affected configuration files, you will need to import these configurations from your config/sync directory back to your database. This can be done with drush config:import

9. After you have imported the configurations, you will need to rebuild your caches. This can be done with drush cache:rebuild

10. Test and verify that you are now able to create new nodes of the affected content types.

πŸ› Bug report
Status

Fixed

Version

9.3

Component
Node systemΒ  β†’

Last updated 2 days ago

No maintainer
Created by

πŸ‡©πŸ‡ͺGermany PhilippJC

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024