ImageTagPlugin.php in Extensible BBCode 8.3
File
standard/src/Plugin/XBBCode/ImageTagPlugin.php
View source
<?php
namespace Drupal\xbbcode_standard\Plugin\XBBCode;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\xbbcode\Parser\Tree\TagElementInterface;
use Drupal\xbbcode\Plugin\RenderTagPlugin;
use function count;
class ImageTagPlugin extends RenderTagPlugin {
public function getDefaultSample() : string {
return $this
->t('[{{ name }} width=57 height=66]@url[/{{ name }}]', [
'@url' => Url::fromUri('base:core/themes/bartik/logo.svg')
->toString(),
]);
}
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,
],
];
}
}