getLanguageTypes example

// Alter LanguageInterface::TYPE_CONTENT to be configurable.       'language_test.content_language_type' => TRUE,
    ]);
    $this->container->get('module_installer')->install(['language_test']);
    $this->resetAll();

    // Check that fixed language types are properly configured without the need     // of saving the language negotiation settings.     $this->checkFixedLanguageTypes();

    $type = LanguageInterface::TYPE_CONTENT;
    $language_types = $this->languageManager()->getLanguageTypes();
    $this->assertContains($type$language_types, 'Content language type is configurable.');

    // Enable some core and custom language negotiation methods. The test     // language type is supposed to be configurable.     $test_type = 'test_language_type';
    $interface_method_id = LanguageNegotiationUI::METHOD_ID;
    $test_method_id = 'test_language_negotiation_method';
    $form_field = $type . '[enabled][' . $interface_method_id . ']';
    $edit = [
      $form_field => TRUE,
      $type . '[enabled][' . $test_method_id . ']' => TRUE,
      

  public function __construct(LanguageManagerInterface $language_manager) {
    $this->languageManager = $language_manager;
  }

  /** * {@inheritdoc} */
  public function getRuntimeContexts(array $unqualified_context_ids) {
    // Add a context for each language type.     $language_types = $this->languageManager->getLanguageTypes();
    $info = $this->languageManager->getDefinedLanguageTypesInfo();

    if ($unqualified_context_ids) {
      foreach ($unqualified_context_ids as $unqualified_context_id) {
        if (array_search($unqualified_context_id$language_types) === FALSE) {
          unset($language_types[$unqualified_context_id]);
        }
      }
    }

    $result = [];
    

  public function getPrimaryNegotiationMethod($type) {
    $enabled_methods = $this->getEnabledNegotiators($type);
    return empty($enabled_methods) ? LanguageNegotiatorInterface::METHOD_ID : key($enabled_methods);
  }

  /** * {@inheritdoc} */
  public function isNegotiationMethodEnabled($method_id$type = NULL) {
    $enabled = FALSE;
    $language_types = !empty($type) ? [$type] : $this->languageManager->getLanguageTypes();

    foreach ($language_types as $type) {
      $enabled_methods = $this->getEnabledNegotiators($type);
      if (isset($enabled_methods[$method_id])) {
        $enabled = TRUE;
        break;
      }
    }

    return $enabled;
  }

  
use StringTranslationTrait;

  /** * {@inheritdoc} */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $language_manager = \Drupal::languageManager();

    if ($language_manager instanceof ConfigurableLanguageManagerInterface) {
      $info = $language_manager->getDefinedLanguageTypesInfo();
      $configurable_types = $language_manager->getLanguageTypes();
      foreach ($configurable_types as $type) {
        $this->derivatives[$type] = $base_plugin_definition;
        $this->derivatives[$type]['admin_label'] = $this->t('Language switcher (@type)', ['@type' => $info[$type]['name']]);
      }
      // If there is just one configurable type then change the title of the       // block.       if (count($configurable_types) == 1) {
        $this->derivatives[reset($configurable_types)]['admin_label'] = $this->t('Language switcher');
      }
    }

    

  public function getContext($type = NULL) {
    if ($type === NULL) {
      $context_parts = [];
      if ($this->languageManager->isMultilingual()) {
        foreach ($this->languageManager->getLanguageTypes() as $type) {
          $context_parts[] = $this->languageManager->getCurrentLanguage($type)->getId();
        }
      }
      else {
        $context_parts[] = $this->languageManager->getCurrentLanguage()->getId();
      }
      return implode(',', $context_parts);
    }
    else {
      $language_types = $this->languageManager->getDefinedLanguageTypesInfo();
      if (!isset($language_types[$type])) {
        
return $this->languageTypes['all'];
  }

  /** * Retrieves language types from the configuration storage. * * @return array * An array of language type names. */
  protected function loadLanguageTypesConfiguration() {
    if (!$this->languageTypes) {
      $this->languageTypes = $this->configFactory->get('language.types')->get() ?: ['configurable' => [], 'all' => parent::getLanguageTypes()];
    }
    return $this->languageTypes;
  }

  /** * {@inheritdoc} */
  public function getDefinedLanguageTypesInfo() {
    if (!isset($this->languageTypesInfo)) {
      $defaults = parent::getDefinedLanguageTypesInfo();

      
/** * Initializes the local cache for language path processors. * * @param string $scope * The scope of the processors: "inbound" or "outbound". */
  protected function initProcessors($scope) {
    $interface = '\Drupal\Core\PathProcessor\\' . Unicode::ucfirst($scope) . 'PathProcessorInterface';
    $this->processors[$scope] = [];
    $weights = [];
    foreach ($this->languageManager->getLanguageTypes() as $type) {
      foreach ($this->negotiator->getNegotiationMethods($type) as $method_id => $method) {
        if (!isset($this->processors[$scope][$method_id])) {
          $reflector = new \ReflectionClass($method['class']);
          if ($reflector->implementsInterface($interface)) {
            $this->processors[$scope][$method_id] = $this->negotiator->getNegotiationMethodInstance($method_id);
            $weights[$method_id] = $method['weight'];
          }
        }
      }
    }

    
$name = $this->t($name);
      }
      $list[PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT] = $name;
      // Remove site default language from $languages so it's not added       // twice with the real languages below.       unset($languages[LanguageInterface::LANGCODE_SITE_DEFAULT]);
    }

    // Add in negotiated languages, if requested.     if ($flags & PluginBase::INCLUDE_NEGOTIATED) {
      $types_info = $manager->getDefinedLanguageTypesInfo();
      $types = $manager->getLanguageTypes();
      // We only go through the configured types.       foreach ($types as $id) {
        if (isset($types_info[$id]['name'])) {
          $name = $types_info[$id]['name'];
          // Surround IDs by '***LANGUAGE_...***', to avoid query collisions.           $id = '***LANGUAGE_' . $id . '***';
          $list[$id] = $this->t('@type language selected for page', ['@type' => $name]);
        }
      }
      if (!empty($current_values)) {
        foreach ($types_info as $id => $type) {
          
Home | Imprint | This part of the site doesn't use cookies.