Verified Commit c61d707e authored by Dave Long's avatar Dave Long
Browse files

Issue #3108102 by lauriii, SpadXIII, akasake, myLies, _pratik_, Suresh Prabhu...

Issue #3108102 by lauriii, SpadXIII, akasake, myLies, _pratik_, Suresh Prabhu Parkala, penyaskito, longwave, smustgrave: Destination url query param affects on form translation delete submission
parent 587a7f90
Loading
Loading
Loading
Loading
+40 −6
Original line number Diff line number Diff line
@@ -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 */
+6 −3
Original line number Diff line number Diff line
@@ -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');
+1 −1
Original line number Diff line number Diff line
@@ -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);
+4 −0
Original line number Diff line number Diff line
@@ -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.