You are here

protected function Tag::getFormats in Extensible BBCode 4.0.x

Same name and namespace in other branches
  1. 8.3 src/Entity/Tag.php \Drupal\xbbcode\Entity\Tag::getFormats()

Get all formats that use this tag.

Return value

\Drupal\filter\FilterFormatInterface[] An array of filter formats using a tag set where this tag is active.

1 call to Tag::getFormats()
Tag::filterFormatCacheTags in src/Entity/Tag.php
Get the cache tags of all text formats that use this BBCode tag.

File

src/Entity/Tag.php, line 216

Class

Tag
Represents a custom XBBCode tag that can be altered by administrators.

Namespace

Drupal\xbbcode\Entity

Code

protected function getFormats() : array {
  $formats = [];
  try {

    // Load all formats that use the BBCode filter.
    $storage = \Drupal::entityTypeManager()
      ->getStorage('filter_format');
    $ids = $storage
      ->getQuery()
      ->condition('filters.xbbcode.status', TRUE)
      ->execute();

    /** @var \Drupal\filter\FilterFormatInterface $format */
    foreach ($storage
      ->loadMultiple($ids) as $id => $format) {
      $config = $format
        ->filters('xbbcode')
        ->getConfiguration();
      $tag_set_id = $config['settings']['tags'];

      // If it references an existing tag set without this tag, skip.
      if ($tag_set_id) {

        /** @var \Drupal\xbbcode\Entity\TagSetInterface $tag_set */
        $tag_set = TagSet::load($tag_set_id);
        if ($tag_set !== NULL && !$tag_set
          ->hasTag($this
          ->id())) {
          continue;
        }
      }

      // Otherwise, include it.
      $formats[$id] = $format;
    }
  } catch (InvalidPluginDefinitionException|PluginNotFoundException $exception) {

    // A broken filter_format entity type is beyond this module to handle.
    watchdog_exception('filter', $exception);
  }
  return $formats;
}