You are here

class LingotekLanguageForm in Lingotek Translation 3.4.x

Same name and namespace in other branches
  1. 8 src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  2. 8.2 src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  3. 4.0.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  4. 3.0.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  5. 3.1.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  6. 3.2.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  7. 3.3.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  8. 3.5.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  9. 3.6.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  10. 3.7.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  11. 3.8.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm

Alters the Drupal language module language forms.

@package Drupal\lingotek\Form

Hierarchy

Expanded class hierarchy of LingotekLanguageForm

1 string reference to 'LingotekLanguageForm'
lingotek.services.yml in ./lingotek.services.yml
lingotek.services.yml
1 service uses LingotekLanguageForm
lingotek.language_form in ./lingotek.services.yml
Drupal\lingotek\Form\LingotekLanguageForm

File

src/Form/LingotekLanguageForm.php, line 20

Namespace

Drupal\lingotek\Form
View source
class LingotekLanguageForm {
  use StringTranslationTrait;

  /**
   * A lingotek connector object
   *
   * @var \Drupal\lingotek\LingotekInterface
   */
  protected $lingotek;

  /**
   * The language-locale mapper.
   *
   * @var \Drupal\lingotek\LanguageLocaleMapperInterface
   */
  protected $languageLocaleMapper;

  /**
   * The Lingotek configuration service.
   *
   * @var \Drupal\lingotek\LingotekConfigurationServiceInterface
   */
  protected $lingotekConfiguration;

  /**
   * Constructs a new LingotekLanguageForm object.
   *
   * @param \Drupal\lingotek\LingotekInterface $lingotek
   *   A lingotek object.
   * @param \Drupal\lingotek\LanguageLocaleMapperInterface $language_locale_mapper
   *   The language-locale mapper.
   * @param \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotek_configuration
   *   The Lingotek configuration service.
   */
  public function __construct(LingotekInterface $lingotek, LanguageLocaleMapperInterface $language_locale_mapper, LingotekConfigurationServiceInterface $lingotek_configuration = NULL) {
    $this->lingotek = $lingotek;
    $this->languageLocaleMapper = $language_locale_mapper;
    if (!$lingotek_configuration) {
      @trigger_error('The lingotek.configuration service must be passed to LingotekLanguageForm::__construct, it is required before Lingotek 4.0.0.', E_USER_DEPRECATED);
      $lingotek_configuration = \Drupal::service('lingotek.configuration');
    }
    $this->lingotekConfiguration = $lingotek_configuration;
  }

