You are here

class LingotekLanguageForm in Lingotek Translation 8

Same name and namespace in other branches
  1. 8.2 src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  2. 4.0.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  3. 3.0.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  4. 3.1.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  5. 3.2.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  6. 3.3.x src/Form/LingotekLanguageForm.php \Drupal\lingotek\Form\LingotekLanguageForm
  7. 3.4.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 16

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;

  /**
   * Constructs a new LingotekConfigTranslationService object.
   *
   * @param \Drupal\lingotek\LingotekInterface $lingotek
   *   A lingotek object.
   * @param \Drupal\lingotek\LanguageLocaleMapperInterface $language_locale_mapper
   *   The language-locale mapper.
   */
  public function __construct(LingotekInterface $lingotek, LanguageLocaleMapperInterface $language_locale_mapper) {
    $this->lingotek = $lingotek;
    $this->languageLocaleMapper = $language_locale_mapper;
  }

  /**
   * 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 ConfigurableLanguageInterface $language */
    $language = $form_state
      ->getFormObject()
      ->getEntity();
    $langcode = $language
      ->getId();
    $form['custom_language']['lingotek_locale'] = [
      '#type' => 'textfield',
      '#title' => t('Locale'),
      // 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)) : '',
      '#weight' => 0,
      '#description' => $this
        ->t('The Lingotek locale this language maps to.') . ' ' . $this
        ->t('Use language codes as <a href=":w3ctags">defined by the W3C</a> for interoperability. <em>Examples: "en", "en-gb" and "zh-hant".</em>', array(
        ':w3ctags' => 'http://www.w3.org/International/articles/language-tags/',
      )),
    ];

    // 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) {
    $form_key = [
      'lingotek_locale',
    ];
    if ($value = $form_state
      ->getValue($form_key)) {
      $language
        ->setThirdPartySetting('lingotek', 'locale', str_replace("-", "_", $value));
    }
  }

  /**
   * 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);
      if (!self::isValidLocale($value)) {
        $form_state
          ->setErrorByName('lingotek_locale', t('The Lingotek locale %locale does not exist.', [
          '%locale' => $value,
        ]));
      }
    }
  }

  /**
   * 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($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::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 LingotekConfigTranslationService object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.