View source
<?php
namespace Drupal\n1ed\Plugin;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
use Drupal\ckeditor\CKEditorPluginContextualInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
abstract class N1EDEcosystemCKEditorPlugin extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, CKEditorPluginContextualInterface {
public abstract function getPluginName();
public abstract function getModuleName();
public abstract function getButtonsDef();
public abstract function addControlsToForm(&$form, $editor, $config);
public function getButtons() {
if (!$this
->isInstalled()) {
return array();
}
return $this
->getButtonsDef();
}
public function getFile() {
return '/libraries/' . $this
->getPluginName() . '/plugin.min.js';
}
public function getDependencies(Editor $editor) {
return array();
}
public function getLibraries(Editor $editor) {
return array();
}
public function isInternal() {
return FALSE;
}
public function isEnabled(Editor $editor) {
return TRUE;
}
protected function getConfigParam($settings, $name, $default, $type) {
if (isset($settings[$name]) && is_string($settings[$name]) && strlen($settings[$name]) > 0) {
$value = $settings[$name];
}
else {
$value = $default;
}
if (isset($type) && $type == 'number') {
$value = intval($value);
}
else {
if (isset($type) && $type == 'boolean') {
if ($value == 'true') {
$value = true;
}
else {
if ($value == 'false') {
$value = false;
}
}
}
else {
if (isset($type) && $type == 'json') {
if ($value != '') {
$value = json_decode($value);
}
}
}
}
$drupal8_specific_defaults = array(
'urlFileManager' => '/flmngr',
'urlFiles' => '/sites/default/files/flmngr',
'dirUploads' => '/uploads/',
);
if (isset($drupal8_specific_defaults[$name]) && $value == '') {
$value = $drupal8_specific_defaults[$name];
}
return $value;
}
public function getConfig(Editor $editor) {
$settings = array();
if (isset($editor
->getSettings()['plugins'][$this
->getPluginName()])) {
$settings = $editor
->getSettings()['plugins'][$this
->getPluginName()];
}
return $settings;
}
protected function addStringToForm(&$form, $settings, $param, $default) {
$form[$param] = array(
'#type' => 'textfield',
'#title' => $param,
'#title_display' => 'invisible',
'#default_value' => $this
->getConfigParam($settings, $param, $default, 'string'),
'#attributes' => array(
'style' => 'display: none!important',
'data-n1ed-eco-param-name' => $param,
'data-n1ed-eco-param-type' => 'string',
'data-n1ed-eco-param-default' => $default,
),
);
}
protected function addNumberToForm(&$form, $settings, $param, $default) {
$form[$param] = array(
'#type' => 'textfield',
'#title' => $param,
'#title_display' => 'invisible',
'#default_value' => $this
->getConfigParam($settings, $param, $default, 'number'),
'#attributes' => array(
'style' => 'display: none!important',
'data-n1ed-eco-param-name' => $param,
'data-n1ed-eco-param-type' => 'number',
'data-n1ed-eco-param-default' => $default,
),
);
}
protected function addBooleanToForm(&$form, $settings, $param, $default) {
$form[$param] = array(
'#type' => 'textfield',
'#title' => $param,
'#title_display' => 'invisible',
'#default_value' => $this
->getConfigParam($settings, $param, $default, 'boolean') ? "true" : "false",
'#attributes' => array(
'style' => 'display: none!important',
'data-n1ed-eco-param-name' => $param,
'data-n1ed-eco-param-type' => 'boolean',
'data-n1ed-eco-param-default' => $default ? "true" : "false",
),
);
}
protected function addJsonToForm(&$form, $settings, $param, $default) {
$form[$param] = array(
'#type' => 'textarea',
'#title' => $param,
'#title_display' => 'invisible',
'#default_value' => $this
->getConfigParam($settings, $param, $default, 'string'),
'#attributes' => array(
'style' => 'display: none!important',
'data-n1ed-eco-param-name' => $param,
'data-n1ed-eco-param-type' => 'json',
'data-n1ed-eco-param-default' => $default,
),
);
}
protected function isInstalled() {
return file_exists($_SERVER['DOCUMENT_ROOT'] . '/libraries/' . $this
->getPluginName());
}
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$config = $this
->getConfig($editor);
$form['info'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'data-n1ed-eco-plugin' => $this
->getPluginName(),
'data-plugin-installed' => $this
->isInstalled() ? "true" : "false",
'style' => 'display: inline-block;margin-top: 13px;margin-left: 10px;',
],
'#value' => '<a href="#n1ed-conf">Configure ' . $this
->getPluginName() . ' add-on</a>',
];
$form['#attached']['library'][] = $this
->getModuleName() . '/' . $this
->getModuleName();
$this
->addControlsToForm($form, $editor, $config);
return $form;
}
}