You are here

public function NodeCreationTrait::getNodeByTitle in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Traits/NodeCreationTrait.php \Drupal\Tests\node\Traits\NodeCreationTrait::getNodeByTitle()
  2. 9 core/modules/node/tests/src/Traits/NodeCreationTrait.php \Drupal\Tests\node\Traits\NodeCreationTrait::getNodeByTitle()

Get a node from the database based on its title.

Parameters

string|\Drupal\Component\Render\MarkupInterface $title: A node title, usually generated by $this->randomMachineName().

$reset: (optional) Whether to reset the entity cache.

Return value

\Drupal\node\NodeInterface A node entity matching $title.

5 calls to NodeCreationTrait::getNodeByTitle()
BookRelationshipTest::createBookNode in core/modules/book/tests/src/Functional/Views/BookRelationshipTest.php
Creates a book node.
EntityTranslationFormTest::testEntityFormLanguage in core/modules/system/tests/src/Functional/Entity/EntityTranslationFormTest.php
Tests entity form language.
MediaThumbnailFormatterTest::testRender in core/modules/media/tests/src/Functional/FieldFormatter/MediaThumbnailFormatterTest.php
Tests the media thumbnail field formatter.
PageTitleTest::testTitleTags in core/modules/system/tests/src/Functional/System/PageTitleTest.php
Tests the handling of HTML in node titles.
TaxonomyTermViewTest::testTaxonomyTermView in core/modules/taxonomy/tests/src/Functional/Views/TaxonomyTermViewTest.php
Tests that the taxonomy term view is working properly.

File

core/modules/node/tests/src/Traits/NodeCreationTrait.php, line 26

Class

NodeCreationTrait
Provides methods to create node based on default settings.

Namespace

Drupal\Tests\node\Traits

Code

public function getNodeByTitle($title, $reset = FALSE) {
  if ($reset) {
    \Drupal::entityTypeManager()
      ->getStorage('node')
      ->resetCache();
  }

  // Cast MarkupInterface objects to string.
  $title = (string) $title;
  $nodes = \Drupal::entityTypeManager()
    ->getStorage('node')
    ->loadByProperties([
    'title' => $title,
  ]);

  // Load the first node returned from the database.
  $returned_node = reset($nodes);
  return $returned_node;
}