function ContentTranslationEnableTranslationPerBundle::configElementValidate

Same name and namespace in other branches
  1. 11.x core/modules/content_translation/src/ContentTranslationEnableTranslationPerBundle.php \Drupal\content_translation\ContentTranslationEnableTranslationPerBundle::configElementValidate()

Provides a validation callback for the language enable/disable element.

Checks whether translation can be enabled: if language is set to one of the special languages and language selector is not hidden, translation cannot be enabled.

Parameters

array $element: The initial form element.

\Drupal\Core\Form\FormStateInterface $formState: The form current state.

array $form: The form render array.

File

core/modules/content_translation/src/ContentTranslationEnableTranslationPerBundle.php, line 117

Class

ContentTranslationEnableTranslationPerBundle
Provides a widget to enable content translation per entity bundle.

Namespace

Drupal\content_translation

Code

public function configElementValidate(array $element, FormStateInterface $formState, array &$form) : void {
  $key = $formState->get([
    'content_translation',
    'key',
  ]);
  $values = $formState->getValue($key);
  if (!$values['language_alterable'] && $values['content_translation'] && $this->languageManager
    ->isLanguageLocked($values['langcode'])) {
    $lockedLanguages = [];
    foreach ($this->languageManager
      ->getLanguages(LanguageInterface::STATE_LOCKED) as $language) {
      $lockedLanguages[$language->getId()] = $language->getName();
    }
    // @todo Set the correct form element name as soon as the element parents
    //   are correctly set. We should be using NestedArray::getValue() but for
    //   now we cannot.
    $formState->setErrorByName('', $this->t('"Show language selector" is not compatible with translating content that has default language: %choice. Either do not hide the language selector or pick a specific language.', [
      '%choice' => $lockedLanguages[$values['langcode']],
    ]));
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.