function ContentTranslationFormLanguageHooks::languageContentSettingsValidate

Form validation handler for content_translation_admin_settings_form().

See also

content_translation_admin_settings_form_submit()

File

core/modules/content_translation/src/Hook/ContentTranslationFormLanguageHooks.php, line 135

Class

ContentTranslationFormLanguageHooks
Form hook implementations for content_translation.

Namespace

Drupal\content_translation\Hook

Code

public static function languageContentSettingsValidate(array $form, FormStateInterface $form_state) : void {
  $settings =& $form_state->getValue('settings');
  foreach ($settings as $entity_type => $entity_settings) {
    foreach ($entity_settings as $bundle => $bundle_settings) {
      if (!empty($bundle_settings['translatable'])) {
        $name = "settings][{$entity_type}][{$bundle}][translatable";
        $translatable_fields = isset($settings[$entity_type][$bundle]['fields']) ? array_filter($settings[$entity_type][$bundle]['fields']) : FALSE;
        if (empty($translatable_fields)) {
          $t_args = [
            '%bundle' => $form['settings'][$entity_type][$bundle]['settings']['#label'],
          ];
          $form_state->setErrorByName($name, t('At least one field needs to be translatable to enable %bundle for translation.', $t_args));
        }
        $values = $bundle_settings['settings']['language'];
        if (empty($values['language_alterable']) && \Drupal::languageManager()->isLanguageLocked($values['langcode'])) {
          $locked_languages = [];
          foreach (\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_LOCKED) as $language) {
            $locked_languages[] = $language->getName();
          }
          $form_state->setErrorByName($name, t('Translation is not supported if language is always one of: @locked_languages', [
            '@locked_languages' => implode(', ', $locked_languages),
          ]));
        }
      }
    }
  }
}

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