TagPluginBase.php in Extensible BBCode 8.3
File
src/Plugin/TagPluginBase.php
View source
<?php
namespace Drupal\xbbcode\Plugin;
use Drupal\Core\Plugin\PluginBase;
use Drupal\xbbcode\Parser\Tree\OutputElementInterface;
use Drupal\xbbcode\Parser\Tree\TagElementInterface;
use Drupal\xbbcode\PreparedTagElement;
use Drupal\xbbcode\TagProcessResult;
abstract class TagPluginBase extends PluginBase implements TagPluginInterface {
protected $status = FALSE;
protected $name;
protected $settings;
protected $sample;
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->name = $this->pluginDefinition['name'];
$this->settings = $this->pluginDefinition['settings'];
$this
->setConfiguration($configuration);
}
public function setConfiguration(array $configuration) : self {
if (isset($configuration['name'])) {
$this->name = $configuration['name'];
}
if (isset($configuration['settings'])) {
$this->settings = (array) $configuration['settings'];
}
return $this;
}
public function getConfiguration() : array {
return [
'id' => $this
->getPluginId(),
'name' => $this->name,
'settings' => $this->settings,
];
}
public function defaultConfiguration() : array {
return [
'name' => $this->pluginDefinition['name'],
'settings' => $this->pluginDefinition['settings'],
];
}
public function label() : string {
return $this->pluginDefinition['label'];
}
public function status() : bool {
return $this->status;
}
public function getDescription() : string {
return $this->pluginDefinition['description'];
}
public function getName() : string {
return $this->name;
}
public function getDefaultName() : string {
return $this->pluginDefinition['name'];
}
public function getDefaultSample() : string {
return $this->pluginDefinition['sample'];
}
public function getSample() : string {
if (!$this->sample) {
$this->sample = str_replace('{{ name }}', $this->name, trim($this
->getDefaultSample()));
}
return $this->sample;
}
public function prepare(string $content, TagElementInterface $tag) : string {
return $content;
}
public function process(TagElementInterface $tag) : OutputElementInterface {
$result = $this
->doProcess(new PreparedTagElement($tag));
foreach ($tag
->getRenderedChildren(FALSE) as $child) {
if ($child instanceof TagProcessResult) {
$result = $result
->merge($child);
}
}
return $result;
}
public abstract function doProcess(TagElementInterface $tag) : TagProcessResult;
}