You are here

public function MarkdownParserManager::getFilter in Markdown 3.0.x

Retrieves the a filter plugin instance based on passed configuration.

Parameters

string $parser: The plugin identifier of the parser that should match against.

array $configuration: An array of configuration relevant to the plugin instance, passed by reference.

Return value

\Drupal\markdown\Plugin\Filter\MarkdownFilterInterface|null A MarkdownFilter instance or NULL if it could not be determined.

Overrides MarkdownParserManagerInterface::getFilter

1 call to MarkdownParserManager::getFilter()
MarkdownParserManager::createInstance in src/MarkdownParserManager.php

File

src/MarkdownParserManager.php, line 96

Class

MarkdownParserManager

Namespace

Drupal\markdown

Code

public function getFilter($parser = NULL, array &$configuration = []) {
  global $user;
  $parser = $this
    ->getFallbackPluginId($parser, $configuration);
  $filter = isset($configuration['filter']) ? $configuration['filter'] : NULL;
  $account = isset($configuration['account']) ? $configuration['account'] : NULL;
  unset($configuration['account']);
  if ($filter === NULL) {
    if ($account === NULL) {
      $account = (int) \Drupal::VERSION[0] >= 8 ? \Drupal::currentUser() : $user;
    }
    foreach (filter_formats($account) as $format) {
      $format_filter = FALSE;

      // Drupal 7.
      if (function_exists('filter_list_format')) {

        /** @var \stdClass $format */
        $filters = filter_list_format($format->format);
        if (isset($filters['markdown'])) {
          $format_filter = \Drupal::service('plugin.manager.filter')
            ->createInstance('markdown', (array) $filters['markdown']);
        }
      }
      else {
        $format_filter = $format
          ->filters()
          ->get('markdown');
      }

      // Skip formats that don't match the desired parser.
      if (!$format_filter || $format_filter->status || !$format_filter instanceof MarkdownFilterInterface || !$format_filter
        ->isEnabled() || $parser && $format_filter
        ->getSetting('parser') !== $parser) {
        continue;
      }
      $filter = $format_filter;
      break;
    }
  }
  elseif (is_string($filter)) {
    if ($account === NULL) {
      $account = (int) \Drupal::VERSION[0] >= 8 ? \Drupal::currentUser() : $user;
    }
    $formats = filter_formats($account);
    if (isset($formats[$filter])) {
      $filter = $formats[$filter]
        ->filters()
        ->get('markdown');
    }
    else {
      $filter = NULL;
    }
  }
  elseif ($filter instanceof FilterFormatInterface) {
    $filter = $filter
      ->filters()
      ->get('markdown');
  }
  if ($filter && !$filter instanceof MarkdownFilterInterface) {
    throw new \InvalidArgumentException($this
      ->t('Filter provided in configuration must be an instance of \\Drupal\\markdown\\Plugin\\Filter\\MarkdownFilterInterface a string representing a filter format or instance of \\Drupal\\filter\\FilterFormatInterface that contains a markdown filter.'));
  }

  // Now reset the filter.
  $configuration['filter'] = $filter;
  return $filter;
}