You are here

public function ShortcodeService::getShortcodePlugins in Shortcode 8

Same name and namespace in other branches
  1. 2.0.x src/ShortcodeService.php \Drupal\shortcode\ShortcodeService::getShortcodePlugins()

Returns array of shortcode plugin definitions enabled for the filter.

Parameters

\Drupal\filter\Plugin\FilterInterface $filter: The filter. Defaults to NULL, where all shortcode plugins will be returned.

bool $reset: TRUE if the static cache should be reset. Defaults to FALSE.

Return value

array Array of shortcode plugin definitions, keyed by token, not id.

1 call to ShortcodeService::getShortcodePlugins()
ShortcodeService::process in src/ShortcodeService.php
Processes the Shortcodes according to the text and the text format.

File

src/ShortcodeService.php, line 96

Class

ShortcodeService
Provide the ShortCode service.

Namespace

Drupal\shortcode

Code

public function getShortcodePlugins(FilterInterface $filter = NULL, $reset = FALSE) {
  $shortcodes =& drupal_static(__FUNCTION__);

  // Prime plugin cache.
  if (!isset($shortcodes) || $reset) {
    $shortcodes_by_id = $this
      ->loadShortcodePlugins();

    // Sorting plugins by weight using uasort() costs quite a bit more than
    // what we're doing below.
    $definitions = [];
    foreach ($shortcodes_by_id as $shortcode) {
      $token = $shortcode['token'];

      // Only replace the definition if weight is smaller.
      if (!isset($definitions[$token]) || $definitions[$token]['weight'] < $shortcode['weight']) {
        $definitions[$token] = $shortcode;
      }
    }
    $shortcodes = [
      'all' => $shortcodes_by_id,
      'default' => $definitions,
    ];
  }

  // If filter is given, only return plugin definitions enabled on the filter.
  if ($filter) {
    $filter_id = $filter
      ->getPluginId();
    if (!isset($shortcodes[$filter_id])) {
      $settings = $filter->settings;
      $shortcodes_by_id = $shortcodes['all'];
      $enabled_shortcodes = [];
      foreach ($shortcodes_by_id as $shortcode_id => $shortcode) {
        if (isset($settings[$shortcode_id]) && $settings[$shortcode_id]) {
          $token = $shortcode['token'];

          // Only replace the definition if weight is smaller.
          if (!isset($enabled_shortcodes[$token]) || $enabled_shortcodes[$token]['weight'] < $shortcode['weight']) {
            $enabled_shortcodes[$token] = $shortcode;
          }
        }
      }
      $shortcodes[$filter_id] = $enabled_shortcodes;
    }
    return $shortcodes[$filter_id];
  }

  // Return all defined shortcode plugin definitions keyed by token.
  return $shortcodes['default'];
}