function ContentTranslationHandler::entityFormAlter
Same name in other branches
- 9 core/modules/content_translation/src/ContentTranslationHandler.php \Drupal\content_translation\ContentTranslationHandler::entityFormAlter()
- 8.9.x core/modules/content_translation/src/ContentTranslationHandler.php \Drupal\content_translation\ContentTranslationHandler::entityFormAlter()
- 11.x core/modules/content_translation/src/ContentTranslationHandler.php \Drupal\content_translation\ContentTranslationHandler::entityFormAlter()
Overrides ContentTranslationHandlerInterface::entityFormAlter
4 calls to ContentTranslationHandler::entityFormAlter()
- CommentTranslationHandler::entityFormAlter in core/
modules/ comment/ src/ CommentTranslationHandler.php - Performs the needed alterations to the entity form.
- NodeTranslationHandler::entityFormAlter in core/
modules/ node/ src/ NodeTranslationHandler.php - Performs the needed alterations to the entity form.
- ProfileTranslationHandler::entityFormAlter in core/
modules/ user/ src/ ProfileTranslationHandler.php - Performs the needed alterations to the entity form.
- TermTranslationHandler::entityFormAlter in core/
modules/ taxonomy/ src/ TermTranslationHandler.php - Performs the needed alterations to the entity form.
4 methods override ContentTranslationHandler::entityFormAlter()
- CommentTranslationHandler::entityFormAlter in core/
modules/ comment/ src/ CommentTranslationHandler.php - Performs the needed alterations to the entity form.
- NodeTranslationHandler::entityFormAlter in core/
modules/ node/ src/ NodeTranslationHandler.php - Performs the needed alterations to the entity form.
- ProfileTranslationHandler::entityFormAlter in core/
modules/ user/ src/ ProfileTranslationHandler.php - Performs the needed alterations to the entity form.
- TermTranslationHandler::entityFormAlter in core/
modules/ taxonomy/ src/ TermTranslationHandler.php - Performs the needed alterations to the entity form.
File
-
core/
modules/ content_translation/ src/ ContentTranslationHandler.php, line 345
Class
- ContentTranslationHandler
- Base class for content translation handlers.
Namespace
Drupal\content_translationCode
public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$metadata = $this->manager
->getTranslationMetadata($entity);
$form_object = $form_state->getFormObject();
$form_langcode = $form_object->getFormLangcode($form_state);
$entity_langcode = $entity->getUntranslated()
->language()
->getId();
$new_translation = $entity->isNewTranslation();
$translations = $entity->getTranslationLanguages();
if ($new_translation) {
// Make sure a new translation does not appear as existing yet.
unset($translations[$form_langcode]);
}
$is_translation = $new_translation || $entity->language()
->getId() != $entity_langcode;
$has_translations = count($translations) > 1;
// Adjust page title to specify the current language being edited, if we
// have at least one translation.
$languages = $this->languageManager
->getLanguages();
if (isset($languages[$form_langcode]) && ($has_translations || $new_translation)) {
$title = $this->entityFormTitle($entity);
// When editing the original values display just the entity label.
if ($is_translation) {
$t_args = [
'%language' => $languages[$form_langcode]->getName(),
'%title' => $entity->label(),
'@title' => $title,
];
$title = $new_translation ? t('Create %language translation of %title', $t_args) : t('@title [%language translation]', $t_args);
}
$form['#title'] = $title;
}
// Display source language selector only if we are creating a new
// translation and there are at least two translations available.
if ($has_translations && $new_translation) {
$source_langcode = $metadata->getSource();
$form['source_langcode'] = [
'#type' => 'details',
'#title' => t('Source language: @language', [
'@language' => $languages[$source_langcode]->getName(),
]),
'#tree' => TRUE,
'#weight' => -100,
'#multilingual' => TRUE,
'source' => [
'#title' => t('Select source language'),
'#title_display' => 'invisible',
'#type' => 'select',
'#default_value' => $source_langcode,
'#options' => [],
],
'submit' => [
'#type' => 'submit',
'#value' => t('Change'),
'#submit' => [
[
$this,
'entityFormSourceChange',
],
],
],
];
foreach ($this->languageManager
->getLanguages() as $language) {
if (isset($translations[$language->getId()])) {
$form['source_langcode']['source']['#options'][$language->getId()] = $language->getName();
}
}
}
// Locate the language widget.
$langcode_key = $this->entityType
->getKey('langcode');
if (isset($form[$langcode_key])) {
$language_widget =& $form[$langcode_key];
}
// If we are editing the source entity, limit the list of languages so that
// it is not possible to switch to a language for which a translation
// already exists. Note that this will only work if the widget is structured
// like \Drupal\Core\Field\Plugin\Field\FieldWidget\LanguageSelectWidget.
if (isset($language_widget['widget'][0]['value']) && !$is_translation && $has_translations) {
$language_select =& $language_widget['widget'][0]['value'];
if ($language_select['#type'] == 'language_select') {
$options = [];
foreach ($this->languageManager
->getLanguages() as $language) {
// Show the current language, and the languages for which no
// translation already exists.
if (empty($translations[$language->getId()]) || $language->getId() == $entity_langcode) {
$options[$language->getId()] = $language->getName();
}
}
$language_select['#options'] = $options;
}
}
if ($is_translation) {
if (isset($language_widget)) {
$language_widget['widget']['#access'] = FALSE;
}
// Replace the delete button with the delete translation one.
if (!$new_translation) {
$weight = 100;
foreach ([
'delete',
'submit',
] as $key) {
if (isset($form['actions'][$key]['weight'])) {
$weight = $form['actions'][$key]['weight'];
break;
}
}
/** @var \Drupal\Core\Access\AccessResultInterface $delete_access */
$delete_access = \Drupal::service('content_translation.delete_access')->checkAccess($entity);
$access = $delete_access->isAllowed() && ($this->getTranslationAccess($entity, 'delete')
->isAllowed() || $entity->access('delete') && $this->entityType
->hasLinkTemplate('delete-form'));
$form['actions']['delete_translation'] = [
'#type' => 'link',
'#title' => $this->t('Delete translation'),
'#access' => $access,
'#weight' => $weight,
'#url' => $this->entityFormDeleteTranslationUrl($entity, $form_langcode),
'#attributes' => [
'class' => [
'button',
'button--danger',
],
],
];
}
// Always remove the delete button on translation forms.
unset($form['actions']['delete']);
}
// We need to display the translation tab only when there is at least one
// translation available or a new one is about to be created.
if ($new_translation || $has_translations) {
$form['content_translation'] = [
'#type' => 'details',
'#title' => t('Translation'),
'#tree' => TRUE,
'#weight' => 10,
'#access' => $this->getTranslationAccess($entity, $new_translation ? 'create' : 'update')
->isAllowed(),
'#multilingual' => TRUE,
];
if (isset($form['advanced'])) {
$form['content_translation'] += [
'#group' => 'advanced',
'#weight' => 100,
'#attributes' => [
'class' => [
'entity-translation-options',
],
],
];
}
// A new translation is enabled by default.
$status = $new_translation || $metadata->isPublished();
// If there is only one published translation we cannot unpublish it,
// since there would be nothing left to display.
$enabled = TRUE;
if ($status) {
$published = 0;
foreach ($entity->getTranslationLanguages() as $langcode => $language) {
$published += $this->manager
->getTranslationMetadata($entity->getTranslation($langcode))
->isPublished();
}
$enabled = $published > 1;
}
$description = $enabled ? t('An unpublished translation will not be visible without translation permissions.') : t('Only this translation is published. You must publish at least one more translation to unpublish this one.');
$form['content_translation']['status'] = [
'#type' => 'checkbox',
'#title' => t('This translation is published'),
'#default_value' => $status,
'#description' => $description,
'#disabled' => !$enabled,
];
$translate = !$new_translation && $metadata->isOutdated();
$outdated_access = !ContentTranslationManager::isPendingRevisionSupportEnabled($entity->getEntityTypeId(), $entity->bundle());
if (!$outdated_access) {
$form['content_translation']['outdated'] = [
'#markup' => $this->t('Translations cannot be flagged as outdated when content is moderated.'),
];
}
elseif (!$translate) {
$form['content_translation']['retranslate'] = [
'#type' => 'checkbox',
'#title' => t('Flag other translations as outdated'),
'#default_value' => FALSE,
'#description' => t('If you made a significant change, which means the other translations should be updated, you can flag all translations of this content as outdated. This will not change any other property of them, like whether they are published or not.'),
'#access' => $outdated_access,
];
}
else {
$form['content_translation']['outdated'] = [
'#type' => 'checkbox',
'#title' => t('This translation needs to be updated'),
'#default_value' => $translate,
'#description' => t('When this option is checked, this translation needs to be updated. Uncheck when the translation is up to date again.'),
'#access' => $outdated_access,
];
$form['content_translation']['#open'] = TRUE;
}
// Default to the anonymous user.
$uid = 0;
if ($new_translation) {
$uid = $this->currentUser
->id();
}
elseif (($account = $metadata->getAuthor()) && $account->id()) {
$uid = $account->id();
}
$form['content_translation']['uid'] = [
'#type' => 'entity_autocomplete',
'#title' => t('Authored by'),
'#target_type' => 'user',
'#default_value' => User::load($uid),
// Validation is done by static::entityFormValidate().
'#validate_reference' => FALSE,
'#maxlength' => 60,
'#description' => t('Leave blank for %anonymous.', [
'%anonymous' => \Drupal::config('user.settings')->get('anonymous'),
]),
];
$date = $new_translation ? $this->time
->getRequestTime() : $metadata->getCreatedTime();
$form['content_translation']['created'] = [
'#type' => 'textfield',
'#title' => t('Authored on'),
'#maxlength' => 25,
'#description' => t('Leave blank to use the time of form submission.'),
'#default_value' => $new_translation || !$date ? '' : $this->dateFormatter
->format($date, 'custom', 'Y-m-d H:i:s O'),
];
$form['#process'][] = [
$this,
'entityFormSharedElements',
];
}
// Process the submitted values before they are stored.
$form['#entity_builders'][] = [
$this,
'entityFormEntityBuild',
];
// Handle entity validation.
$form['#validate'][] = [
$this,
'entityFormValidate',
];
// Handle entity deletion.
if (isset($form['actions']['delete'])) {
$form['actions']['delete']['#submit'][] = [
$this,
'entityFormDelete',
];
}
// Handle entity form submission before the entity has been saved.
foreach (Element::children($form['actions']) as $action) {
if (isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] == 'submit') {
array_unshift($form['actions'][$action]['#submit'], [
$this,
'entityFormSubmit',
]);
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.