Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / ckeditor / src / Plugin / CKEditorPlugin / Language.php
1 <?php
2
3 namespace Drupal\ckeditor\Plugin\CKEditorPlugin;
4
5 use Drupal\ckeditor\CKEditorPluginBase;
6 use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
7 use Drupal\ckeditor\CKEditorPluginCssInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Language\LanguageManager;
10 use Drupal\Core\Language\LanguageInterface;
11 use Drupal\editor\Entity\Editor;
12
13 /**
14  * Defines the "language" plugin.
15  *
16  * @CKEditorPlugin(
17  *   id = "language",
18  *   label = @Translation("Language")
19  * )
20  */
21 class Language extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, CKEditorPluginCssInterface {
22
23   /**
24    * {@inheritdoc}
25    */
26   public function isInternal() {
27     return TRUE;
28   }
29
30   /**
31    * {@inheritdoc}
32    */
33   public function getFile() {
34     // This plugin is already part of Drupal core's CKEditor build.
35     return FALSE;
36   }
37
38   /**
39    * {@inheritdoc}
40    */
41   public function getLibraries(Editor $editor) {
42     return ['ckeditor/drupal.ckeditor.plugins.language'];
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function getConfig(Editor $editor) {
49     $language_list = [];
50     $config = ['language_list' => 'un'];
51     $settings = $editor->getSettings();
52     if (isset($settings['plugins']['language'])) {
53       $config = $settings['plugins']['language'];
54     }
55
56     $predefined_languages = ($config['language_list'] === 'all') ?
57       LanguageManager::getStandardLanguageList() :
58       LanguageManager::getUnitedNationsLanguageList();
59
60     // Generate the language_list setting as expected by the CKEditor Language
61     // plugin, but key the values by the full language name so that we can sort
62     // them later on.
63     foreach ($predefined_languages as $langcode => $language) {
64       $english_name = $language[0];
65       $direction = empty($language[2]) ? NULL : $language[2];
66       if ($direction === LanguageInterface::DIRECTION_RTL) {
67         $language_list[$english_name] = $langcode . ':' . $english_name . ':rtl';
68       }
69       else {
70         $language_list[$english_name] = $langcode . ':' . $english_name;
71       }
72     }
73
74     // Sort on full language name.
75     ksort($language_list);
76     $config = ['language_list' => array_values($language_list)];
77     return $config;
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function getButtons() {
84     return [
85       'Language' => [
86         'label' => $this->t('Language'),
87         'image_alternative' => [
88           '#type' => 'inline_template',
89           '#template' => '<a href="#" class="cke-icon-only" role="button" title="' . $this->t('Language') . '" aria-label="' . $this->t('Language') . '"><span class="cke_button_icon cke_button__language_icon">' . $this->t('Language') . '</span></a>',
90         ],
91       ],
92     ];
93   }
94
95   /**
96    * {@inheritdoc}
97    */
98   public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
99     // Defaults.
100     $config = ['language_list' => 'un'];
101     $settings = $editor->getSettings();
102     if (isset($settings['plugins']['language'])) {
103       $config = $settings['plugins']['language'];
104     }
105
106     $predefined_languages = LanguageManager::getStandardLanguageList();
107     $form['language_list'] = [
108       '#title' => $this->t('Language list'),
109       '#title_display' => 'invisible',
110       '#type' => 'select',
111       '#options' => [
112         'un' => $this->t("United Nations' official languages"),
113         'all' => $this->t('All @count languages', ['@count' => count($predefined_languages)]),
114       ],
115       '#default_value' => $config['language_list'],
116       '#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.', [
117         ':url' => 'https://www.un.org/en/sections/about-un/official-languages',
118         '@count' => count($predefined_languages),
119       ]),
120       '#attached' => ['library' => ['ckeditor/drupal.ckeditor.language.admin']],
121     ];
122
123     return $form;
124   }
125
126   /**
127    * {@inheritdoc}
128    */
129   public function getCssFiles(Editor $editor) {
130     return [
131         drupal_get_path('module', 'ckeditor') . '/css/plugins/language/ckeditor.language.css',
132     ];
133   }
134
135 }