Shortcode.php in Shortcode 2.0.x
File
src/Plugin/Filter/Shortcode.php
View source
<?php
namespace Drupal\shortcode\Plugin\Filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
class Shortcode extends FilterBase {
public function settingsForm(array $form, FormStateInterface $form_state) {
$shortcodeService = \Drupal::service('shortcode');
$shortcodes = $shortcodeService
->loadShortcodePlugins();
$shortcodes_by_provider = [];
foreach ($shortcodes as $shortcode_id => $shortcode_info) {
$provider_id = $shortcode_info['provider'];
if (!isset($shortcodes_by_provider[$provider_id])) {
$shortcodes_by_provider[$provider_id] = [];
}
$shortcodes_by_provider[$provider_id][$shortcode_id] = $shortcode_info;
}
$settings = [];
foreach ($shortcodes_by_provider as $provider_id => $shortcodes) {
$settings['header-' . $provider_id] = [
'#markup' => '<b class="shortcodeSectionHeader">Shortcodes provided by ' . $provider_id . '</b>',
];
$sorted_shortcodes = $shortcodes;
uasort($sorted_shortcodes, function ($a, $b) {
return $b['weight'] - $a['weight'];
});
foreach ($sorted_shortcodes as $shortcode_id => $shortcode_info) {
$settings[$shortcode_id] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable %name shortcode', [
'%name' => $shortcode_info['title'],
]),
'#default_value' => isset($this->settings[$shortcode_id]) ? $this->settings[$shortcode_id] : TRUE,
'#description' => isset($shortcode_info['description']) ? $shortcode_info['description'] : $this
->t('Enable or disable this shortcode in this input format'),
];
}
}
return $settings;
}
public function process($text, $langcode) {
if (!empty($text)) {
$shortcodeEngine = \Drupal::service('shortcode');
$text = $shortcodeEngine
->process($text, $langcode, $this);
}
return new FilterProcessResult($text);
}
public function tips($long = FALSE) {
$type = \Drupal::service('shortcode');
$shortcodes = $type
->getShortcodePlugins($this);
$type = \Drupal::service('plugin.manager.shortcode');
$tips = [];
foreach ($shortcodes as $shortcode_info) {
$shortcode = $type
->createInstance($shortcode_info['id']);
$tips[] = $shortcode
->tips($long);
}
$output = '';
foreach ($tips as $tip) {
$output .= '<li>' . $tip . '</li>';
}
return '<p>You can use wp-like shortcodes such as: </p><ul>' . $output . '</ul>';
}
}