View source
<?php
namespace Drupal\views\Plugin\views\display;
use Drupal\Core\Url;
use Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\Block\ViewsBlock;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Block extends DisplayPluginBase {
protected $usesAttachments = TRUE;
protected $entityTypeManager;
protected $blockManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, BlockManagerInterface $block_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->blockManager = $block_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('plugin.manager.block'));
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['block_description'] = [
'default' => '',
];
$options['block_category'] = [
'default' => $this
->t('Lists (Views)'),
];
$options['block_hide_empty'] = [
'default' => FALSE,
];
$options['allow'] = [
'contains' => [
'items_per_page' => [
'default' => 'items_per_page',
],
],
];
return $options;
}
public function blockSettings(array $settings) {
$settings['items_per_page'] = 'none';
return $settings;
}
public function execute() {
$element = $this->view
->render();
if ($this
->outputIsEmpty() && $this
->getOption('block_hide_empty') && empty($this->view->style_plugin->definition['even empty'])) {
return [];
}
else {
return $element;
}
}
public function optionsSummary(&$categories, &$options) {
parent::optionsSummary($categories, $options);
$categories['block'] = [
'title' => $this
->t('Block settings'),
'column' => 'second',
'build' => [
'#weight' => -10,
],
];
$block_description = strip_tags($this
->getOption('block_description'));
if (empty($block_description)) {
$block_description = $this
->t('None');
}
$block_category = $this
->getOption('block_category');
$options['block_description'] = [
'category' => 'block',
'title' => $this
->t('Block name'),
'value' => views_ui_truncate($block_description, 24),
];
$options['block_category'] = [
'category' => 'block',
'title' => $this
->t('Block category'),
'value' => views_ui_truncate($block_category, 24),
];
$filtered_allow = array_filter($this
->getOption('allow'));
$options['allow'] = [
'category' => 'block',
'title' => $this
->t('Allow settings'),
'value' => empty($filtered_allow) ? $this
->t('None') : $this
->t('Items per page'),
];
$options['block_hide_empty'] = [
'category' => 'other',
'title' => $this
->t('Hide block if the view output is empty'),
'value' => $this
->getOption('block_hide_empty') ? $this
->t('Yes') : $this
->t('No'),
];
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
switch ($form_state
->get('section')) {
case 'block_description':
$form['#title'] .= $this
->t('Block admin description');
$form['block_description'] = [
'#type' => 'textfield',
'#description' => $this
->t('This will appear as the name of this block in administer >> structure >> blocks.'),
'#default_value' => $this
->getOption('block_description'),
];
break;
case 'block_category':
$form['#title'] .= $this
->t('Block category');
$form['block_category'] = [
'#type' => 'textfield',
'#autocomplete_route_name' => 'block.category_autocomplete',
'#description' => $this
->t('The category this block will appear under on the <a href=":href">blocks placement page</a>.', [
':href' => Url::fromRoute('block.admin_display')
->toString(),
]),
'#default_value' => $this
->getOption('block_category'),
];
break;
case 'block_hide_empty':
$form['#title'] .= $this
->t('Block empty settings');
$form['block_hide_empty'] = [
'#title' => $this
->t('Hide block if no result/empty text'),
'#type' => 'checkbox',
'#description' => $this
->t('Hide the block if there is no result and no empty text and no header/footer which is shown on empty result'),
'#default_value' => $this
->getOption('block_hide_empty'),
];
break;
case 'exposed_form_options':
$this->view
->initHandlers();
if (!$this
->usesExposed() && parent::usesExposed()) {
$form['exposed_form_options']['warning'] = [
'#weight' => -10,
'#markup' => '<div class="messages messages--warning">' . $this
->t('Exposed filters in block displays require "Use AJAX" to be set to work correctly.') . '</div>',
];
}
break;
case 'allow':
$form['#title'] .= $this
->t('Allow settings in the block configuration');
$options = [
'items_per_page' => $this
->t('Items per page'),
];
$allow = array_keys(array_filter($this
->getOption('allow')));
$form['allow'] = [
'#type' => 'checkboxes',
'#default_value' => $allow,
'#options' => $options,
];
break;
}
}
public function submitOptionsForm(&$form, FormStateInterface $form_state) {
parent::submitOptionsForm($form, $form_state);
$section = $form_state
->get('section');
switch ($section) {
case 'block_description':
case 'block_category':
case 'allow':
case 'block_hide_empty':
$this
->setOption($section, $form_state
->getValue($section));
break;
}
}
public function blockForm(ViewsBlock $block, array &$form, FormStateInterface $form_state) {
$allow_settings = array_filter($this
->getOption('allow'));
$block_configuration = $block
->getConfiguration();
foreach ($allow_settings as $type => $enabled) {
if (empty($enabled)) {
continue;
}
switch ($type) {
case 'items_per_page':
$form['override']['items_per_page'] = [
'#type' => 'select',
'#title' => $this
->t('Items per block'),
'#options' => [
'none' => $this
->t('@count (default setting)', [
'@count' => $this
->getPlugin('pager')
->getItemsPerPage(),
]),
1 => 1,
2 => 2,
3 => 3,
4 => 4,
5 => 5,
6 => 6,
10 => 10,
12 => 12,
20 => 20,
24 => 24,
40 => 40,
48 => 48,
],
'#default_value' => $block_configuration['items_per_page'],
];
break;
}
}
return $form;
}
public function blockValidate(ViewsBlock $block, array $form, FormStateInterface $form_state) {
}
public function blockSubmit(ViewsBlock $block, $form, FormStateInterface $form_state) {
if ($items_per_page = $form_state
->getValue([
'override',
'items_per_page',
])) {
$block
->setConfigurationValue('items_per_page', $items_per_page);
}
$form_state
->unsetValue([
'override',
'items_per_page',
]);
}
public function preBlockBuild(ViewsBlock $block) {
$config = $block
->getConfiguration();
if ($config['items_per_page'] !== 'none') {
$this->view
->setItemsPerPage($config['items_per_page']);
}
}
public function usesExposedFormInBlock() {
return TRUE;
}
public function remove() {
parent::remove();
if ($this->entityTypeManager
->hasDefinition('block')) {
$plugin_id = 'views_block:' . $this->view->storage
->id() . '-' . $this->display['id'];
foreach ($this->entityTypeManager
->getStorage('block')
->loadByProperties([
'plugin' => $plugin_id,
]) as $block) {
$block
->delete();
}
}
if ($this->blockManager instanceof CachedDiscoveryInterface) {
$this->blockManager
->clearCachedDefinitions();
}
}
}