View source
<?php
namespace Drupal\ckeditor_font\Plugin\CKEditorPlugin;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
class FontCKEditorButton extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface {
public function getButtons() {
$modulePath = drupal_get_path('module', 'ckeditor_font');
return array(
'Font' => array(
'label' => $this
->t('Font Families'),
'image' => $modulePath . '/icons/font.png',
),
'FontSize' => array(
'label' => $this
->t('Font Size'),
'image' => $modulePath . '/icons/fontsize.png',
),
);
}
public function getLibraryPath() {
$directories[] = \Drupal::service('site.path') . "/libraries/";
$directories[] = 'libraries/';
if ($installProfile = \Drupal::installProfile()) {
$profile_path = drupal_get_path('profile', $installProfile);
$directories[] = "{$profile_path}/libraries/";
}
foreach ($directories as $dir) {
if (file_exists(DRUPAL_ROOT . '/' . $dir . 'font/plugin.js')) {
return $dir . 'font';
}
}
return FALSE;
}
public function getFile() {
$plugin_path = $this
->getLibraryPath() . '/plugin.js';
if (file_exists($plugin_path)) {
return $plugin_path;
}
return FALSE;
}
function isInternal() {
return FALSE;
}
function getDependencies(Editor $editor) {
return array();
}
function getLibraries(Editor $editor) {
return array();
}
public function getConfig(Editor $editor) {
$config = array();
$settings = $editor
->getSettings();
if (!isset($settings['plugins']['font']['font_names']) && !isset($settings['plugins']['font']['font_sizes'])) {
return $config;
}
if (isset($settings['plugins']['font']['font_sizes'])) {
$font_sizes = $settings['plugins']['font']['font_sizes'];
$sizes = $this
->generateFontStyleSetting($font_sizes, 'size');
if (!empty($sizes)) {
$config['fontSize_sizes'] = implode('; ', $sizes);
}
}
if (isset($settings['plugins']['font']['font_names'])) {
$font_names = $settings['plugins']['font']['font_names'];
$fonts = $this
->generateFontStyleSetting($font_names, 'font');
if (!empty($fonts)) {
$config['font_names'] = implode('; ', $fonts);
}
}
return $config;
}
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
$config = array(
'font_names' => '',
'font_sizes' => '',
);
$settings = $editor
->getSettings();
if (isset($settings['plugins']['font'])) {
$config = $settings['plugins']['font'];
}
$form['font_names'] = array(
'#title' => $this
->t('Font families'),
'#type' => 'textarea',
'#default_value' => $config['font_names'],
'#description' => $this
->t('Enter fonts on new lines. Fonts must be added with the following syntax:<br><code>Primary font, fallback1, fallback2|Font Label</code>'),
'#element_validate' => array(
array(
$this,
'validateFontValue',
),
),
);
$form['font_sizes'] = array(
'#title' => $this
->t('Font sizes'),
'#type' => 'textarea',
'#default_value' => $config['font_sizes'],
'#description' => $this
->t('Enter font sizes on new lines. Sizes must be added with the following syntax:<br><code>123px|Size label</code><br><code>123em|Size label</code><br><code>123%|Size label</code>'),
'#element_validate' => array(
array(
$this,
'validateFontSizeValue',
),
),
);
return $form;
}
public function validateFontValue(array $element, FormStateInterface $form_state) {
if ($this
->generateFontStyleSetting($element['#value'], 'font') === FALSE) {
$form_state
->setError($element, t('The provided list of fonts is syntactically incorrect.'));
}
}
public function validateFontSizeValue(array $element, FormStateInterface $form_state) {
if ($this
->generateFontStyleSetting($element['#value'], 'size') === FALSE) {
$form_state
->setError($element, t('The provided list of font sizes is syntactically incorrect.'));
}
}
protected function generateFontStyleSetting($fonts, $type) {
$font_style = array();
$fonts = trim($fonts);
if (empty($fonts)) {
return $font_style;
}
$fonts = str_replace(array(
"\r\n",
"\r",
), "\n", $fonts);
foreach (explode("\n", $fonts) as $font) {
$font = trim($font);
if (empty($font)) {
continue;
}
switch ($type) {
case 'font':
$pattern = '@^\\s*[a-zA-Z0-9\\,\\-\\s]+\\s*\\|\\s*.+\\s*$@';
break;
case 'size':
$pattern = '@^\\s*\\d+(\\.?\\d+)?(px|em|%|pt|rem)\\|.*$@';
break;
}
if (!preg_match($pattern, $font)) {
return FALSE;
}
list($families, $label) = explode('|', $font);
$font_name = $label ? $label . '/' : '';
$font_name .= $families;
$font_style[] = $font_name;
}
return $font_style;
}
}