Issue #3161212 by joseph.olstad, acbramley, asubit, eduardo morales alberti, sandeepsingh199, berdir, catch: Node add/edit gives a Call to a member function getAccountName() on null when author is NULL

(cherry picked from commit ca57f2128a)
merge-requests/12078/head
catch 2025-05-07 09:59:33 +01:00
parent 8c7ffa560b
commit b174fb0932
4 changed files with 47 additions and 1 deletions

View File

@ -163,7 +163,7 @@ class NodeForm extends ContentEntityForm {
$form['meta']['author'] = [
'#type' => 'item',
'#title' => $this->t('Author'),
'#markup' => $node->getOwner()->getAccountName(),
'#markup' => $node->getOwner()?->getAccountName(),
'#wrapper_attributes' => ['class' => ['entity-meta__author']],
];

View File

@ -0,0 +1,5 @@
name: 'Node no default author'
type: module
description: 'Disables the default value callback for the uid field on node.'
package: Testing
version: VERSION

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace Drupal\node_no_default_author\Hook;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Hook\Attribute\Hook;
/**
* Hook implementations for node_no_default_author.
*/
class NodeNoDefaultAuthorHooks {
/**
* Implements hook_entity_base_field_info_alter().
*/
#[Hook('entity_base_field_info_alter')]
public function entityBaseFieldInfoAlter(&$fields, EntityTypeInterface $entity_type): void {
if ($entity_type->id() === 'node') {
$fields['uid']->setDefaultValueCallback(static::class . '::noDefaultAuthor');
}
}
/**
* An empty callback to set for the default value callback of uid.
*/
public static function noDefaultAuthor(): void {
}
}

View File

@ -258,6 +258,16 @@ class NodeEditFormTest extends NodeTestBase {
$this->assertSession()->pageTextContains($this->container->get('date.formatter')->format($node->getChangedTime(), 'short'));
}
/**
* Tests the node form when the author is NULL.
*/
public function testNodeFormNullAuthor(): void {
\Drupal::service('module_installer')->install(['node_no_default_author']);
$this->drupalLogin($this->adminUser);
$this->drupalGet('node/add/page');
$this->assertSession()->statusCodeEquals(200);
}
/**
* Checks that the "authored by" works correctly with various values.
*