BaseExtension.php in Markdown 3.0.x
File
src/Plugin/Markdown/Extension/BaseExtension.php
View source
<?php
namespace Drupal\markdown\Plugin\Markdown\Extension;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\Url;
use Drupal\markdown\Annotation\MarkdownExtension;
use Drupal\markdown\Plugin\Filter\MarkdownFilterInterface;
use Drupal\markdown\Traits\MarkdownStatesTrait;
class BaseExtension extends PluginBase implements MarkdownExtensionInterface {
use MarkdownStatesTrait;
public static function installed() : bool {
return FALSE;
}
public static function version() {
return NULL;
}
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
}
protected function baseConfigurationDefaults() {
return [
'id' => $this
->getPluginId(),
'label' => $this
->t('Broken'),
'provider' => $this->pluginDefinition['provider'],
'settings' => $this
->defaultSettings() + [
'enabled' => FALSE,
],
];
}
public function defaultSettings() {
return [];
}
public function calculateDependencies() {
return [];
}
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return $this->configuration;
}
public function getDescription() {
return $this->pluginDefinition['description'] ?? NULL;
}
public function getLabel($version = TRUE) {
$label = $this->pluginDefinition['label'] ?? $this->pluginId;
if ($version && ($version = $this
->getVersion())) {
$label .= " ({$version})";
}
return $label;
}
public function getSetting($name) {
$settings = $this
->getSettings();
return isset($settings[$name]) ? $settings[$name] : NULL;
}
public function getUrl() {
$url = $this->pluginDefinition['url'] ?? NULL;
if ($url && UrlHelper::isExternal($url)) {
return Url::fromUri($url);
}
return $url ? Url::fromUserInput($url) : NULL;
}
public function getVersion() {
return $this->pluginDefinition['version'] ?? NULL;
}
public function isEnabled() {
return !!$this
->getSetting('enabled');
}
public function isInstalled() : bool {
return $this->pluginDefinition['installed'] ?? FALSE;
}
public function label() {
return $this->configuration['label'] ?: $this->pluginId;
}
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->baseConfigurationDefaults(), $this
->defaultConfiguration(), $configuration);
}
public function getSettings() {
return $this->configuration['settings'];
}
public function setSetting($name, $value = NULL) {
if (isset($value)) {
if (isset($this->configuration['settings'][$name]) && ($type = gettype($this->configuration['settings'][$name]))) {
$original_value = is_object($value) ? clone $value : $value;
if (!settype($value, $type)) {
$value = $original_value;
}
}
$this->configuration['settings'][$name] = $value;
}
else {
unset($this->configuration['settings'][$name]);
}
}
public function setSettings(array $settings = []) {
foreach ($settings as $name => $value) {
$this
->setSetting($name, $value);
}
}
public function settingsForm(array $element, FormStateInterface $formState, MarkdownFilterInterface $filter) {
$definition = $this
->getPluginDefinition();
$element['provider'] = [
'#type' => 'value',
'#value' => $definition['provider'],
];
return $element;
}
}