Node add gives a Call to a member function getAccountName() on null Drupal 9

Created on 24 July 2020, almost 5 years ago
Updated 28 January 2025, 6 months ago

Problem/Motivation

When I visit node add I recibe a "Call to a member function getAccountName() on null " error.
Here is the full message.

Error: Call to a member function getAccountName() on null en Drupal\node\NodeForm->form() (lรญnea 155 de /var/www/html/web/core/modules/node/src/NodeForm.php)
#0 /var/www/html/web/core/lib/Drupal/Core/Entity/EntityForm.php(106): Drupal\node\NodeForm->form(Array, Object(Drupal\Core\Form\FormState))
#1 [internal function]: Drupal\Core\Entity\EntityForm->buildForm(Array, Object(Drupal\Core\Form\FormState))
#2 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(532): call_user_func_array(Array, Array)
#3 /var/www/html/web/core/lib/Drupal/Core/Form/FormBuilder.php(278): Drupal\Core\Form\FormBuilder->retrieveForm('node_project_fo...', Object(Drupal\Core\Form\FormState))
#4 /var/www/html/web/core/lib/Drupal/Core/Controller/FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\node\NodeForm), Object(Drupal\Core\Form\FormState))
#5 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#6 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#7 /var/www/html/web/core/lib/Drupal/Core/Render/Renderer.php(573): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#8 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#9 /var/www/html/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#10 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(158): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#11 /var/www/html/vendor/symfony/http-kernel/HttpKernel.php(80): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#12 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#13 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#14 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#15 /var/www/html/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#16 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 /var/www/html/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 /var/www/html/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#19 /var/www/html/web/core/lib/Drupal/Core/DrupalKernel.php(705): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#20 /var/www/html/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#21 {main}

Steps to reproduce

Visit node/add/[Bundle]

Proposed resolution

I think is related with the NodeForm, because try to retrieve the author of a new node, and it does not have it.
NodeForm::form

    $form['meta']['author'] = [
      '#type' => 'item',
      '#title' => $this->t('Author'),
      '#markup' => $node->getOwner()->getAccountName(),
      '#wrapper_attributes' => ['class' => ['entity-meta__author']],
    ];
๐Ÿ› Bug report
Status

Closed: duplicate

Version

9.1

Component

forms system

Created by

๐Ÿ‡ช๐Ÿ‡ธSpain eduardo morales alberti Spain, ๐Ÿ‡ช๐Ÿ‡บ

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

Merge Requests

Comments & Activities

