54056a525aecb28226d5c2a09745afa5d0fbcb18
[yaffs-website] / web / modules / contrib / linkit / src / Plugin / CKEditorPlugin / Linkit.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Plugin\CKEditorPlugin\Linkit.
6  */
7
8 namespace Drupal\linkit\Plugin\CKEditorPlugin;
9
10 use Drupal\ckeditor\CKEditorPluginBase;
11 use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
12 use Drupal\Core\Entity\EntityStorageInterface;
13 use Drupal\Core\Form\FormStateInterface;
14 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
15 use Drupal\editor\Entity\Editor;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17
18 /**
19  * Defines the "linkit" plugin.
20  *
21  * @CKEditorPlugin(
22  *   id = "linkit",
23  *   label = @Translation("Linkit"),
24  *   module = "linkit"
25  * )
26  */
27 class Linkit extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, ContainerFactoryPluginInterface {
28
29   /**
30    * The Linkit profile storage.
31    *
32    * @var \Drupal\Core\Entity\EntityStorageInterface
33    */
34   protected $linkitProfileStorage;
35
36   /**
37    * {@inheritdoc}
38    */
39   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $linkit_profile_storage) {
40     parent::__construct($configuration, $plugin_id, $plugin_definition);
41     $this->linkitProfileStorage = $linkit_profile_storage;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
48     return new static(
49       $configuration,
50       $plugin_id,
51       $plugin_definition,
52       $container->get('entity.manager')->getStorage('linkit_profile')
53     );
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function getFile() {
60     return drupal_get_path('module', 'linkit') . '/js/plugins/linkit/plugin.js';
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   public function getConfig(Editor $editor) {
67     return array(
68       'linkit_dialogTitleAdd' => t('Add link'),
69       'linkit_dialogTitleEdit' => t('Edit link'),
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function getButtons() {
77     return array(
78       'Linkit' => array(
79         'label' => t('Linkit'),
80         'image' => drupal_get_path('module', 'linkit') . '/js/plugins/linkit/linkit.png',
81       ),
82     );
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
89     $settings = $editor->getSettings();
90
91     $all_profiles = $this->linkitProfileStorage->loadMultiple();
92
93     $options = array();
94     foreach ($all_profiles as $profile) {
95       $options[$profile->id()] = $profile->label();
96     }
97
98     $form['linkit_profile'] = array(
99       '#type' => 'select',
100       '#title' => t('Select a linkit profile'),
101       '#options' => $options,
102       '#default_value' => isset($settings['plugins']['linkit']) ? $settings['plugins']['linkit'] : '',
103       '#empty_option' => $this->t('- Select profile -'),
104       '#description' => $this->t('Select the linkit profile you wish to use with this text format.'),
105       '#element_validate' => array(
106         array($this, 'validateLinkitProfileSelection'),
107       ),
108     );
109
110     return $form;
111   }
112
113   /**
114    * #element_validate handler for the "linkit_profile" element in settingsForm().
115    */
116   public function validateLinkitProfileSelection(array $element, FormStateInterface $form_state) {
117     $toolbar_buttons = $form_state->getValue(array('editor', 'settings', 'toolbar', 'button_groups'));
118     if (strpos($toolbar_buttons, '"Linkit"') !== FALSE && empty($element['#value'])) {
119       $form_state->setError($element, t('Please select the linkit profile you wish to use.'));
120     }
121   }
122
123 }