function BlockContentCreationTrait::createBlockContent

Creates a content block.

Parameters

bool|string $title: (optional) Title of block. When no value is given uses a random name. Defaults to FALSE.

string $bundle: (optional) Bundle name. Defaults to 'basic'.

bool $save: (optional) Whether to save the block. Defaults to TRUE.

array $values: (optional) Additional values for the block_content entity.

Return value

\Drupal\block_content\Entity\BlockContent Created content block.

14 calls to BlockContentCreationTrait::createBlockContent()
BlockContentContextualLinksTest::testBlockContentContextualLinks in core/modules/block_content/tests/src/Functional/BlockContentContextualLinksTest.php
Tests contextual links.
BlockContentCreationTest::testConfigDependencies in core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php
Tests placed content blocks create a dependency in the block placement.
BlockContentCreationTest::testFailedBlockCreation in core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php
Verifies that a transaction rolls back the failed creation.
BlockContentListTest::testListing in core/modules/block_content/tests/src/Functional/BlockContentListTest.php
Tests the content block listing page with different permissions.
BlockContentListViewsTest::testListing in core/modules/block_content/tests/src/Functional/BlockContentListViewsTest.php
Tests the content block listing page.

... See full list

File

core/modules/block_content/tests/src/Traits/BlockContentCreationTrait.php, line 31

Class

BlockContentCreationTrait
Provides methods for creating block_content entities and types.

Namespace

Drupal\Tests\block_content\Traits

Code

protected function createBlockContent(bool|string $title = FALSE, string $bundle = 'basic', bool $save = TRUE, array $values = []) : BlockContent {
  $title = $title ?: $this->randomMachineName();
  $values += [
    'info' => $title,
    'type' => $bundle,
    'langcode' => 'en',
  ];
  $block_content = BlockContent::create($values);
  if ($block_content && $save === TRUE) {
    $block_content->save();
  }
  return $block_content;
}

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