You are here

public function FilterMarkdown::setConfiguration in Markdown 8.2

Sets the configuration for this plugin instance.

Parameters

array $configuration: An associative array containing the plugin's configuration.

Overrides FilterBase::setConfiguration

1 call to FilterMarkdown::setConfiguration()
FilterMarkdown::processSubform in src/Plugin/Filter/FilterMarkdown.php
Process callback for constructing markdown settings for this filter.

File

src/Plugin/Filter/FilterMarkdown.php, line 161

Class

FilterMarkdown
Provides a filter for Markdown.

Namespace

Drupal\markdown\Plugin\Filter

Code

public function setConfiguration(array $configuration) {

  // Normalize any passed filter format. This is needed in the event the
  // filter is being loaded from cached database configuration.
  // @see \Drupal\markdown\Plugin\Filter\Markdown::getConfiguration()
  // @see filter_formats()
  // @see markdown_filter_format_load()
  if (isset($configuration['filterFormat'])) {

    // Filter format is an entity, ensure configuration has an identifier.
    if ($configuration['filterFormat'] instanceof FilterFormat) {
      $this
        ->setFilterFormat($configuration['filterFormat']);
      $configuration['filterFormat'] = $configuration['filterFormat']
        ->id();
    }
    elseif (is_string($configuration['filterFormat']) && (!$this->filterFormat || $this->filterFormat
      ->id() !== $configuration['filterFormat'])) {
      if ($currentFilterFormat = drupal_static('markdown_filter_format_load')) {
        $filterFormat = $currentFilterFormat;
      }
      else {

        /** @var \Drupal\filter\Entity\FilterFormat $filterFormat */
        $filterFormat = FilterFormat::load($configuration['filterFormat']);
      }
      $this
        ->setFilterFormat($filterFormat);
    }
  }

  // The passed configuration is for the filter plugin.
  $configuration += [
    'settings' => [],
  ];

  // The settings of the filter plugin are the parser configuration.
  $parserConfiguration = $configuration['settings'];

  // Some older 8.x-2.x code used to have just the parser as a string.
  // @todo Remove after 8.x-2.0 release.
  if (isset($parserConfiguration['parser'])) {
    if (\is_string($parserConfiguration['parser'])) {
      $parserConfiguration['id'] = $parserConfiguration['parser'];
    }
    elseif (is_array($parserConfiguration['parser'])) {
      $parserConfiguration += $parserConfiguration['parser'];
    }
    unset($parserConfiguration['parser']);
  }
  $parserId = !empty($parserConfiguration['id']) ? $parserConfiguration['id'] : $this->parserManager
    ->getDefaultParser()
    ->getPluginId();

  // If the "override" setting for the filter isn't flagged, then it should
  // be using the site-wide parser configuration. Replace the configuration
  // so it only passes the render_strategy configuration to override any
  // site-wide configuration as that is still relevant to the filter.
  $override = !empty($parserConfiguration['override']);
  if (!$override) {
    $render_strategy = isset($parserConfiguration['render_strategy']) ? $parserConfiguration['render_strategy'] : [];
    $parserConfiguration = \Drupal::config("markdown.parser.{$parserId}")
      ->get() ?: [];
    $parserConfiguration['render_strategy'] = $render_strategy;
  }

  // Create a new parser based on the configuration being set.
  $parser = $this->parserManager
    ->createInstance($parserId, array_merge([
    'enabled' => TRUE,
  ], $parserConfiguration));
  $this
    ->setParser($parser);

  // Normalize the configuration settings from the parser itself.
  $parserConfiguration = array_merge([
    'override' => $override,
  ], $parser
    ->getConfiguration());

  // Remove settings and extension settings if not overridden.
  if (!($parserConfiguration['override'] = $override)) {
    unset($parserConfiguration['settings']);
    unset($parserConfiguration['extensions']);
  }

  // Remove any weight, not needed here.
  unset($parserConfiguration['weight']);

  // Remove dependencies, this is added above.
  // @see \Drupal\markdown\Plugin\Filter\Markdown::calculateDependencies()
  unset($parserConfiguration['dependencies']);

  // Replace filter settings with normalized parser configuration.
  $configuration['settings'] = $parserConfiguration;
  return parent::setConfiguration($configuration);
}