class BlockListBuilder
Same name in other branches
- 9 core/modules/block/src/BlockListBuilder.php \Drupal\block\BlockListBuilder
- 10 core/modules/block/src/BlockListBuilder.php \Drupal\block\BlockListBuilder
- 11.x core/modules/block/src/BlockListBuilder.php \Drupal\block\BlockListBuilder
Defines a class to build a listing of block entities.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses \Drupal\Core\StringTranslation\StringTranslationTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait
- class \Drupal\Core\Entity\EntityListBuilder extends \Drupal\Core\Entity\EntityHandlerBase implements \Drupal\Core\Entity\EntityListBuilderInterface, \Drupal\Core\Entity\EntityHandlerInterface uses \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait
- class \Drupal\Core\Config\Entity\ConfigEntityListBuilder extends \Drupal\Core\Entity\EntityListBuilder
- class \Drupal\block\BlockListBuilder extends \Drupal\Core\Config\Entity\ConfigEntityListBuilder implements \Drupal\Core\Form\FormInterface
- class \Drupal\Core\Config\Entity\ConfigEntityListBuilder extends \Drupal\Core\Entity\EntityListBuilder
- class \Drupal\Core\Entity\EntityListBuilder extends \Drupal\Core\Entity\EntityHandlerBase implements \Drupal\Core\Entity\EntityListBuilderInterface, \Drupal\Core\Entity\EntityHandlerInterface uses \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait
Expanded class hierarchy of BlockListBuilder
See also
File
-
core/
modules/ block/ src/ BlockListBuilder.php, line 25
Namespace
Drupal\blockView source
class BlockListBuilder extends ConfigEntityListBuilder implements FormInterface {
/**
* The theme containing the blocks.
*
* @var string
*/
protected $theme;
/**
* The current request.
*
* @var \Symfony\Component\HttpFoundation\Request
*/
protected $request;
/**
* The theme manager.
*
* @var \Drupal\Core\Theme\ThemeManagerInterface
*/
protected $themeManager;
/**
* The form builder.
*
* @var \Drupal\Core\Form\FormBuilderInterface
*/
protected $formBuilder;
/**
* {@inheritdoc}
*/
protected $limit = FALSE;
/**
* The messenger.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Constructs a new BlockListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
* The theme manager.
* @param \Drupal\Core\Form\FormBuilderInterface $form_builder
* The form builder.
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*
* @param string|null $theme
* (optional) The theme to display the blocks for. If NULL, the current
* theme will be used.
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request.
*
* @return array
* The block list as a renderable array.
*/
public function render($theme = NULL, Request $request = NULL) {
$this->request = $request;
$this->theme = $theme;
return $this->formBuilder
->getForm($this);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'block_admin_display_form';
}
/**
* {@inheritdoc}
*/
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';
// Build the form tree.
$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;
}
/**
* Builds the main "Blocks" portion of the form.
*
* @return array
*/
protected function buildBlocksForm() {
// Build blocks first for each region.
$blocks = [];
$entities = $this->load();
/** @var \Drupal\block\BlockInterface[] $entities */
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',
],
];
// Weights range from -delta to +delta, so delta should be at least half
// of the amount of blocks present. This makes sure all blocks in the same
// region get an unique weight.
$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;
// Remove the block placement from the current request so that it is not
// passed on to any redirect destinations.
$this->request->query
->remove('block-placement');
}
// Loop over each region and build blocks.
$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'] = [
'#plain_text' => $info['status'] ? $info['label'] : $this->t('@label (disabled)', [
'@label' => $info['label'],
]),
'#wrapper_attributes' => [
'class' => [
'block',
],
],
];
$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']);
}
}
}
// Do not allow disabling the main system content block when it is present.
if (isset($form['system_main']['region'])) {
$form['system_main']['region']['#required'] = TRUE;
}
return $form;
}
/**
* Gets the name of the theme used for this block listing.
*
* @return string
* The name of the theme.
*/
protected function getThemeName() {
// If no theme was specified, use the current theme.
if (!$this->theme) {
$this->theme = $this->themeManager
->getActiveTheme()
->getName();
}
return $this->theme;
}
/**
* {@inheritdoc}
*/
protected function getEntityIds() {
return $this->getStorage()
->getQuery()
->condition('theme', $this->getThemeName())
->sort($this->entityType
->getKey('id'))
->execute();
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// No validation.
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$entities = $this->storage
->loadMultiple(array_keys($form_state->getValue('blocks')));
/** @var \Drupal\block\BlockInterface[] $entities */
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.'));
}
/**
* Wraps system_region_list().
*/
protected function systemRegionList($theme, $show = REGIONS_ALL) {
return system_region_list($theme, $show);
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
BlockListBuilder::$formBuilder | protected | property | The form builder. | |||
BlockListBuilder::$limit | protected | property | The number of entities to list per page, or FALSE to list all entities. | Overrides EntityListBuilder::$limit | ||
BlockListBuilder::$messenger | protected | property | The messenger. | Overrides MessengerTrait::$messenger | ||
BlockListBuilder::$request | protected | property | The current request. | |||
BlockListBuilder::$theme | protected | property | The theme containing the blocks. | |||
BlockListBuilder::$themeManager | protected | property | The theme manager. | |||
BlockListBuilder::buildBlocksForm | protected | function | Builds the main "Blocks" portion of the form. | |||
BlockListBuilder::buildForm | public | function | Form constructor. | Overrides FormInterface::buildForm | ||
BlockListBuilder::createInstance | public static | function | Instantiates a new instance of this entity handler. | Overrides EntityListBuilder::createInstance | ||
BlockListBuilder::getDefaultOperations | public | function | Gets this list's default operations. | Overrides ConfigEntityListBuilder::getDefaultOperations | ||
BlockListBuilder::getEntityIds | protected | function | Loads entity IDs using a pager sorted by the entity id. | Overrides EntityListBuilder::getEntityIds | ||
BlockListBuilder::getFormId | public | function | Returns a unique string identifying the form. | Overrides FormInterface::getFormId | ||
BlockListBuilder::getThemeName | protected | function | Gets the name of the theme used for this block listing. | |||
BlockListBuilder::render | public | function | Overrides EntityListBuilder::render | |||
BlockListBuilder::submitForm | public | function | Form submission handler. | Overrides FormInterface::submitForm | ||
BlockListBuilder::systemRegionList | protected | function | Wraps system_region_list(). | |||
BlockListBuilder::validateForm | public | function | Form validation handler. | Overrides FormInterface::validateForm | ||
BlockListBuilder::__construct | public | function | Constructs a new BlockListBuilder object. | Overrides EntityListBuilder::__construct | ||
ConfigEntityListBuilder::load | public | function | Loads entities of this type from storage for listing. | Overrides EntityListBuilder::load | 7 | |
DependencySerializationTrait::$_entityStorages | protected | property | An array of entity type IDs keyed by the property name of their storages. | |||
DependencySerializationTrait::$_serviceIds | protected | property | An array of service IDs keyed by property name used for serialization. | |||
DependencySerializationTrait::__sleep | public | function | 1 | |||
DependencySerializationTrait::__wakeup | public | function | 2 | |||
EntityHandlerBase::$moduleHandler | protected | property | The module handler to invoke hooks on. | 2 | ||
EntityHandlerBase::moduleHandler | protected | function | Gets the module handler. | 2 | ||
EntityHandlerBase::setModuleHandler | public | function | Sets the module handler for this handler. | |||
EntityListBuilder::$entityType | protected | property | Information about the entity type. | |||
EntityListBuilder::$entityTypeId | protected | property | The entity type ID. | |||
EntityListBuilder::$storage | protected | property | The entity storage class. | 1 | ||
EntityListBuilder::buildHeader | public | function | Builds the header row for the entity listing. | 26 | ||
EntityListBuilder::buildOperations | public | function | Builds a renderable list of operation links for the entity. | 2 | ||
EntityListBuilder::buildRow | public | function | Builds a row for an entity in the entity listing. | 26 | ||
EntityListBuilder::ensureDestination | protected | function | Ensures that a destination is present on the given URL. | |||
EntityListBuilder::getLabel | Deprecated | protected | function | Gets the label of an entity. | ||
EntityListBuilder::getOperations | public | function | Provides an array of information to build a list of operation links. | Overrides EntityListBuilderInterface::getOperations | 2 | |
EntityListBuilder::getStorage | public | function | Gets the entity storage. | Overrides EntityListBuilderInterface::getStorage | ||
EntityListBuilder::getTitle | protected | function | Gets the title of the page. | 1 | ||
MessengerTrait::messenger | public | function | Gets the messenger. | 17 | ||
MessengerTrait::setMessenger | public | function | Sets the messenger. | |||
RedirectDestinationTrait::$redirectDestination | protected | property | The redirect destination service. | 1 | ||
RedirectDestinationTrait::getDestinationArray | protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |||
RedirectDestinationTrait::getRedirectDestination | protected | function | Returns the redirect destination service. | |||
RedirectDestinationTrait::setRedirectDestination | public | function | Sets the redirect destination service. | |||
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | |||
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | |||
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | |||
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | |||
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 | ||
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.