View source
<?php
namespace Drupal\config_pages\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\config_pages\Entity\ConfigPages;
use Drupal\config_pages\Entity\ConfigPagesType;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
class ConfigPagesBlock extends BlockBase implements BlockPluginInterface, ContainerFactoryPluginInterface {
private $entity_display_repository;
private $entity_type_manager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityDisplayRepositoryInterface $entity_display_repository, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
$this->entity_display_repository = $entity_display_repository;
$this->entity_type_manager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_display.repository'), $container
->get('entity_type.manager'));
}
public function getCacheTags() {
$config = $this
->getConfiguration();
if (!empty($config['config_page_type'])) {
$config_page = ConfigPages::config($config['config_page_type']);
return is_object($config_page) ? $config_page
->getCacheTags() : [];
}
return [];
}
public function build() {
$config = $this
->getConfiguration();
if (!empty($config['config_page_type'])) {
$config_page = ConfigPages::config($config['config_page_type']);
if (!is_object($config_page)) {
return [];
}
$view_mode = $config['config_page_view_mode'];
$build = $this->entity_type_manager
->getViewBuilder('config_pages')
->view($config_page, $view_mode, NULL);
$build['#contextual_links'] = [
'config_pages_type' => [
'route_parameters' => [
'config_pages_type' => $config['config_page_type'],
],
],
];
return $build;
}
return [];
}
public function defaultConfiguration() {
$settings = parent::defaultConfiguration();
if (isset($this->pluginDefinition['cache'])) {
$settings['cache'] = $this->pluginDefinition['cache'];
}
return $settings;
}
public function getMachineNameSuggestion() {
return 'config_pages';
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$config = $this
->getConfiguration();
$config_pages_types = ConfigPagesType::loadMultiple();
$options = [];
foreach ($config_pages_types as $cp_type) {
$id = $cp_type
->id();
$label = $cp_type
->label();
$options[$id] = $label;
}
$form['config_page_type'] = [
'#type' => 'select',
'#title' => $this
->t('Select ConfigPage type to show'),
'#options' => $options,
'#default_value' => isset($config['config_page_type']) ? $config['config_page_type'] : '',
];
$view_modes = $this->entity_display_repository
->getViewModes('config_pages');
$options = [];
foreach ($view_modes as $id => $view_mode) {
$options[$id] = $view_mode['label'];
}
$form['config_page_view_mode'] = [
'#type' => 'select',
'#title' => $this
->t('Select view mode for ConfigPage to show'),
'#options' => $options,
'#default_value' => isset($config['config_page_view_mode']) ? $config['config_page_view_mode'] : '',
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this
->setConfigurationValue('config_page_type', $form_state
->getValue('config_page_type'));
$this
->setConfigurationValue('config_page_view_mode', $form_state
->getValue('config_page_view_mode'));
}
}