function ContentTypeCreationTrait::createContentType

Creates a custom content type based on default settings.

Parameters

array $values: An array of settings to change from the defaults. Example: 'type' => 'foo'.

bool $create_body: Whether to create the body field.

Return value

\Drupal\node\Entity\NodeType Created content type.

348 calls to ContentTypeCreationTrait::createContentType()
AddedStylesheetsTest::setUp in core/modules/ckeditor5/tests/src/Functional/AddedStylesheetsTest.php
AddModerationConfigActionTest::testAddEntityTypeAndBundle in core/modules/content_moderation/tests/src/Kernel/ConfigAction/AddModerationConfigActionTest.php
Tests adding entity types and bundles to a workflow.
AnnouncementsCacheTest::testDynamicPageCache in core/modules/announcements_feed/tests/src/Functional/AnnouncementsCacheTest.php
Tests dynamic page cache.
BaseFieldOverrideValidationTest::setUp in core/tests/Drupal/KernelTests/Core/Entity/BaseFieldOverrideValidationTest.php
BasicTest::testViewsWizardAndListing in core/modules/views/tests/src/Functional/Wizard/BasicTest.php
Tests the Views wizard and listing.

... See full list

1 method overrides ContentTypeCreationTrait::createContentType()
WorkspacesContentModerationStateTest::createContentType in core/modules/content_moderation/tests/src/Kernel/WorkspacesContentModerationStateTest.php
Creates a custom content type based on default settings.

File

core/modules/node/tests/src/Traits/ContentTypeCreationTrait.php, line 33

Class

ContentTypeCreationTrait
Provides methods to create content type from given values.

Namespace

Drupal\Tests\node\Traits

Code

protected function createContentType(array $values = [], bool $create_body = TRUE) {
  // Find a non-existent random type name.
  if (!isset($values['type'])) {
    do {
      $id = $this->randomMachineName(8);
    } while (NodeType::load($id));
  }
  else {
    $id = $values['type'];
  }
  $values += [
    'type' => $id,
    'name' => $id,
  ];
  $type = NodeType::create($values);
  $status = $type->save();
  if ($create_body) {
    $this->createBodyField('node', $type->id());
  }
  if ($this instanceof TestCase) {
    $this->assertSame($status, SAVED_NEW, (new FormattableMarkup('Created content type %type.', [
      '%type' => $type->id(),
    ]))
      ->__toString());
  }
  else {
    $this->assertEquals(SAVED_NEW, $status, (new FormattableMarkup('Created content type %type.', [
      '%type' => $type->id(),
    ]))
      ->__toString());
  }
  return $type;
}

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