ckeditor5-1.0.x-dev/src/Plugin/CKEditor5Plugin/Language.php
src/Plugin/CKEditor5Plugin/Language.php
<?php
namespace Drupal\ckeditor5\Plugin\CKEditor5Plugin;
use Drupal\ckeditor5\Plugin\CKEditor5PluginDefault;
use Drupal\ckeditor5\Plugin\CKEditor5PluginConfigurableInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Language\LanguageInterface;
use Drupal\editor\Entity\Editor;
/**
* CKEditor5 Language plugin.
*/
class Language extends CKEditor5PluginDefault implements CKEditor5PluginConfigurableInterface {
/**
* {@inheritdoc}
*/
public function getDynamicPluginConfig(array $static_plugin_config, Editor $editor) {
$predefined_languages = $editor->getSettings()['plugins']['language']['language_list'] === 'all' ?
LanguageManager::getStandardLanguageList() :
LanguageManager::getUnitedNationsLanguageList();
// Generate the language_list setting as expected by the CKEditor Language
// plugin, but key the values by the full language name so that we can sort
// them later on.
$language_list = [];
foreach ($predefined_languages as $langcode => $language) {
$english_name = $language[0];
$direction = empty($language[2]) ? NULL : $language[2];
$language_list[$english_name] = [
'title' => $english_name,
'languageCode' => $langcode,
];
if ($direction === LanguageInterface::DIRECTION_RTL) {
$language_list[$english_name]['textDirection'] = 'rtl';
}
}
// Sort on full language name.
ksort($language_list);
$config = $static_plugin_config;
$config['language']['textPartLanguage'] = array_values($language_list);
return $config;
}
/**
* {@inheritdoc}
*
* @see \Drupal\editor\Form\EditorImageDialog
* @see editor_image_upload_settings_form()
*/
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
// Defaults.
$config = ['language_list' => 'un'];
$settings = $editor->getSettings();
if (isset($settings['plugins']['language'])) {
$config = $settings['plugins']['language'];
}
$predefined_languages = LanguageManager::getStandardLanguageList();
$form['language_list'] = [
'#title' => $this->t('Language list'),
'#title_display' => 'invisible',
'#type' => 'select',
'#options' => [
'un' => $this->t("United Nations' official languages"),
'all' => $this->t('All @count languages', ['@count' => count($predefined_languages)]),
],
'#default_value' => $config['language_list'],
'#description' => $this->t('The list of languages to show in the language dropdown. The basic list will only show the <a href=":url">six official languages of the UN</a>. The extended list will show all @count languages that are available in Drupal.', [
':url' => 'https://www.un.org/en/sections/about-un/official-languages',
'@count' => count($predefined_languages),
]),
];
return $form;
}
}
