function ContentTranslationTestBase::createEntity

Same name in this branch
  1. 8.9.x core/modules/content_translation/tests/src/Functional/ContentTranslationTestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationTestBase::createEntity()
Same name in other branches
  1. 9 core/modules/content_translation/tests/src/Functional/ContentTranslationTestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationTestBase::createEntity()
  2. 10 core/modules/content_translation/tests/src/Functional/ContentTranslationTestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationTestBase::createEntity()
  3. 11.x core/modules/content_translation/tests/src/Functional/ContentTranslationTestBase.php \Drupal\Tests\content_translation\Functional\ContentTranslationTestBase::createEntity()

Creates the entity to be translated.

Parameters

array $values: An array of initial values for the entity.

string $langcode: The initial language code of the entity.

string $bundle_name: (optional) The entity bundle, if the entity uses bundles. Defaults to NULL. If left NULL, $this->bundle will be used.

Return value

string The entity id.

1 call to ContentTranslationTestBase::createEntity()
ContentTranslationUITestBase::doTestBasicTranslation in core/modules/content_translation/src/Tests/ContentTranslationUITestBase.php
Tests the basic translation workflow.

File

core/modules/content_translation/src/Tests/ContentTranslationTestBase.php, line 224

Class

ContentTranslationTestBase
Base class for content translation tests.

Namespace

Drupal\content_translation\Tests

Code

protected function createEntity($values, $langcode, $bundle_name = NULL) {
    $entity_values = $values;
    $entity_values['langcode'] = $langcode;
    $entity_type = \Drupal::entityTypeManager()->getDefinition($this->entityTypeId);
    if ($bundle_key = $entity_type->getKey('bundle')) {
        $entity_values[$bundle_key] = $bundle_name ?: $this->bundle;
    }
    $controller = $this->container
        ->get('entity_type.manager')
        ->getStorage($this->entityTypeId);
    if (!$controller instanceof SqlContentEntityStorage) {
        foreach ($values as $property => $value) {
            if (is_array($value)) {
                $entity_values[$property] = [
                    $langcode => $value,
                ];
            }
        }
    }
    $entity = $this->container
        ->get('entity_type.manager')
        ->getStorage($this->entityTypeId)
        ->create($entity_values);
    $entity->save();
    return $entity->id();
}

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