View source
<?php
namespace Drupal\block;
use Drupal\Component\Utility\Html;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class BlockListBuilder extends ConfigEntityListBuilder implements FormInterface {
protected $theme;
protected $request;
protected $themeManager;
protected $formBuilder;
protected $messenger;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, ThemeManagerInterface $theme_manager, FormBuilderInterface $form_builder, MessengerInterface $messenger) {
parent::__construct($entity_type, $storage);
$this->themeManager = $theme_manager;
$this->formBuilder = $form_builder;
$this->messenger = $messenger;
$this->limit = FALSE;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager')
->getStorage($entity_type
->id()), $container
->get('theme.manager'), $container
->get('form_builder'), $container
->get('messenger'));
}
public function render($theme = NULL, Request $request = NULL) {
$this->request = $request;
$this->theme = $theme;
return $this->formBuilder
->getForm($this);
}
public function getFormId() {
return 'block_admin_display_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#attached']['library'][] = 'core/drupal.tableheader';
$form['#attached']['library'][] = 'block/drupal.block';
$form['#attached']['library'][] = 'block/drupal.block.admin';
$form['#attributes']['class'][] = 'clearfix';
$form['blocks'] = $this
->buildBlocksForm();
$form['actions'] = [
'#tree' => FALSE,
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save blocks'),
'#button_type' => 'primary',
];
return $form;
}
protected function buildBlocksForm() {
$blocks = [];
$entities = $this
->load();
foreach ($entities as $entity_id => $entity) {
$definition = $entity
->getPlugin()
->getPluginDefinition();
$blocks[$entity
->getRegion()][$entity_id] = [
'label' => $entity
->label(),
'entity_id' => $entity_id,
'weight' => $entity
->getWeight(),
'entity' => $entity,
'category' => $definition['category'],
'status' => $entity
->status(),
];
}
$form = [
'#type' => 'table',
'#header' => [
$this
->t('Block'),
$this
->t('Category'),
$this
->t('Region'),
$this
->t('Weight'),
$this
->t('Operations'),
],
'#attributes' => [
'id' => 'blocks',
],
];
$weight_delta = round(count($entities) / 2);
$placement = FALSE;
if ($this->request->query
->has('block-placement')) {
$placement = $this->request->query
->get('block-placement');
$form['#attached']['drupalSettings']['blockPlacement'] = $placement;
$this->request->query
->remove('block-placement');
}
$regions = $this
->systemRegionList($this
->getThemeName(), REGIONS_VISIBLE);
foreach ($regions as $region => $title) {
$form['#tabledrag'][] = [
'action' => 'match',
'relationship' => 'sibling',
'group' => 'block-region-select',
'subgroup' => 'block-region-' . $region,
'hidden' => FALSE,
];
$form['#tabledrag'][] = [
'action' => 'order',
'relationship' => 'sibling',
'group' => 'block-weight',
'subgroup' => 'block-weight-' . $region,
];
$form['region-' . $region] = [
'#attributes' => [
'class' => [
'region-title',
'region-title-' . $region,
],
'no_striping' => TRUE,
],
];
$form['region-' . $region]['title'] = [
'#theme_wrappers' => [
'container' => [
'#attributes' => [
'class' => 'region-title__action',
],
],
],
'#prefix' => $title,
'#type' => 'link',
'#title' => $this
->t('Place block <span class="visually-hidden">in the %region region</span>', [
'%region' => $title,
]),
'#url' => Url::fromRoute('block.admin_library', [
'theme' => $this
->getThemeName(),
], [
'query' => [
'region' => $region,
],
]),
'#wrapper_attributes' => [
'colspan' => 5,
],
'#attributes' => [
'class' => [
'use-ajax',
'button',
'button--small',
],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
];
$form['region-' . $region . '-message'] = [
'#attributes' => [
'class' => [
'region-message',
'region-' . $region . '-message',
empty($blocks[$region]) ? 'region-empty' : 'region-populated',
],
],
];
$form['region-' . $region . '-message']['message'] = [
'#markup' => '<em>' . $this
->t('No blocks in this region') . '</em>',
'#wrapper_attributes' => [
'colspan' => 5,
],
];
if (isset($blocks[$region])) {
foreach ($blocks[$region] as $info) {
$entity_id = $info['entity_id'];
$form[$entity_id] = [
'#attributes' => [
'class' => [
'draggable',
],
],
];
$form[$entity_id]['#attributes']['class'][] = $info['status'] ? 'block-enabled' : 'block-disabled';
if ($placement && $placement == Html::getClass($entity_id)) {
$form[$entity_id]['#attributes']['class'][] = 'color-success';
$form[$entity_id]['#attributes']['class'][] = 'js-block-placed';
}
$form[$entity_id]['info'] = [
'#wrapper_attributes' => [
'class' => [
'block',
],
],
];
if ($info['status']) {
$form[$entity_id]['info']['#plain_text'] = $info['label'];
}
else {
$form[$entity_id]['info']['#markup'] = $this
->t('@label (disabled)', [
'@label' => $info['label'],
]);
}
$form[$entity_id]['type'] = [
'#markup' => $info['category'],
];
$form[$entity_id]['region-theme']['region'] = [
'#type' => 'select',
'#default_value' => $region,
'#required' => TRUE,
'#title' => $this
->t('Region for @block block', [
'@block' => $info['label'],
]),
'#title_display' => 'invisible',
'#options' => $regions,
'#attributes' => [
'class' => [
'block-region-select',
'block-region-' . $region,
],
],
'#parents' => [
'blocks',
$entity_id,
'region',
],
];
$form[$entity_id]['region-theme']['theme'] = [
'#type' => 'hidden',
'#value' => $this
->getThemeName(),
'#parents' => [
'blocks',
$entity_id,
'theme',
],
];
$form[$entity_id]['weight'] = [
'#type' => 'weight',
'#default_value' => $info['weight'],
'#delta' => $weight_delta,
'#title' => $this
->t('Weight for @block block', [
'@block' => $info['label'],
]),
'#title_display' => 'invisible',
'#attributes' => [
'class' => [
'block-weight',
'block-weight-' . $region,
],
],
];
$form[$entity_id]['operations'] = $this
->buildOperations($info['entity']);
}
}
}
if (isset($form['system_main']['region'])) {
$form['system_main']['region']['#required'] = TRUE;
}
return $form;
}
protected function getThemeName() {
if (!$this->theme) {
$this->theme = $this->themeManager
->getActiveTheme()
->getName();
}
return $this->theme;
}
protected function getEntityIds() {
return $this
->getStorage()
->getQuery()
->condition('theme', $this
->getThemeName())
->sort($this->entityType
->getKey('id'))
->execute();
}
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if (isset($operations['edit'])) {
$operations['edit']['title'] = $this
->t('Configure');
}
if (isset($operations['delete'])) {
$operations['delete']['title'] = $this
->t('Remove');
}
return $operations;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entities = $this->storage
->loadMultiple(array_keys($form_state
->getValue('blocks')));
foreach ($entities as $entity_id => $entity) {
$entity_values = $form_state
->getValue([
'blocks',
$entity_id,
]);
$entity
->setWeight($entity_values['weight']);
$entity
->setRegion($entity_values['region']);
$entity
->save();
}
$this->messenger
->addStatus($this
->t('The block settings have been updated.'));
}
protected function systemRegionList($theme, $show = REGIONS_ALL) {
return system_region_list($theme, $show);
}
}