View source
<?php
namespace Drupal\bootstrap_styles\Style;
use Drupal\Component\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\Markup;
abstract class StylePluginBase extends PluginBase implements StylePluginInterface {
use StringTranslationTrait;
const CONFIG = 'bootstrap_styles.settings';
protected $configFactory;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->configFactory = $config_factory;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'));
}
public function getTitle() {
return $this->pluginDefinition['title'];
}
public function config() {
return $this->configFactory
->getEditable(static::CONFIG);
}
public function getStyleOptions(string $name) {
$config = $this
->config();
$options = [];
$config_options = $config
->get($name);
$options = [
'_none' => $this
->t('N/A'),
];
$lines = explode(PHP_EOL, $config_options);
foreach ($lines as $line) {
$line = explode('|', $line);
if ($line && isset($line[0]) && isset($line[1])) {
$options[$line[0]] = $line[1] . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $line[1] . '</div>';
}
}
return $options;
}
public function getSvgIconMarkup(string $path) {
$svg = file_get_contents(DRUPAL_ROOT . '/' . $path);
$svg = preg_replace([
'/<\\?xml.*\\?>/i',
'/<!DOCTYPE((.|\\n|\\r)*?)">/i',
], '', $svg);
$svg = trim($svg);
return Markup::create($svg);
}
public function getStyleOptionClassByIndex(string $name, int $index) {
$class = '';
$options = $this
->getStyleOptions($name);
$count = 0;
foreach ($options as $key => $value) {
if ($count == $index) {
$class = $key;
break;
}
$count++;
}
return $class;
}
public function getStyleOptionIndexByClass(string $name, string $class) {
$index = 0;
$options = $this
->getStyleOptions($name);
$count = 0;
foreach ($options as $key => $value) {
if ($key == $class) {
$index = $count;
break;
}
$count++;
}
return $index;
}
public function getStyleOptionsCount(string $name) {
$count = count($this
->getStyleOptions($name)) - 1;
return $count;
}
public function addClassesToBuild(array $build, array $classes, $theme_wrapper = NULL) {
if ($theme_wrapper && isset($build['#theme_wrappers'][$theme_wrapper])) {
$build['#theme_wrappers'][$theme_wrapper]['#attributes']['class'] = array_merge($build['#theme_wrappers'][$theme_wrapper]['#attributes']['class'], $classes);
}
elseif (isset($build['#attributes']['class'])) {
$build['#attributes']['class'] = array_merge($build['#attributes']['class'], $classes);
}
else {
$build['#attributes']['class'] = $classes;
}
return $build;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function buildStyleFormElements(array &$form, FormStateInterface $form_state, $storage) {
return $form;
}
public function submitStyleFormElements(array $group_elements) {
}
public function build(array $build, array $storage, $theme_wrapper = NULL) {
return $build;
}
}