Drupal investigation

ProfileTranslationHandler.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Drupal\user;
  3. use Drupal\Core\Entity\EntityInterface;
  4. use Drupal\content_translation\ContentTranslationHandler;
  5. use Drupal\Core\Form\FormStateInterface;
  6. /**
  7. * Defines the translation handler for users.
  8. */
  9. class ProfileTranslationHandler extends ContentTranslationHandler {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. protected function hasPublishedStatus() {
  14. // User status has nothing to do with translations visibility.
  15. return FALSE;
  16. }
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function hasCreatedTime() {
  21. // User creation date has nothing to do with translation creation date.
  22. return FALSE;
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function entityFormAlter(array &$form, FormStateInterface $form_state, EntityInterface $entity) {
  28. parent::entityFormAlter($form, $form_state, $entity);
  29. $form['actions']['submit']['#submit'][] = [$this, 'entityFormSave'];
  30. }
  31. /**
  32. * Form submission handler for ProfileTranslationHandler::entityFormAlter().
  33. *
  34. * This handles the save action.
  35. *
  36. * @see \Drupal\Core\Entity\EntityForm::build()
  37. */
  38. public function entityFormSave(array $form, FormStateInterface $form_state) {
  39. if ($this->getSourceLangcode($form_state)) {
  40. $entity = $form_state->getFormObject()->getEntity();
  41. // We need a redirect here, otherwise we would get an access denied page
  42. // since the current URL would be preserved and we would try to add a
  43. // translation for a language that already has a translation.
  44. $form_state->setRedirectUrl($entity->urlInfo());
  45. }
  46. }
  47. }