View source
<?php
namespace Drupal\ckeditor\Plugin\CKEditorPlugin;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
use Drupal\ckeditor\CKEditorPluginCssInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageManager;
use Drupal\Core\Language\LanguageInterface;
use Drupal\editor\Entity\Editor;
class Language extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, CKEditorPluginCssInterface {
public function isInternal() {
return TRUE;
}
public function getFile() {
return FALSE;
}
public function getLibraries(Editor $editor) {
return [
'ckeditor/drupal.ckeditor.plugins.language',
];
}
public function getConfig(Editor $editor) {
$language_list = [];
$config = [
'language_list' => 'un',
];
$settings = $editor
->getSettings();
if (isset($settings['plugins']['language'])) {
$config = $settings['plugins']['language'];
}
$predefined_languages = $config['language_list'] === 'all' ? LanguageManager::getStandardLanguageList() : LanguageManager::getUnitedNationsLanguageList();
foreach ($predefined_languages as $langcode => $language) {
$english_name = $language[0];
$direction = empty($language[2]) ? NULL : $language[2];
if ($direction === LanguageInterface::DIRECTION_RTL) {
$language_list[$english_name] = $langcode . ':' . $english_name . ':rtl';
}
else {
$language_list[$english_name] = $langcode . ':' . $english_name;
}
}
ksort($language_list);
$config = [
'language_list' => array_values($language_list),
];
return $config;
}
public function getButtons() {
$label = $this
->t('Language');
return [
'Language' => [
'label' => $label,
'image_alternative' => [
'#type' => 'inline_template',
'#template' => '<a href="#" class="cke-icon-only" role="button" title="' . $label . '" aria-label="' . $label . '"><span class="cke_button_icon cke_button__language_icon">' . $label . '</span></a>',
],
],
];
}
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$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),
]),
'#attached' => [
'library' => [
'ckeditor/drupal.ckeditor.language.admin',
],
],
];
return $form;
}
public function getCssFiles(Editor $editor) {
return [
$this
->getModulePath('ckeditor') . '/css/plugins/language/ckeditor.language.css',
];
}
}