function BlockContentTestBase::createBlockContentType

Creates a block type (bundle).

Parameters

array|string $values: The value to create the block content type. If $values is an array it should be like: ['id' => 'foo', 'label' => 'Foo']. If $values is a string, it will be considered that it represents the label.

bool $create_body: Whether or not to create the body field

Return value

\Drupal\block_content\Entity\BlockContentType Created block type.

6 calls to BlockContentTestBase::createBlockContentType()
BlockContentTestBase::setUp in core/modules/block_content/tests/src/Functional/BlockContentTestBase.php
Sets the test up.
BlockContentTypeTest::testBlockContentAddPageOrder in core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php
Tests the order of the block content types on the add page.
BlockContentTypeTest::testBlockContentTypeCreation in core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php
Tests creating a block type programmatically and via a form.
BlockContentTypeTest::testBlockContentTypeDeletion in core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php
Tests deleting a block type that still has content.
BlockContentTypeTest::testBlockContentTypeEditing in core/modules/block_content/tests/src/Functional/BlockContentTypeTest.php
Tests editing a block type using the UI.

... See full list

File

core/modules/block_content/tests/src/Functional/BlockContentTestBase.php, line 107

Class

BlockContentTestBase
Sets up block content types.

Namespace

Drupal\Tests\block_content\Functional

Code

protected function createBlockContentType($values, $create_body = FALSE) {
  if (is_array($values)) {
    if (!isset($values['id'])) {
      do {
        $id = $this->randomMachineName(8);
      } while (BlockContentType::load($id));
    }
    else {
      $id = $values['id'];
    }
    $values += [
      'id' => $id,
      'label' => $id,
      'revision' => FALSE,
    ];
    $bundle = BlockContentType::create($values);
  }
  else {
    $bundle = BlockContentType::create([
      'id' => $values,
      'label' => $values,
      'revision' => FALSE,
    ]);
  }
  $bundle->save();
  if ($create_body) {
    block_content_add_body_field($bundle->id());
  }
  return $bundle;
}

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