protected function EntityTestBase::createTranslatableNode

Creates a node of a given bundle.

It uses $this->field_names to populate content of attached fields.

Parameters

string $bundle: Node type name.

string $sourcelang: Source lang of the node to be created.

Return value

\Drupal\node\NodeInterface Newly created node object.

File

src/Tests/EntityTestBase.php, line 166

Class

EntityTestBase
Utility test case class with helper methods to create entities and their fields with populated translatable content. Extend this class if you create tests in which you need Drupal entities and/or fields.

Namespace

Drupal\tmgmt\Tests

Code

protected function createTranslatableNode($bundle, $sourcelang = 'en') {
  $node = array(
    'type' => $bundle,
    'langcode' => $sourcelang,
  );
  foreach ($this->field_names['node'][$bundle] as $field_name) {

    // @todo: Why are some missing?
    if ($field_storage_config = FieldStorageConfig::loadByName('node', $field_name)) {
      $cardinality = $field_storage_config
        ->getCardinality() == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED ? 1 : $field_storage_config
        ->getCardinality();

      // Create two deltas for each field.
      for ($delta = 0; $delta <= $cardinality; $delta++) {
        $node[$field_name][$delta]['value'] = $this
          ->randomMachineName(20);
        $node[$field_name][$delta]['format'] = 'plain_text';
        if ($field_storage_config
          ->getType() == 'text_with_summary') {
          $node[$field_name][$delta]['summary'] = $this
            ->randomMachineName(10);
        }
      }
    }
  }
  return $this
    ->drupalCreateNode($node);
}