VueBlock.php in Decoupled Blocks: Vue.js 8
File
src/Plugin/Block/VueBlock.php
View source
<?php
namespace Drupal\pdb_vue\Plugin\Block;
use Drupal\pdb\Plugin\Block\PdbBlock;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\pdb_vue\Render\VueMarkup;
class VueBlock extends PdbBlock implements ContainerFactoryPluginInterface {
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 build() {
$info = $this
->getComponentInfo();
$machine_name = $info['machine_name'];
$config = $this->configFactory
->get('pdb_vue.settings');
$template = '';
$build = parent::build();
if (isset($config) && $config
->get('use_spa') && isset($info['component']) && $info['component']) {
$props_string = $this
->buildPropertyString();
$build['#allowed_tags'] = [
$machine_name,
];
$build['#markup'] = '<' . $machine_name . $props_string . ' instance-id="' . $this->configuration['uuid'] . '"></' . $machine_name . '>';
}
else {
if (!empty($info['template'])) {
$template = file_get_contents($info['path'] . '/' . $info['template']);
}
$build['#markup'] = VueMarkup::create('<div class="' . $machine_name . '" id="' . $this->configuration['uuid'] . '">' . $template . '</div>');
}
return $build;
}
public function attachSettings(array $component) {
$attached = [];
$config = $this->configFactory
->get('pdb_vue.settings');
if (isset($config)) {
$attached['drupalSettings']['pdbVue']['developmentMode'] = $config
->get('development_mode');
if ($config
->get('use_spa')) {
$attached['drupalSettings']['pdbVue']['spaElement'] = $config
->get('spa_element');
}
}
else {
$attached['drupalSettings']['pdbVue']['developmentMode'] = FALSE;
}
return $attached;
}
public function attachLibraries(array $component) {
$config = $this->configFactory
->get('pdb_vue.settings');
$version = $config
->get('version') == 'vue3' ? 'vue3' : 'vue';
$libraries = parent::attachLibraries($component);
if (isset($component['libraries'])) {
if (empty($libraries)) {
$libraries[] = "pdb_vue/{$version}";
}
$libraries = array_merge($libraries, $component['libraries']);
}
if (isset($config) && $config
->get('use_spa') && isset($component['component']) && $component['component']) {
$libraries[] = "pdb_vue/{$version}.spa-init";
}
$library_attachments = [
'library' => $libraries,
];
return $library_attachments;
}
public function buildPropertyString() {
$props_string = '';
if (isset($this->configuration['pdb_configuration'])) {
$props = $this->configuration['pdb_configuration'];
foreach ($props as $field => $value) {
$bind = '';
if (is_numeric($value)) {
$bind = ':';
}
$field = $this
->convertKebabCase($field);
$props_string .= ' ' . $bind . $field . '="' . $value . '"';
}
}
return $props_string;
}
public function convertKebabCase($value) {
return ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '-$0', $value)), '-');
}
}
Classes
Name |
Description |
VueBlock |
Exposes a Vue component as a block. |