protected BlockListBuilder::buildBlocksForm()
Builds the main "Blocks" portion of the form.
Return value
array
File
- core/modules/block/src/BlockListBuilder.php, line 146
Class
- BlockListBuilder
- Defines a class to build a listing of block entities.
Namespace
Drupal\block
Code
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] = array(
'label' => $entity->label(),
'entity_id' => $entity_id,
'weight' => $entity->getWeight(),
'entity' => $entity,
'category' => $definition['category'],
);
}
$form = array(
'#type' => 'table',
'#header' => array(
$this->t('Block'),
$this->t('Category'),
$this->t('Region'),
$this->t('Weight'),
$this->t('Operations'),
),
'#attributes' => array(
'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;
}
// Loop over each region and build blocks.
$regions = $this->systemRegionList($this->getThemeName(), REGIONS_VISIBLE);
$block_regions_with_disabled = $regions + array(BlockInterface::BLOCK_REGION_NONE => $this->t('Disabled', array(), array('context' => 'Plural')));
foreach ($block_regions_with_disabled as $region => $title) {
$form['#tabledrag'][] = array(
'action' => 'match',
'relationship' => 'sibling',
'group' => 'block-region-select',
'subgroup' => 'block-region-' . $region,
'hidden' => FALSE,
);
$form['#tabledrag'][] = array(
'action' => 'order',
'relationship' => 'sibling',
'group' => 'block-weight',
'subgroup' => 'block-weight-' . $region,
);
$form['region-' . $region] = array(
'#attributes' => array(
'class' => array('region-title', 'region-title-' . $region),
'no_striping' => TRUE,
),
);
$form['region-' . $region]['title'] = array(
'#theme_wrappers' => array(
'container' => array(
'#attributes' => array('class' => 'region-title__action'),
)
),
'#prefix' => $region != BlockInterface::BLOCK_REGION_NONE ? $title : $block_regions_with_disabled[$region],
'#type' => 'link',
'#title' => $this->t('Place block <span class="visually-hidden">in the %region region</span>', ['%region' => $block_regions_with_disabled[$region]]),
'#url' => Url::fromRoute('block.admin_library', ['theme' => $this->getThemeName()], ['query' => ['region' => $region]]),
'#wrapper_attributes' => array(
'colspan' => 5,
),
'#attributes' => [
'class' => ['use-ajax', 'button', 'button--small'],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
]),
],
);
$form['region-' . $region . '-message'] = array(
'#attributes' => array(
'class' => array(
'region-message',
'region-' . $region . '-message',
empty($blocks[$region]) ? 'region-empty' : 'region-populated',
),
),
);
$form['region-' . $region . '-message']['message'] = array(
'#markup' => '<em>' . $this->t('No blocks in this region') . '</em>',
'#wrapper_attributes' => array(
'colspan' => 5,
),
);
if (isset($blocks[$region])) {
foreach ($blocks[$region] as $info) {
$entity_id = $info['entity_id'];
$form[$entity_id] = array(
'#attributes' => array(
'class' => array('draggable'),
),
);
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'] = array(
'#plain_text' => $info['label'],
'#wrapper_attributes' => array(
'class' => array('block'),
),
);
$form[$entity_id]['type'] = array(
'#markup' => $info['category'],
);
$form[$entity_id]['region-theme']['region'] = array(
'#type' => 'select',
'#default_value' => $region,
'#empty_value' => BlockInterface::BLOCK_REGION_NONE,
'#title' => $this->t('Region for @block block', array('@block' => $info['label'])),
'#title_display' => 'invisible',
'#options' => $regions,
'#attributes' => array(
'class' => array('block-region-select', 'block-region-' . $region),
),
'#parents' => array('blocks', $entity_id, 'region'),
);
$form[$entity_id]['region-theme']['theme'] = array(
'#type' => 'hidden',
'#value' => $this->getThemeName(),
'#parents' => array('blocks', $entity_id, 'theme'),
);
$form[$entity_id]['weight'] = array(
'#type' => 'weight',
'#default_value' => $info['weight'],
'#delta' => $weight_delta,
'#title' => $this->t('Weight for @block block', array('@block' => $info['label'])),
'#title_display' => 'invisible',
'#attributes' => array(
'class' => array('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;
}
Please login to continue.