function NodeCreationTrait::getNodeByTitle

Same name and namespace in other branches
  1. 8.9.x core/modules/node/tests/src/Traits/NodeCreationTrait.php \Drupal\Tests\node\Traits\NodeCreationTrait::getNodeByTitle()
  2. 10 core/modules/node/tests/src/Traits/NodeCreationTrait.php \Drupal\Tests\node\Traits\NodeCreationTrait::getNodeByTitle()
  3. 11.x 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.

51 calls to NodeCreationTrait::getNodeByTitle()
BookBreadcrumbTest::createBookNode in core/modules/book/tests/src/Functional/BookBreadcrumbTest.php
Creates a book node.
BookContentModerationTest::testBookWithPendingRevisions in core/modules/book/tests/src/Functional/BookContentModerationTest.php
Tests that book drafts can not modify the book outline.
BookRelationshipTest::createBookNode in core/modules/book/tests/src/Functional/Views/BookRelationshipTest.php
Creates a book node.
CommentLanguageTest::testCommentLanguage in core/modules/comment/tests/src/Functional/CommentLanguageTest.php
Tests that comment language is properly set.
ContentTranslationContextualLinksTest::testContentTranslationContextualLinks in core/modules/content_translation/tests/src/Functional/ContentTranslationContextualLinksTest.php
Tests that a contextual link is available for translating a node.

... See full list

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.