function ContentDevelGenerate::develGenerateContentAddNodeTranslation

Same name in other branches
  1. 5.x devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php \Drupal\devel_generate\Plugin\DevelGenerate\ContentDevelGenerate::develGenerateContentAddNodeTranslation()

Create translation for the given node.

Parameters

array $results: Results array.

\Drupal\node\NodeInterface $node: Node to add translations to.

Throws

\Drupal\Core\Entity\EntityStorageException

1 call to ContentDevelGenerate::develGenerateContentAddNodeTranslation()
ContentDevelGenerate::develGenerateContentAddNode in devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php
Create one node. Used by both batch and non-batch code branches.

File

devel_generate/src/Plugin/DevelGenerate/ContentDevelGenerate.php, line 691

Class

ContentDevelGenerate
Provides a ContentDevelGenerate plugin.

Namespace

Drupal\devel_generate\Plugin\DevelGenerate

Code

protected function develGenerateContentAddNodeTranslation(array &$results, NodeInterface $node) {
    if (is_null($this->contentTranslationManager)) {
        return;
    }
    if (!$this->contentTranslationManager
        ->isEnabled('node', $node->getType())) {
        return;
    }
    if ($node->langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED || $node->langcode == LanguageInterface::LANGCODE_NOT_APPLICABLE) {
        return;
    }
    if (!isset($results['num_translations'])) {
        $results['num_translations'] = 0;
    }
    // Translate node to each target language.
    $skip_languages = [
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
        LanguageInterface::LANGCODE_NOT_APPLICABLE,
        $node->langcode->value,
    ];
    foreach ($results['translate_language'] as $langcode) {
        if (in_array($langcode, $skip_languages)) {
            continue;
        }
        $translation_node = $node->addTranslation($langcode);
        $translation_node->devel_generate = $results;
        $translation_node->setTitle($node->getTitle() . ' (' . $langcode . ')');
        $this->populateFields($translation_node);
        $translation_node->save();
        if ($translation_node->id() > 0 && !empty($results['add_alias'])) {
            $path_alias = $this->aliasStorage
                ->create([
                'path' => '/node/' . $translation_node->id(),
                'alias' => '/node-' . $translation_node->id() . '-' . $translation_node->bundle() . '-' . $langcode,
                'langcode' => $langcode,
            ]);
            $path_alias->save();
        }
        $results['num_translations']++;
    }
}