function ContentTranslationController::add

Same name in other branches
  1. 8.9.x core/modules/content_translation/src/Controller/ContentTranslationController.php \Drupal\content_translation\Controller\ContentTranslationController::add()
  2. 10 core/modules/content_translation/src/Controller/ContentTranslationController.php \Drupal\content_translation\Controller\ContentTranslationController::add()
  3. 11.x core/modules/content_translation/src/Controller/ContentTranslationController.php \Drupal\content_translation\Controller\ContentTranslationController::add()

Builds an add translation page.

Parameters

\Drupal\Core\Language\LanguageInterface $source: The language of the values being translated. Defaults to the entity language.

\Drupal\Core\Language\LanguageInterface $target: The language of the translated values. Defaults to the current content language.

\Drupal\Core\Routing\RouteMatchInterface $route_match: The route match object from which to extract the entity type.

string $entity_type_id: (optional) The entity type ID.

Return value

array A processed form array ready to be rendered.

File

core/modules/content_translation/src/Controller/ContentTranslationController.php, line 361

Class

ContentTranslationController
Base class for entity translation controllers.

Namespace

Drupal\content_translation\Controller

Code

public function add(LanguageInterface $source, LanguageInterface $target, RouteMatchInterface $route_match, $entity_type_id = NULL) {
    
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $route_match->getParameter($entity_type_id);
    // In case of a pending revision, make sure we load the latest
    // translation-affecting revision for the source language, otherwise the
    // initial form values may not be up-to-date.
    if (!$entity->isDefaultRevision() && ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id, $entity->bundle())) {
        
        /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
        $storage = $this->entityTypeManager()
            ->getStorage($entity->getEntityTypeId());
        $revision_id = $storage->getLatestTranslationAffectedRevisionId($entity->id(), $source->getId());
        if ($revision_id != $entity->getRevisionId()) {
            $entity = $storage->loadRevision($revision_id);
        }
    }
    // @todo Exploit the upcoming hook_entity_prepare() when available.
    // See https://www.drupal.org/node/1810394.
    $this->prepareTranslation($entity, $source, $target);
    // @todo Provide a way to figure out the default form operation. Maybe like
    //   $operation = isset($info['default_operation']) ? $info['default_operation'] : 'default';
    //   See https://www.drupal.org/node/2006348.
    // Use the add form handler, if available, otherwise default.
    $operation = $entity->getEntityType()
        ->hasHandlerClass('form', 'add') ? 'add' : 'default';
    $form_state_additions = [];
    $form_state_additions['langcode'] = $target->getId();
    $form_state_additions['content_translation']['source'] = $source;
    $form_state_additions['content_translation']['target'] = $target;
    $form_state_additions['content_translation']['translation_form'] = !$entity->access('update');
    return $this->entityFormBuilder()
        ->getForm($entity, $operation, $form_state_additions);
}

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