View source
<?php
namespace Drupal\dashboards\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DashboardBlock extends BlockBase implements ContainerFactoryPluginInterface, BlockPluginInterface {
protected $basePlugin;
public function __construct(array $configuration, $plugin_id, $plugin_definition, PluginManagerInterface $plugin_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$id = explode(':', $this
->getDerivativeId())[1];
$this->basePlugin = $plugin_manager
->createInstance($id);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.dashboard'));
}
public function blockValidate($form, FormStateInterface $form_state) {
$this->basePlugin
->validateForm($form, $form_state, $this
->getConfiguration());
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
return $this->basePlugin
->buildSettingsForm($form, $form_state, $this
->getConfiguration());
}
public function blockSubmit($form, FormStateInterface $form_state) {
$configuration = $this
->getConfiguration();
parent::blockSubmit($form, $form_state);
$this->basePlugin
->massageFormValues($form, $form_state, $configuration);
$values = $form_state
->getValues();
$this->configuration = array_merge($configuration, $values);
}
public function build() {
return [
'#type' => 'container',
'#attached' => [
'library' => [
'dashboards/component',
],
],
'#attributes' => [
'class' => [
'dashboard-component',
],
],
'content' => $this->basePlugin
->buildRenderArray($this
->getConfiguration()),
];
}
}