You are here

public function XBBCodeFilter::tips in Extensible BBCode 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/Filter/XBBCodeFilter.php \Drupal\xbbcode\Plugin\Filter\XBBCodeFilter::tips()
  2. 4.0.x src/Plugin/Filter/XBBCodeFilter.php \Drupal\xbbcode\Plugin\Filter\XBBCodeFilter::tips()

Generates a filter's tip.

A filter's tips should be informative and to the point. Short tips are preferably one-liners.

@todo Split into getSummaryItem() and buildGuidelines().

Parameters

bool $long: Whether this callback should return a short tip to display in a form (FALSE), or whether a more elaborate filter tips should be returned for template_preprocess_filter_tips() (TRUE).

Return value

string|null Translated text to display as a tip, or NULL if this filter has no tip.

Overrides FilterBase::tips

File

src/Plugin/Filter/XBBCodeFilter.php, line 85
Contains Drupal\xbbcode\Plugin\Filter\XBBCodeFilter.

Class

XBBCodeFilter
Provides a filter that converts BBCode to HTML.

Namespace

Drupal\xbbcode\Plugin\Filter

Code

public function tips($long = FALSE) {
  if (!$this->tags) {
    return $this
      ->t('BBCode is enabled, but no tags are defined.');
  }
  if ($long) {
    $table = [
      '#type' => 'table',
      '#caption' => $this
        ->t('Allowed BBCode tags:'),
      '#header' => [
        $this
          ->t('Tag Description'),
        $this
          ->t('You Type'),
        $this
          ->t('You Get'),
      ],
    ];
    foreach ($this->tags as $name => $tag) {
      $table[$name] = [
        [
          '#markup' => "<strong>[{$name}]</strong><br />" . $tag->description,
          '#attributes' => [
            'class' => [
              'description',
            ],
          ],
        ],
        [
          '#markup' => '<code>' . str_replace("\n", '<br />', SafeMarkup::checkPlain($tag->sample)) . '</code>',
          '#attributes' => [
            'class' => [
              'type',
            ],
          ],
        ],
        [
          '#markup' => $this
            ->process($tag->sample, NULL)
            ->getProcessedText(),
          '#attributes' => [
            'class' => [
              'get',
            ],
          ],
        ],
      ];
    }
    return Drupal::service('renderer')
      ->render($table);
  }
  else {
    foreach ($this->tags as $name => $tag) {
      $tags[$name] = '<abbr title="' . $tag->description . '">[' . $name . ']</abbr>';
    }
    return $this
      ->t('You may use these tags: !tags', [
      '!tags' => implode(', ', $tags),
    ]);
  }
}