public function BlockListController::buildForm

Implements \Drupal\Core\Form\FormInterface::buildForm().

Form constructor for the main block administration form.

Overrides FormInterface::buildForm

File

drupal/core/modules/block/lib/Drupal/block/BlockListController.php, line 107
Contains \Drupal\block\BlockListController.

Class

BlockListController
Defines the block list controller.

Namespace

Drupal\block

Code

public function buildForm(array $form, array &$form_state) {
  $entities = $this
    ->load();
  $form['#attached']['css'][] = drupal_get_path('module', 'block') . '/css/block.admin.css';
  $form['#attached']['library'][] = array(
    'system',
    'drupal.tableheader',
  );
  $form['#attached']['library'][] = array(
    'block',
    'drupal.block',
  );

  // Add a last region for disabled blocks.
  $block_regions_with_disabled = $this->regions + array(
    BLOCK_REGION_NONE => BLOCK_REGION_NONE,
  );
  $form['block_regions'] = array(
    '#type' => 'value',
    '#value' => $block_regions_with_disabled,
  );

  // 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);

  // Build the form tree.
  $form['edited_theme'] = array(
    '#type' => 'value',
    '#value' => $this->theme,
  );
  $form['blocks'] = array(
    '#type' => 'table',
    '#header' => array(
      t('Block'),
      t('Region'),
      t('Weight'),
      t('Operations'),
    ),
    '#attributes' => array(
      'id' => 'blocks',
    ),
  );

  // Build blocks first for each region.
  foreach ($entities as $entity_id => $entity) {
    $definition = $entity
      ->getPlugin()
      ->getPluginDefinition();
    $blocks[$entity
      ->get('region')][$entity_id] = array(
      'admin_label' => $definition['admin_label'],
      'entity_id' => $entity_id,
      'weight' => $entity
        ->get('weight'),
    );
  }

  // Loop over each region and build blocks.
  foreach ($block_regions_with_disabled as $region => $title) {
    $form['blocks']['#tabledrag'][] = array(
      'match',
      'sibling',
      'block-region-select',
      'block-region-' . $region,
      NULL,
      FALSE,
    );
    $form['blocks']['#tabledrag'][] = array(
      'order',
      'sibling',
      'block-weight',
      'block-weight-' . $region,
    );
    $form['blocks'][$region] = array(
      '#attributes' => array(
        'class' => array(
          'region-title',
          'region-title-' . $region,
          'odd',
        ),
        'no_striping' => TRUE,
      ),
    );
    $form['blocks'][$region]['title'] = array(
      '#markup' => $region != BLOCK_REGION_NONE ? $title : t('Disabled'),
      '#wrapper_attributes' => array(
        'colspan' => 5,
      ),
    );
    $form['blocks'][$region . '-message'] = array(
      '#attributes' => array(
        'class' => array(
          'region-message',
          'region-' . $region . '-message',
          empty($blocks[$region]) ? 'region-empty' : 'region-populated',
        ),
      ),
    );
    $form['blocks'][$region . '-message']['message'] = array(
      '#markup' => '<em>' . 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['blocks'][$entity_id] = array(
          '#attributes' => array(
            'class' => array(
              'draggable',
            ),
          ),
        );
        $form['blocks'][$entity_id]['info'] = array(
          '#markup' => check_plain($info['admin_label']),
          '#wrapper_attributes' => array(
            'class' => array(
              'block',
            ),
          ),
        );
        $form['blocks'][$entity_id]['region-theme']['region'] = array(
          '#type' => 'select',
          '#default_value' => $region,
          '#empty_value' => BLOCK_REGION_NONE,
          '#title_display' => 'invisible',
          '#title' => t('Region for @block block', array(
            '@block' => $info['admin_label'],
          )),
          '#options' => $this->regions,
          '#attributes' => array(
            'class' => array(
              'block-region-select',
              'block-region-' . $region,
            ),
          ),
          '#parents' => array(
            'blocks',
            $entity_id,
            'region',
          ),
        );
        $form['blocks'][$entity_id]['region-theme']['theme'] = array(
          '#type' => 'hidden',
          '#value' => $this->theme,
          '#parents' => array(
            'blocks',
            $entity_id,
            'theme',
          ),
        );
        $form['blocks'][$entity_id]['weight'] = array(
          '#type' => 'weight',
          '#default_value' => $info['weight'],
          '#delta' => $weight_delta,
          '#title_display' => 'invisible',
          '#title' => t('Weight for @block block', array(
            '@block' => $info['admin_label'],
          )),
          '#attributes' => array(
            'class' => array(
              'block-weight',
              'block-weight-' . $region,
            ),
          ),
        );
        $links['configure'] = array(
          'title' => t('configure'),
          'href' => 'admin/structure/block/manage/' . $entity_id . '/configure',
        );
        $links['delete'] = array(
          'title' => t('delete'),
          'href' => 'admin/structure/block/manage/' . $entity_id . '/delete',
        );
        $form['blocks'][$entity_id]['operations'] = array(
          '#type' => 'operations',
          '#links' => $links,
        );
      }
    }
  }

  // Do not allow disabling the main system content block when it is present.
  if (isset($form['blocks']['system_main']['region'])) {
    $form['blocks']['system_main']['region']['#required'] = TRUE;
  }
  $form['actions'] = array(
    '#tree' => FALSE,
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save blocks'),
    '#button_type' => 'primary',
  );
  return $form;
}