ProfileTranslationHandler.php 1.61 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\user\ProfileTranslationHandler.
6 7 8 9 10
 */

namespace Drupal\user;

use Drupal\Core\Entity\EntityInterface;
11
use Drupal\content_translation\ContentTranslationHandler;
12
use Drupal\Core\Form\FormStateInterface;
13 14

/**
15
 * Defines the translation handler for users.
16
 */
17
class ProfileTranslationHandler extends ContentTranslationHandler {
18

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
  /**
   * {@inheritdoc}
   */
  protected function hasPublishedStatus() {
    // User status has nothing to do with translations visibility.
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  protected function hasCreatedTime() {
    // User creation date has nothing to do with translation creation date.
    return FALSE;
  }

35
  /**
36
   * {@inheritdoc}
37
   */
38
  public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
39 40 41 42 43
    parent::entityFormAlter($form, $form_state, $entity);
    $form['actions']['submit']['#submit'][] = array($this, 'entityFormSave');
  }

  /**
44
   * Form submission handler for ProfileTranslationHandler::entityFormAlter().
45 46 47
   *
   * This handles the save action.
   *
48
   * @see \Drupal\Core\Entity\EntityForm::build().
49
   */
50
  public function entityFormSave(array $form, FormStateInterface $form_state) {
51
    if ($this->getSourceLangcode($form_state)) {
52
      $entity = $form_state->getFormObject()->getEntity();
53 54 55
      // We need a redirect here, otherwise we would get an access denied page
      // since the current URL would be preserved and we would try to add a
      // translation for a language that already has a translation.
56
      $form_state->setRedirectUrl($entity->urlInfo());
57 58
    }
  }
59

60
}