View source
<?php
namespace Drupal\dashboards\Plugin\Dashboard;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\views\Views;
use Drupal\views\Entity\View;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\dashboards\Plugin\DashboardBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ViewEmbed extends DashboardBase {
protected $entityQuery;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CacheBackendInterface $cache_backend, QueryInterface $query) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $cache_backend);
$this->entityQuery = $query;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('dashboards.cache'), $container
->get('entity_type.manager')
->getStorage('view')
->getQuery());
}
public function buildRenderArray($configuration) : array {
$viewId = explode(':', $configuration['view']);
$view = Views::getView($viewId[0]);
if (!$view) {
return [
'#markup' => 'View do not exist anymore',
];
}
return views_embed_view($viewId[0], $viewId[1]);
}
public function validateForm(array $form, FormStateInterface $form_state, array $configuration) : void {
if (!$form_state
->getValue('view')) {
$form_state
->setErrorByName('view', $this
->t('Please provide a view.'));
}
}
public function buildSettingsForm(array $form, FormStateInterface $form_state, array $configuration) : array {
$views = $this->entityQuery
->condition('status', TRUE)
->condition("display.*.display_plugin", [
'embed',
], 'IN')
->execute();
$views = View::loadMultiple($views);
$options = [];
foreach ($views as $view) {
foreach ($view
->get('display') as $display) {
if ($display['display_plugin'] != 'embed') {
continue;
}
$options[$view
->id() . ':' . $display['id']] = $view
->label() . ': ' . $display['display_title'];
}
}
if (empty($options)) {
$form['view'] = [
'#markup' => $this
->t('Please add a embed view to use this plugin.'),
];
return $form;
}
$form['view'] = [
'#type' => 'select',
'#required' => TRUE,
'#options' => $options,
'#default_value' => isset($configuration['view']) ? $configuration['view'] : FALSE,
];
return $form;
}
}