"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/ckeditor/src/Plugin/CKEditorPlugin/Language.php" (26 Nov 2020, 4181 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Language.php" see the Fossies "Dox" file reference documentation.

    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     $label = $this->t('Language');
   85     return [
   86       'Language' => [
   87         'label' => $label,
   88         'image_alternative' => [
   89           '#type' => 'inline_template',
   90           '#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>',
   91         ],
   92       ],
   93     ];
   94   }
   95 
   96   /**
   97    * {@inheritdoc}
   98    */
   99   public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
  100     // Defaults.
  101     $config = ['language_list' => 'un'];
  102     $settings = $editor->getSettings();
  103     if (isset($settings['plugins']['language'])) {
  104       $config = $settings['plugins']['language'];
  105     }
  106 
  107     $predefined_languages = LanguageManager::getStandardLanguageList();
  108     $form['language_list'] = [
  109       '#title' => $this->t('Language list'),
  110       '#title_display' => 'invisible',
  111       '#type' => 'select',
  112       '#options' => [
  113         'un' => $this->t("United Nations' official languages"),
  114         'all' => $this->t('All @count languages', ['@count' => count($predefined_languages)]),
  115       ],
  116       '#default_value' => $config['language_list'],
  117       '#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.', [
  118         ':url' => 'https://www.un.org/en/sections/about-un/official-languages',
  119         '@count' => count($predefined_languages),
  120       ]),
  121       '#attached' => ['library' => ['ckeditor/drupal.ckeditor.language.admin']],
  122     ];
  123 
  124     return $form;
  125   }
  126 
  127   /**
  128    * {@inheritdoc}
  129    */
  130   public function getCssFiles(Editor $editor) {
  131     return [
  132         drupal_get_path('module', 'ckeditor') . '/css/plugins/language/ckeditor.language.css',
  133     ];
  134   }
  135 
  136 }