You are here

function disable_language_form_alter in Disable language 8

Implements hook_form_alter().

Add the disabled option to the form.

File

./disable_language.module, line 17
Contains disable_language.module.

Code

function disable_language_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Add checkbox to language edit form.
  if ($form_id === 'language_admin_edit_form') {
    $configurableLanguage = $form_state
      ->getFormObject()
      ->getEntity();
    $this_language = $form_state
      ->getFormObject()
      ->getEntity();
    $languages = Drupal::languageManager()
      ->getLanguages();
    $options = [];
    foreach ($languages as $language) {

      // Only include this language if its not itself.
      if ($language
        ->getId() != $this_language
        ->getId()) {
        $options[$language
          ->getId()] = $language
          ->getName();
      }
    }
    $configurableLanguage = $form_state
      ->getFormObject()
      ->getEntity();
    $form['disable'] = [
      '#type' => 'checkbox',
      '#title' => t('Disable language'),
      '#description' => t('This will remove the language from the language switcher and filter out the Simple XML sitemap'),
      '#default_value' => $configurableLanguage
        ->getThirdPartySetting('disable_language', 'disable'),
    ];
    $form['redirect_language'] = [
      '#type' => 'select',
      '#title' => t('Select language to which we redirect'),
      '#description' => t('This option will redirect to the selected language when a user calls the disabled language'),
      '#options' => $options,
      '#default_value' => $configurableLanguage
        ->getThirdPartySetting('disable_language', 'redirect_language'),
      '#states' => [
        'visible' => [
          ':input[name="disable"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['#entity_builders'][] = 'disable_language_language_admin_edit_form_builder';

    // @todo: Add validation so people can't disable all languages
  }

  // Add disabled information to the overview.
  if ($form_id === 'language_admin_overview_form') {
    $configurableLanguages = \Drupal::service('disable_language.disable_language_manager')
      ->getConfigurableLanguages();

    // Add the disabled header in the right position.
    $offset = array_search('weight', array_keys($form['languages']['#header']));
    $form['languages']['#header'] = array_slice($form['languages']['#header'], 0, $offset, TRUE) + [
      'disabled' => t('Disabled'),
    ] + array_slice($form['languages']['#header'], $offset, NULL, TRUE);
    foreach ($configurableLanguages as $langcode => $configurableLanguage) {
      $disabled = $configurableLanguage
        ->getThirdPartySetting('disable_language', 'disable') ? t('Yes') : t('No');
      $data['disabled']['data']['#markup'] = $disabled;
      $form['languages'][$langcode] = array_slice($form['languages'][$langcode], 0, $offset, TRUE) + $data + array_slice($form['languages'][$langcode], $offset, NULL, TRUE);
    }
  }
}