diff --git a/core/modules/content_translation/src/ContentTranslationHandler.php b/core/modules/content_translation/src/ContentTranslationHandler.php index 24caa245c7c4874f449a8cbb86f6307ec7c8f2c1..fe837505ded8f3d904cb7235c0f2333ae9d992e2 100644 --- a/core/modules/content_translation/src/ContentTranslationHandler.php +++ b/core/modules/content_translation/src/ContentTranslationHandler.php @@ -18,8 +18,10 @@ use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Render\Element; +use Drupal\Core\Routing\RedirectDestinationInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\Url; use Drupal\user\Entity\User; use Drupal\user\EntityOwnerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -119,8 +121,10 @@ class ContentTranslationHandler implements ContentTranslationHandlerInterface, E * The date formatter service. * @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository * The installed entity definition repository service. + * @param \Drupal\Core\Routing\RedirectDestinationInterface|null $redirectDestination + * The request stack. */ - public function __construct(EntityTypeInterface $entity_type, LanguageManagerInterface $language_manager, ContentTranslationManagerInterface $manager, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, MessengerInterface $messenger, DateFormatterInterface $date_formatter, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository) { + public function __construct(EntityTypeInterface $entity_type, LanguageManagerInterface $language_manager, ContentTranslationManagerInterface $manager, EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, MessengerInterface $messenger, DateFormatterInterface $date_formatter, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository, protected ?RedirectDestinationInterface $redirectDestination = NULL) { $this->entityTypeId = $entity_type->id(); $this->entityType = $entity_type; $this->languageManager = $language_manager; @@ -130,6 +134,10 @@ public function __construct(EntityTypeInterface $entity_type, LanguageManagerInt $this->fieldStorageDefinitions = $entity_last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($this->entityTypeId); $this->messenger = $messenger; $this->dateFormatter = $date_formatter; + if ($this->redirectDestination === NULL) { + @trigger_error('Calling ContentTranslationHandler::__construct() without the $redirectDestination argument is deprecated in drupal:10.2.0 and will be required in drupal:11.0.0. See https://www.drupal.org/node/3375487', E_USER_DEPRECATED); + $this->redirectDestination = \Drupal::service('redirect.destination'); + } } /** @@ -144,7 +152,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI $container->get('current_user'), $container->get('messenger'), $container->get('date.formatter'), - $container->get('entity.last_installed_schema.repository') + $container->get('entity.last_installed_schema.repository'), + $container->get('redirect.destination') ); } @@ -419,11 +428,14 @@ public function entityFormAlter(array &$form, FormStateInterface $form_state, En ($entity->access('delete') && $this->entityType->hasLinkTemplate('delete-form')) ); $form['actions']['delete_translation'] = [ - '#type' => 'submit', - '#value' => t('Delete translation'), - '#weight' => $weight, - '#submit' => [[$this, 'entityFormDeleteTranslation']], + '#type' => 'link', + '#title' => $this->t('Delete translation'), '#access' => $access, + '#weight' => $weight, + '#url' => $this->entityFormDeleteTranslationUrl($entity, $form_langcode), + '#attributes' => [ + 'class' => ['button', 'button--danger'], + ], ]; } @@ -769,12 +781,34 @@ public function entityFormDelete($form, FormStateInterface $form_state) { } } + /** + * Form submission handler for ContentTranslationHandler::entityFormAlter(). + * + * Get the entity delete form route url. + */ + protected function entityFormDeleteTranslationUrl(EntityInterface $entity, $form_langcode) { + $entity_type_id = $entity->getEntityTypeId(); + $options = []; + $options['query']['destination'] = $this->redirectDestination->get(); + + if ($entity->access('delete') && $this->entityType->hasLinkTemplate('delete-form')) { + return $entity->toUrl('delete-form', $options); + } + + return Url::fromRoute("entity.$entity_type_id.content_translation_delete", [ + $entity_type_id => $entity->id(), + 'language' => $form_langcode, + ], $options); + } + /** * Form submission handler for ContentTranslationHandler::entityFormAlter(). * * Takes care of content translation deletion. */ public function entityFormDeleteTranslation($form, FormStateInterface $form_state) { + @trigger_error('Calling ContentTranslationHandler::entityFormDeleteTranslation() is deprecated in drupal:10.2.0 and will be removed in drupal:11.0.0. See https://www.drupal.org/node/3375492', E_USER_DEPRECATED); + /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */ $form_object = $form_state->getFormObject(); /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ diff --git a/core/modules/content_translation/tests/src/Functional/ContentTranslationRevisionTranslationDeletionTest.php b/core/modules/content_translation/tests/src/Functional/ContentTranslationRevisionTranslationDeletionTest.php index 25b60c7573876057efd7d0982de3543ba945b144..97e2f0da6ba645341a14a53da3a2a5aea028bbda 100644 --- a/core/modules/content_translation/tests/src/Functional/ContentTranslationRevisionTranslationDeletionTest.php +++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationRevisionTranslationDeletionTest.php @@ -93,7 +93,7 @@ public function doTestOverview($index) { $warning = 'The "Delete translation" action is only available for published translations.'; $this->assertSession()->statusMessageContains($warning, 'warning'); $this->drupalGet($this->getEditUrl($it_revision)); - $this->assertSession()->buttonNotExists('Delete translation'); + $this->assertSession()->linkNotExistsExact('Delete translation'); // Publish the translation and verify it can be deleted. $edit = [ @@ -109,7 +109,7 @@ public function doTestOverview($index) { $this->assertSession()->linkByHrefExists($it_delete_href); $this->assertSession()->statusMessageNotContains($warning); $this->drupalGet($this->getEditUrl($it_revision)); - $this->assertSession()->buttonExists('Delete translation'); + $this->assertSession()->linkExistsExact('Delete translation'); // Create an English draft and verify the published translation was // preserved. @@ -205,8 +205,11 @@ public function doTestOverview($index) { $this->assertSession()->linkByHrefExists($it_delete_href); // Verify that now the translation can be deleted. - $this->drupalGet($it_delete_url); + $this->drupalGet($this->getEditUrl($it_revision)->setOption('query', ['destination', '/kittens'])); + $this->clickLink('Delete translation'); $this->submitForm([], 'Delete Italian translation'); + $this->assertStringEndsWith('/kittens', $this->getSession()->getCurrentUrl()); + $entity = $this->storage->loadUnchanged($id); $this->assertFalse($entity->hasTranslation('it')); $it_revision = $this->loadRevisionTranslation($entity, 'it'); diff --git a/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php b/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php index e012360be7140abb2294cba2dcc5f6e4d4824398..cb63ea242b2569a7d0c314de24b70dd74a0fe2af 100644 --- a/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php +++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationUITestBase.php @@ -379,7 +379,7 @@ protected function doTestTranslationDeletion() { $language = ConfigurableLanguage::load($langcode); $url = $entity->toUrl('edit-form', ['language' => $language]); $this->drupalGet($url); - $this->submitForm([], 'Delete translation'); + $this->clickLink('Delete translation'); $this->submitForm([], 'Delete ' . $language->getName() . ' translation'); $storage->resetCache([$this->entityId]); $entity = $storage->load($this->entityId, TRUE); diff --git a/core/themes/claro/claro.theme b/core/themes/claro/claro.theme index c50ec48c9a36efe30aa991c408c3d3348bd88949..d174b632561c9c54daf725782d607a9d75aa7a78 100644 --- a/core/themes/claro/claro.theme +++ b/core/themes/claro/claro.theme @@ -414,6 +414,10 @@ function claro_form_alter(array &$form, FormStateInterface $form_state, $form_id $form['actions']['delete'] = _claro_convert_link_to_action_link($form['actions']['delete'], 'trash', 'default', 'danger'); } + if (isset($form['actions']['delete_translation']['#type']) && $form['actions']['delete_translation']['#type'] === 'link' && !empty($build_info['callback_object']) && $build_info['callback_object'] instanceof EntityForm) { + $form['actions']['delete_translation'] = _claro_convert_link_to_action_link($form['actions']['delete_translation'], 'trash', 'default', 'danger'); + } + if (($form_object instanceof ViewsForm || $form_object instanceof ViewsFormInterface) && isset($form['override']['#prefix'])) { // Replace form--inline class so positioning of override form elements don't // have to depend on floats.