trait ContentTypeCreationTrait

Provides methods to create content type from given values.

This trait is meant to be used only by test classes.

Hierarchy

65 files declare their use of ContentTypeCreationTrait
AddModerationConfigActionTest.php in core/modules/content_moderation/tests/src/Kernel/ConfigAction/AddModerationConfigActionTest.php
BaseFieldOverrideValidationTest.php in core/tests/Drupal/KernelTests/Core/Entity/BaseFieldOverrideValidationTest.php
BlockExposedFilterAJAXTest.php in core/modules/views/tests/src/FunctionalJavascript/BlockExposedFilterAJAXTest.php
BrowserTestBase.php in core/tests/Drupal/Tests/BrowserTestBase.php
ClaroViewsBulkOperationsTest.php in core/tests/Drupal/FunctionalJavascriptTests/Theme/ClaroViewsBulkOperationsTest.php

... See full list

File

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

Namespace

Drupal\Tests\node\Traits
View source
trait ContentTypeCreationTrait {
  use BodyFieldCreationTrait;
  
  /**
   * Creates a custom content type based on default settings.
   *
   * @param array $values
   *   An array of settings to change from the defaults.
   *   Example: 'type' => 'foo'.
   * @param bool $create_body
   *   Whether to create the body field.
   *
   * @return \Drupal\node\Entity\NodeType
   *   Created content type.
   */
  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;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
BodyFieldCreationTrait::createBodyField protected function Creates a field of an body field storage on the specified bundle.
ContentTypeCreationTrait::createContentType protected function Creates a custom content type based on default settings. 1

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