You are here

hidden_language.module in Hidden Language 2.x

Same filename and directory in other branches
  1. 8 hidden_language.module
  2. 7 hidden_language.module

File

hidden_language.module
View source
<?php

use Drupal\Core\Form\FormStateInterface;
use Drupal\language\ConfigurableLanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;

/**
 * Implements hook_form_FORM_ID_alter() for language_admin_overview_form().
 */
function hidden_language_form_language_admin_overview_form_alter(&$form, FormStateInterface $form_state) {

  /** @var ConfigurableLanguageInterface[] $languages */
  $languages = $form['languages']['#languages'];
  array_splice($form['languages']['#header'], -1, 0, [
    'hidden' => t('Hidden'),
  ]);
  foreach ($languages as $langcode => $language) {
    $form['languages'][$langcode]['hidden'] = array(
      '#type' => 'checkbox',
      '#title' => t('Hidden'),
      '#title_display' => 'invisible',
      '#default_value' => $language
        ->getThirdPartySetting('hidden_language', 'hidden', FALSE),
    );
  }
  $form['#submit'][] = 'hidden_language_language_admin_overview_form_submit';
}

/**
 * Form submission handler for language_admin_add_form().
 *
 * Store information about hidden languages.
 */
function hidden_language_language_admin_overview_form_submit($form, FormStateInterface $form_state) {

  /** @var ConfigurableLanguageInterface[] $languages */
  $languages = $form['languages']['#languages'];
  $language_form_values = $form_state
    ->getValue('languages');
  foreach ($languages as $language) {
    if (isset($language_form_values[$language
      ->id()])) {
      $language_form_value = $language_form_values[$language
        ->id()];
      $language
        ->setThirdPartySetting('hidden_language', 'hidden', boolval($language_form_value['hidden']));
      $language
        ->save();
    }
  }
}

/**
 * Implements hook_language_switch_links_alter().
 *
 * Hide links to hidden languages.
 */
function hidden_language_language_switch_links_alter(array &$links, $type, $path) {
  $languages = \Drupal::languageManager()
    ->getLanguages();

  /** @var ConfigurableLanguage[] $languages */
  $languages = ConfigurableLanguage::loadMultiple(array_keys($languages));
  foreach ($languages as $langcode => $language) {
    if ($language
      ->getThirdPartySetting('hidden_language', 'hidden', FALSE)) {
      unset($links[$langcode]);
    }
  }
}

Functions