BlockListBuilder::buildBlocksForm

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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;
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.