public function BlockListingForm::buildForm in Layout Builder Browser 8
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/ BlockListingForm.php, line 82
Class
- BlockListingForm
- Builds a listing of block entities.
Namespace
Drupal\layout_builder_browser\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$categories = $this
->loadCategories();
$form['categories'] = [
'#type' => 'table',
'#header' => $this
->buildHeader(),
'#empty' => $this
->t('No block categories defined.'),
'#attributes' => [
'id' => 'blocks',
],
];
foreach ($categories as $category) {
$category_id = $category["category"]->id;
$form['categories'][$category_id] = $this
->buildBlockCategoryRow($category["category"]);
$form['categories']['#tabledrag'][] = [
'action' => 'order',
'relationship' => 'sibling',
'group' => 'block-weight',
'subgroup' => 'block-weight-' . $category_id,
];
$form['categories']['region-' . $category_id . '-message'] = [
'#attributes' => [
'class' => [
'region-message',
'region-' . $category_id . '-message',
empty($category['blocks']) ? 'region-empty' : 'region-populated',
],
],
];
$form['categories']['region-' . $category_id . '-message']['message'] = [
'#markup' => '<em>' . $this
->t('No blocks in this category') . '</em>',
'#wrapper_attributes' => [
'colspan' => 5,
],
];
foreach ($category['blocks'] as $block) {
$block['category'] = $category_id;
$form['categories'][$block['id']] = $this
->buildBlockRow($block);
}
}
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
];
$form['#attached']['library'][] = 'layout_builder_browser/admin';
$form['#attached']['library'][] = 'block/drupal.block';
$form['#attached']['library'][] = 'block/drupal.block.admin';
return $form;
}