You are here

public function ImageTagPlugin::buildElement in Extensible BBCode 8.3

Same name and namespace in other branches
  1. 4.0.x standard/src/Plugin/XBBCode/ImageTagPlugin.php \Drupal\xbbcode_standard\Plugin\XBBCode\ImageTagPlugin::buildElement()

Build a render array from the tag.

Parameters

\Drupal\xbbcode\Parser\Tree\TagElementInterface $tag: The tag element in the parse tree.

Return value

array The render array.

Overrides RenderTagPlugin::buildElement

File

standard/src/Plugin/XBBCode/ImageTagPlugin.php, line 37

Class

ImageTagPlugin
Inserts an image.

Namespace

Drupal\xbbcode_standard\Plugin\XBBCode

Code

public function buildElement(TagElementInterface $tag) : array {
  $style = [];
  $dimensions = explode('x', $tag
    ->getOption());
  if (count($dimensions) === 2) {
    [
      $width,
      $height,
    ] = $dimensions;
  }
  else {
    $width = (string) $tag
      ->getAttribute('width');
    $height = (string) $tag
      ->getAttribute('height');
  }
  if (is_numeric($width)) {
    $style[] = "width:{$width}px";
  }
  if (is_numeric($height)) {
    $style[] = "height:{$height}px";
  }
  $src = Html::decodeEntities($tag
    ->getContent());
  return [
    '#type' => 'inline_template',
    '#template' => '<img src="{{ src }}" alt="{{ src }}" style="{{ style }};" />',
    '#context' => [
      'tag' => $tag,
      'style' => implode(';', $style),
      'src' => $src,
    ],
  ];
}