Commit b174fb09 authored by catch's avatar catch
Browse files

Issue #3161212 by joseph.olstad, acbramley, asubit, eduardo morales alberti,...

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 ca57f212)
parent 8c7ffa56
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -163,7 +163,7 @@ public function form(array $form, FormStateInterface $form_state) {
    $form['meta']['author'] = [
      '#type' => 'item',
      '#title' => $this->t('Author'),
      '#markup' => $node->getOwner()->getAccountName(),
      '#markup' => $node->getOwner()?->getAccountName(),
      '#wrapper_attributes' => ['class' => ['entity-meta__author']],
    ];

+5 −0
Original line number Diff line number Diff line
name: 'Node no default author'
type: module
description: 'Disables the default value callback for the uid field on node.'
package: Testing
version: VERSION
+31 −0
Original line number Diff line number Diff line
<?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 {
  }

}
+10 −0
Original line number Diff line number Diff line
@@ -258,6 +258,16 @@ public function testNodeMetaInformation(): void {
    $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.
   *