You are here

public function TagPluginCollection::getTable in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x src/TagPluginCollection.php \Drupal\xbbcode\TagPluginCollection::getTable()

Generate a table of available tags, with samples.

Return value

array A render element.

File

src/TagPluginCollection.php, line 115

Class

TagPluginCollection
A collection of tag plugins.

Namespace

Drupal\xbbcode

Code

public function getTable() : array {
  $table = [
    '#type' => 'table',
    '#caption' => $this
      ->t('Allowed BBCode tags:'),
    '#header' => [
      $this
        ->t('Tag Description'),
      $this
        ->t('You Type'),
      $this
        ->t('You Get'),
    ],
    '#empty' => $this
      ->t('BBCode is active, but no tags are available.'),
  ];
  foreach ($this as $name => $tag) {

    /** @var \Drupal\xbbcode\Plugin\TagPluginInterface $tag */
    $parser = new XBBCodeParser(static::createFromTags([
      $name => $tag,
    ]));
    $tree = $parser
      ->parse($tag
      ->getSample());
    $sample = $tree
      ->render();
    $attachments = [];
    foreach ($tree
      ->getRenderedChildren() as $child) {
      if ($child instanceof TagProcessResult) {
        $attachments = BubbleableMetadata::mergeAttachments($attachments, $child
          ->getAttachments());
      }
    }
    $table[$name] = [
      [
        '#type' => 'inline_template',
        '#template' => '<strong>[{{ tag.name }}]</strong><br /> {{ tag.description }}',
        '#context' => [
          'tag' => $tag,
        ],
        '#attributes' => [
          'class' => [
            'description',
          ],
        ],
      ],
      [
        '#type' => 'inline_template',
        '#template' => '<code>{{ tag.sample|nl2br }}</code>',
        '#context' => [
          'tag' => $tag,
        ],
        '#attributes' => [
          'class' => [
            'type',
          ],
        ],
      ],
      [
        '#markup' => Markup::create($sample),
        '#attached' => $attachments,
        '#attributes' => [
          'class' => [
            'get',
          ],
        ],
      ],
    ];
  }
  return $table;
}