Not all content is available!

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

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia SandeepSingh199

    Hi, I added some more checks

    +    // Set default author.
    +    $meta_author = \Drupal::currentUser()->getAccountName();
    +    if (!$node->isNew() && $node->getOwner() != null) {
    +      $meta_author = $node->getOwner()->getAccountName();
    +    }
    
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    Hitting this with D11 , client deleted a user

  • Pipeline finished with Failed
    4 months ago
    Total: 229s
    #443141
  • Pipeline finished with Failed
    4 months ago
    Total: 230s
    #443144
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    Suggested workarounds from comment #8 and related suggestions did not yield any clues. Patch 32 works for us. Our client deleted some users in prod that were involved in site building and some specific use cases involving content. Result is a WSOD for related environments when doing a node edit. The page in question has nothing special, just a node form with some normal fields. The reference to the user that is null is somewhere in a revision or as author/owner. This is about all the meta I can provide at this time.

  • Pipeline finished with Success
    4 months ago
    Total: 503s
    #443152
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Sivaji_Ganesh_Jojodae Chennai

    > changed target branch from 11.x to 11.1.x

    Should be 11.x

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States nicxvan
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    The issue title says node/add but that looks like a very old config export that was fixed in a different issue.

  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

    It doesn't make sense to fall back to the current user. If there is no current owner, it shouldn't display anything. it will afaik set the current user then as owner on save, but it's not yet true. So it should just not display anything then.

    This _should_ not happen, deleting users should either delete the content or reassign it, this is just about dealing with invalid data.

    And yes, originally the issue was about problems with the default value config that wasn't updated, I think a new issue would have made more sense, but it's technically speaking that error.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    It doesn't make sense to fall back to the current user. If there is no current owner, it shouldn't display anything. it will afaik set the current user then as owner on save, but it's not yet true. So it should just not display anything then.

    Yes agreed with this, and should possibly trigger_error() an E_USER_WARNING since it could be the result of a botched database update or attempt to prune user accounts and similar.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    While it shouldn't happen, it did for us and it is 100% content or revision related. We have about 6 environments of the same build. My copy is a production dump from before the client deleted users. In my copy, I do not see the exception however in the more recent production copy where the users were deleted there is an exception and the patch is needed in this case.

    So 100% the configuration is the same in our test cases however the content is not the same.

  • First commit to issue fork.
  • Status changed to Needs work 3 months ago
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley

    acbramley โ†’ changed the visibility of the branch 3161212-node-add-gives to hidden.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley

    Working on this, closing previous MR as the branch can't be rebased easily and it was against 11.1.x

  • Merge request !11763Issue #3161212: Protect against NULL author โ†’ (Closed) created by acbramley
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley

    It's actually quite difficult to contrive a scenario with core to hit a node add/edit page with a NULL author.

    The easiest way for node/add is to override the default value callback to stop setting it to the current user.

    I tried to get edit form coverage as well but as soon as you delete the user, the node is gone too because of NodeHooks1::userPredelete

    The uid column is non-NULL so we can't update the db directly.

    As for wording when the user is missing, I think we can just accept an empty string here hopefully? Seeing as this is quite a rare scenario I think as long as we don't WSOD that's good enough. The author will be set on next save anyway.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley
  • Pipeline finished with Success
    3 months ago
    Total: 866s
    #467989
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland

    What #49 said (and what I said in #41). This shouldn't happen, only thing I can think if is doing things directly in the database and bypassing entity API. (#49 is not quite correcting that regard. You don't need to set it to NULL, that's not what the scenario is about. It's about either setting it to a non-existing UID or deleting said user record without triggering entity hooks).

    But I'm not against hardening our code against invalid data. I probably wouldn't even have asked for test coverage but great that you added it anyway.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley

    You don't need to set it to NULL, that's not what the scenario is about. It's about either setting it to a non-existing UID

    Tried that and even that's not possible, we have this in Node::preSave

          // If no owner has been set explicitly, make the anonymous user the owner.
          if (!$translation->getOwner()) {
            $translation->setOwnerId(0);
          }
    

    But yeah I agree, this is more than enough :)

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Ran test-only feature

    1) Drupal\Tests\node\Functional\NodeEditFormTest::testNodeFormNullAuthor
    Behat\Mink\Exception\ExpectationException: Current response status code is 500, but 200 expected.
    /builds/issue/drupal-3161212/vendor/behat/mink/src/WebAssert.php:888
    /builds/issue/drupal-3161212/vendor/behat/mink/src/WebAssert.php:145
    /builds/issue/drupal-3161212/core/modules/node/tests/src/Functional/NodeEditFormTest.php:267
    FAILURES!
    Tests: 4, Assertions: 105, Failures: 1.
    

    99% of the code is tests so the actual fix makes sense.

    Very nice.

    • catch โ†’ committed b174fb09 on 11.1.x
      Issue #3161212 by joseph.olstad, acbramley, asubit, eduardo morales...
    • catch โ†’ committed ca57f212 on 11.x
      Issue #3161212 by joseph.olstad, acbramley, asubit, eduardo morales...
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    Latest MR looks good, no attempt to replace it with something else, just avoid the fatal error.

    Committed/pushed to 11.x and cherry-picked to 11.1.x, thanks!

    This is eligible for backport to 10.5.x, but we've broken 10.5.x several times recently cherry-picking 11.x commits without a full test run, so marking fixed. If you'd like this to be backported to 10.5.x, feel free to re-open with a 10.5.x-specific MR - it might just need a cherry-pick, but that way we get a full test run before commit.

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

Production build 0.71.5 2024