You are here

class BaseExtension in Markdown 3.0.x

Base class for markdown extensions.

Plugin annotation


@MarkdownExtension(
  id = "_broken",
  label = @Translation("Missing Extension"),
)

Hierarchy

Expanded class hierarchy of BaseExtension

File

src/Plugin/Markdown/Extension/BaseExtension.php, line 22

Namespace

Drupal\markdown\Plugin\Markdown\Extension
View source
class BaseExtension extends PluginBase implements MarkdownExtensionInterface {
  use MarkdownStatesTrait;

  /**
   * {@inheritdoc}
   */
  public static function installed() : bool {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public static function version() {
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this
      ->setConfiguration($configuration);
  }

  /**
   * Returns generic default configuration for markdown extension plugins.
   *
   * @return array
   *   An associative array with the default configuration.
   */
  protected function baseConfigurationDefaults() {
    return [
      'id' => $this
        ->getPluginId(),
      'label' => $this
        ->t('Broken'),
      'provider' => $this->pluginDefinition['provider'],
      'settings' => $this
        ->defaultSettings() + [
        'enabled' => FALSE,
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultSettings() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this->pluginDefinition['description'] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getLabel($version = TRUE) {
    $label = $this->pluginDefinition['label'] ?? $this->pluginId;
    if ($version && ($version = $this
      ->getVersion())) {
      $label .= " ({$version})";
    }
    return $label;
  }

  /**
   * {@inheritdoc}
   */
  public function getSetting($name) {
    $settings = $this
      ->getSettings();
    return isset($settings[$name]) ? $settings[$name] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getUrl() {
    $url = $this->pluginDefinition['url'] ?? NULL;
    if ($url && UrlHelper::isExternal($url)) {
      return Url::fromUri($url);
    }
    return $url ? Url::fromUserInput($url) : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getVersion() {
    return $this->pluginDefinition['version'] ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function isEnabled() {
    return !!$this
      ->getSetting('enabled');
  }

  /**
   * {@inheritdoc}
   */
  public function isInstalled() : bool {
    return $this->pluginDefinition['installed'] ?? FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this->configuration['label'] ?: $this->pluginId;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = NestedArray::mergeDeep($this
      ->baseConfigurationDefaults(), $this
      ->defaultConfiguration(), $configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function getSettings() {
    return $this->configuration['settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function setSetting($name, $value = NULL) {
    if (isset($value)) {

      // Get the type of the exist value (if any).
      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]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setSettings(array $settings = []) {
    foreach ($settings as $name => $value) {
      $this
        ->setSetting($name, $value);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $element, FormStateInterface $formState, MarkdownFilterInterface $filter) {
    $definition = $this
      ->getPluginDefinition();
    $element['provider'] = [
      '#type' => 'value',
      '#value' => $definition['provider'],
    ];
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BaseExtension::baseConfigurationDefaults protected function Returns generic default configuration for markdown extension plugins.
BaseExtension::calculateDependencies public function
BaseExtension::defaultConfiguration public function
BaseExtension::defaultSettings public function Retrieves the default settings. Overrides MarkdownExtensionInterface::defaultSettings 3
BaseExtension::getConfiguration public function
BaseExtension::getDescription public function Retrieves the description of the plugin, if set. Overrides MarkdownInstallablePluginInterface::getDescription
BaseExtension::getLabel public function Displays the human-readable label of the plugin. Overrides MarkdownInstallablePluginInterface::getLabel
BaseExtension::getSetting public function Retrieves a setting. Overrides MarkdownExtensionInterface::getSetting
BaseExtension::getSettings public function Retrieves the current settings. Overrides MarkdownExtensionInterface::getSettings
BaseExtension::getUrl public function Retrieves the URL of the plugin, if set. Overrides MarkdownInstallablePluginInterface::getUrl
BaseExtension::getVersion public function The current version of the parser. Overrides MarkdownInstallablePluginInterface::getVersion
BaseExtension::installed public static function Indicates whether the parser is installed. Overrides MarkdownInstallablePluginInterface::installed
BaseExtension::isEnabled public function Indicates whether the extension is being used. Overrides MarkdownExtensionInterface::isEnabled
BaseExtension::isInstalled public function Indicates whether the parser is installed. Overrides MarkdownInstallablePluginInterface::isInstalled
BaseExtension::label public function
BaseExtension::setConfiguration public function
BaseExtension::setSetting public function Sets a specific setting. Overrides MarkdownExtensionInterface::setSetting
BaseExtension::setSettings public function Provides settings to an extension. Overrides MarkdownExtensionInterface::setSettings
BaseExtension::settingsForm public function Returns the configuration form elements specific to this plugin. Overrides MarkdownExtensionInterface::settingsForm 3
BaseExtension::version public static function Retrieves the version of the installed parser. Overrides MarkdownInstallablePluginInterface::version
BaseExtension::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MarkdownStatesTrait::getElementParents protected static function Retrieves the ancestry of the extension in a form/render array.
MarkdownStatesTrait::getSatesSelector protected static function Retrieves a states selector to use based on the form/render array parents.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.