  /**
   * Alters the configurable language entity edit and add form.
   *
   * @param array $form
   *   The form definition array for the configurable language entity.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function form(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\language\ConfigurableLanguageInterface $language */
    $language = $form_state
      ->getFormObject()
      ->getEntity();
    $langcode = $language
      ->getId();
    $form['custom_language']['lingotek'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Lingotek translation'),
      '#weight' => 0,
    ];
    $form['custom_language']['lingotek']['lingotek_disabled'] = [
      '#type' => 'checkbox',
      '#title' => t('Disabled for Lingotek translation'),
      // If we have a langcode, check if there is a locale or default to the one we can guess.
      '#default_value' => $langcode !== NULL ? !$this->lingotekConfiguration
        ->isLanguageEnabled($language) : FALSE,
      '#description' => $this
        ->t('Check this if you want Lingotek to ignore this language or locale.'),
    ];
    $form['custom_language']['lingotek']['lingotek_locale'] = [
      '#type' => 'textfield',
      '#title' => t('Locale'),
      '#autocomplete_route_name' => 'lingotek.supported_locales_autocomplete',
      // If we have a langcode, check if there is a locale or default to the one we can guess.
      '#default_value' => $langcode !== NULL ? str_replace("_", "-", $this->languageLocaleMapper
        ->getLocaleForLangcode($langcode)) : '',
      '#description' => $this
        ->t('The Lingotek locale this language maps to.') . ' ' . $this
        ->t('Use locale codes as <a href=":w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', [
        ':w3ctags' => 'http://www.w3.org/International/articles/language-tags/',
      ]),
    ];
    $form['custom_language']['lingotek']['lingotek_locale_link'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Lingotek supported locales list'),
      '#url' => Url::fromRoute('lingotek.supported_locales'),
      '#ajax' => [
        'class' => [
          'use-ajax',
        ],
      ],
      '#attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'dialog',
        'data-dialog-options' => Json::encode([
          'width' => 861,
          'height' => 700,
          'draggable' => TRUE,
          'autoResize' => FALSE,
        ]),
      ],
    ];

    // Buttons are different if adding or editing a language. We need validation
    // on both cases.
    if ($langcode) {
      $form['actions']['submit']['#validate'][] = LingotekLanguageForm::class . '::validateLocale';
    }
    else {
      $form['custom_language']['submit']['#validate'][] = LingotekLanguageForm::class . '::validateLocale';
    }
    $form['#entity_builders'][] = LingotekLanguageForm::class . '::languageEntityBuilder';
  }

  /**
   * Entity builder for the configurable language type form with lingotek options.
   *
   * @param string $entity_type
   *   The entity type.
   * @param \Drupal\language\ConfigurableLanguageInterface $language
   *   The language object.
   * @param array $form
   *   The form definition array for the configurable language entity.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @see lingotek_form_language_admin_add_form_alter()
   * @see lingotek_form_language_admin_edit_form_alter()
   */
  public static function languageEntityBuilder($entity_type, ConfigurableLanguageInterface $language, array &$form, FormStateInterface $form_state) {

    // We need to check if the value exists, as we are enabling by default those
    // predefined languages in Drupal.
    if ($form_state
      ->hasValue([
      'lingotek_locale',
    ])) {
      $lingotek_locale = $form_state
        ->getValue([
        'lingotek_locale',
      ]);
      $lingotekDisabled = $form_state
        ->getValue([
        'lingotek_disabled',
      ]);
      $language
        ->setThirdPartySetting('lingotek', 'locale', str_replace("-", "_", $lingotek_locale));
      $language
        ->setThirdPartySetting('lingotek', 'disabled', $lingotekDisabled);
    }
  }

  /**
   * Validate the configurable language type form with lingotek options.
   *
   * @param array $form
   *   The form definition array for the configurable language entity.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   *
   * @see lingotek_form_language_admin_add_form_alter()
   * @see lingotek_form_language_admin_edit_form_alter()
   */
  public static function validateLocale(&$form, FormStateInterface $form_state) {
    $form_key = [
      'lingotek_locale',
    ];
    if (!$form_state
      ->isValueEmpty($form_key)) {
      $value = $form_state
        ->getValue($form_key);
      try {
        if (!self::isValidLocale($value)) {
          $form_state
            ->setErrorByName('lingotek_locale', t('The Lingotek locale %locale does not exist.', [
            '%locale' => $value,
          ]));
        }
      } catch (LingotekApiException $lingotekApiException) {
        if ($lingotekApiException
          ->getCode() === 401) {
          \Drupal::messenger()
            ->addWarning("The Lingotek locale has not been validated.", 'warning');
        }
      }
    }
  }

  /**
   * Checks if a locale is valid.
   *
   * @param string $locale
   *   The locale to validate.
   *
   * @return bool
   *   TRUE if it's a valid locale in Lingotek. FALSE if not.
   */
  public static function isValidLocale($locale) {
    $locales = \Drupal::service('lingotek')
      ->getLocales();
    return in_array(str_replace("_", "-", $locale), $locales);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LingotekLanguageForm::$languageLocaleMapper protected property The language-locale mapper.
LingotekLanguageForm::$lingotek protected property A lingotek connector object
LingotekLanguageForm::$lingotekConfiguration protected property The Lingotek configuration service.
LingotekLanguageForm::form public function Alters the configurable language entity edit and add form.
LingotekLanguageForm::isValidLocale public static function Checks if a locale is valid.
LingotekLanguageForm::languageEntityBuilder public static function Entity builder for the configurable language type form with lingotek options.
LingotekLanguageForm::validateLocale public static function Validate the configurable language type form with lingotek options.
LingotekLanguageForm::__construct public function Constructs a new LingotekLanguageForm object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.