delivery-8.x-1.x-dev/modules/workspaces_allowed_languages/workspaces_allowed_languages.module

modules/workspaces_allowed_languages/workspaces_allowed_languages.module
<?php

/**
 * @file
 * Provides a language field on workspaces to restrict the languages which can
 * be used when accessing that workspace.
 */

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Url;

/**
 * Implements hook_entity_base_field_info().
 */
function workspaces_allowed_languages_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type->id() === 'workspace') {
    $fields['primary_language'] = BaseFieldDefinition::create('string')
      // @todo: change maybe the form element to be a select field.
      ->setLabel(t('Primary language'))
      ->setDescription(t('The primary language for this workspace. In case of doubt, the language will always be set to this one.'))
      ->setRevisionable(TRUE)
      ->setCardinality(1)
      ->setDisplayOptions('form', array(
        'type' => 'string',
        'weight' => 10,
      ))
      ->setDisplayConfigurable('form', TRUE);
    $fields['secondary_languages'] = BaseFieldDefinition::create('string')
      // @todo: change maybe the form element to be a select field.
      ->setLabel(t('Secondary languages'))
      ->setDescription(t('A set of languages which are also allowed to be used on this workspace.'))
      ->setRevisionable(TRUE)
      ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => 10,
      ])
      ->setDisplayConfigurable('form', TRUE);
    return $fields;
  }
}

/**
 * Implements hook_country_switcher_links_alter().
 */
function workspaces_allowed_languages_country_switcher_links_alter(&$links) {
  $language_manager = \Drupal::languageManager();
  $native_languages = $language_manager->getNativeLanguages();
  foreach ($links as &$links_group) {
    if (empty($links_group['countries'])) {
      continue;
    }
    $new_links = [];
    foreach ($links_group['countries'] as $id => $country_info) {
      $allowed_languages = $country_info['country']->get('allowed_languages')->getValue();
      if (!empty($allowed_languages)) {
        foreach ($allowed_languages as $allowed_language) {
          if (empty($native_languages[$allowed_language['value']])) {
            continue;
          }
          $language = $native_languages[$allowed_language['value']];
          $new_link = $country_info;
          // Clone the url because we need to also change some options for it.
          $new_link['url'] = clone $country_info['url'];
          $new_link['url']->setOption('language', $language);
          // The displayed language name should be translated in that specific
          // language.
          $new_link['label'] .= ' (' . t($language->label(), [], ['langcode' => $language->getId(), 'context' => 'language_name']) . ')';
          $new_links[$id . '-' . $language->getId()] = $new_link;
        }
      }
      else {
        // If no language information available, just list the country as it is.
        $new_links[$id] = $country_info;
      }
    }
    $links_group['countries'] = $new_links;
  }
}

/**
 * Implements hook_language_switch_links_alter().
 */
function workspaces_allowed_languages_language_switch_links_alter(array &$links, $type, Url $url) {
  /* @var \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager */
  $workspace_manager = \Drupal::getContainer()->get('workspaces.manager');
  $current_workspace = $workspace_manager->getActiveWorkspace();
  $allowed_languages = $current_workspace->get('allowed_languages')->getValue();
  if (!empty($allowed_languages)) {
    $languages = [];
    foreach ($allowed_languages as $allowed_language) {
      $languages[$allowed_language['value']] = $allowed_language['value'];
    }
    $links = array_intersect_key($links, $languages);
  }
}

/**
 * Implements hook_element_info_alter().
 */
/**
 * Implements hook_element_info_alter().
 */
function workspaces_allowed_languages_element_info_alter(array &$info) {
  if (isset($info['language_select'])) {
    $info['language_select']['#process'][] = 'workspaces_allowed_languages_process_language_select';
  }
}

/**
 * Preprocess the language selection widget to only allow languages assigned to
 * the current workspace.
 */
function workspaces_allowed_languages_process_language_select($element) {
  /** @var \Drupal\workspaces\WorkspaceManagerInterface $workspaceManager */
  $workspaceManager = \Drupal::service('workspaces.manager');
  $workspace = $workspaceManager->getActiveWorkspace();

  // Don't restrict languages if the workspace doesn't have any assigned.
  if ($workspace->primary_language->count() === 0 && $workspace->secondary_languages->count() === 0) {
    return $element;
  }

  $languages = [$workspace->primary_language->value];
  foreach ($workspace->secondary_languages as $item) {
    $languages[] = $item->value;
  }

  foreach (array_keys($element['#options']) as $key) {
    if (!in_array($key, $languages)) {
      unset($element['#options'][$key]);
    }

  }

  if (!in_array($element['#default_value'], $languages)) {
    $element['#default_value'] = $languages[0];
  }

  return $element;
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc