public function LayoutChangeRegions::buildForm in Panels 8.4
Same name and namespace in other branches
- 8.3 src/Form/LayoutChangeRegions.php \Drupal\panels\Form\LayoutChangeRegions::buildForm()
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides FormInterface::buildForm
File
- src/
Form/ LayoutChangeRegions.php, line 63
Class
- LayoutChangeRegions
- Provides a form for mapping old regions into the regions of a new layout.
Namespace
Drupal\panels\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
/* @var $variant_plugin \Drupal\panels\Plugin\DisplayVariant\PanelsDisplayVariant */
$variant_plugin = $cached_values['plugin'];
$form['#attached']['library'][] = 'block/drupal.block';
$form['old_layout'] = [
'#title' => $this
->t('Old Layout'),
'#type' => 'select',
'#options' => $this->manager
->getLayoutOptions(),
'#default_value' => $cached_values['layout_change']['old_layout'],
'#disabled' => TRUE,
];
$form['new_layout'] = [
'#title' => $this
->t('New Layout'),
'#type' => 'select',
'#options' => $this->manager
->getLayoutOptions(),
'#default_value' => $cached_values['layout_change']['new_layout'],
'#disabled' => TRUE,
];
$layout_settings = !empty($cached_values['layout_change']['layout_settings']) ? $cached_values['layout_change']['layout_settings'] : [];
$old_layout = $this->manager
->createInstance($cached_values['layout_change']['old_layout'], []);
$new_layout = $this->manager
->createInstance($cached_values['layout_change']['new_layout'], $layout_settings);
if ($block_assignments = $variant_plugin
->getRegionAssignments()) {
// Build a table of all blocks used by this variant.
$form['blocks'] = [
'#type' => 'table',
'#header' => [
$this
->t('Label'),
$this
->t('ID'),
$this
->t('Region'),
$this
->t('Weight'),
],
'#attributes' => array(
'id' => 'blocks',
),
'#empty' => $this
->t('There are no regions for blocks.'),
];
// Loop through the blocks per region.
$new_regions = $new_layout
->getPluginDefinition()
->getRegionLabels();
$new_regions['__unassigned__'] = $this
->t('Unassigned');
$regions = [];
foreach ($old_layout
->getPluginDefinition()
->getRegions() as $region => $region_definition) {
if (empty($block_assignments[$region])) {
continue;
}
$label = $region_definition['label'];
// Prevent region names clashing with new regions.
$region_id = 'old_' . $region;
$new_region = isset($new_regions[$region]) ? $region : '__unassigned__';
$row['label']['#markup'] = $label;
$row['id']['#markup'] = $region;
// Allow the region to be changed for each block.
$row['region'] = [
'#title' => $this
->t('Region'),
'#title_display' => 'invisible',
'#type' => 'select',
'#options' => $new_regions,
'#default_value' => $new_region,
'#attributes' => [
'class' => [
'block-region-select',
'block-region-' . $new_region,
],
],
];
// Allow the weight to be changed for each region.
$row['weight'] = [
'#type' => 'weight',
'#default_value' => 0,
'#title' => $this
->t('Weight for @block block', [
'@block' => $label,
]),
'#title_display' => 'invisible',
'#attributes' => [
'class' => [
'block-weight',
'block-weight-' . $region,
],
],
];
$form['blocks'][$region_id] = $row;
$regions[$new_region][] = $region_id;
}
foreach ($new_regions as $region => $label) {
// Add a section for each region and allow blocks to be dragged between
// them.
$form['blocks']['region-' . $region] = [
'#attributes' => [
'class' => [
'region-title',
'region-title-' . $region,
],
'no_striping' => TRUE,
],
];
$form['blocks']['region-' . $region]['title'] = [
'#markup' => $label,
'#wrapper_attributes' => [
'colspan' => 4,
],
];
$form['blocks']['region-' . $region . '-message'] = [
'#attributes' => [
'class' => [
'region-message',
'region-' . $region . '-message',
empty($regions[$region]) ? 'region-empty' : 'region-populated',
],
],
];
if (empty($regions[$region])) {
$form['blocks']['region-' . $region . '-message']['message'] = [
'#markup' => '<em>' . $this
->t('No blocks in this region') . '</em>',
'#wrapper_attributes' => [
'colspan' => 4,
],
];
}
}
}
return $form;
}