diff --git a/core/modules/block/block.admin.inc b/core/modules/block/block.admin.inc index 6019d34..c3955fc 100644 --- a/core/modules/block/block.admin.inc +++ b/core/modules/block/block.admin.inc @@ -20,215 +20,16 @@ function block_admin_demo($theme = NULL) { /** * Page callback: Shows the block administration page. * - * @param $theme - * The theme to display the administration page for. If not provided, defaults - * to the currently used theme. - * - * @see block_menu() - */ -function block_admin_display($theme = NULL) { - global $theme_key; - - drupal_theme_initialize(); - - if (!isset($theme)) { - // If theme is not specifically set, rehash for the current theme. - $theme = $theme_key; - } - - // Fetch and sort blocks. - $blocks = block_admin_display_prepare_blocks($theme); - - return drupal_get_form('block_admin_display_form', $blocks, $theme); -} - -/** - * Prepares a list of blocks for display on the blocks administration page. - * - * @param $theme - * The machine-readable name of the theme whose blocks should be returned. - * - * @return - * An array of blocks, as returned by _block_rehash(), sorted by region in - * preparation for display on the blocks administration page. - * - * @see block_admin_display_form() - */ -function block_admin_display_prepare_blocks($theme) { - $blocks = _block_rehash($theme); - $compare_theme = &drupal_static('_block_compare:theme'); - $compare_theme = $theme; - uasort($blocks, '_block_compare'); - return $blocks; -} - -/** - * Form constructor for the main block administration form. - * - * @param $blocks - * An array of blocks, as returned by block_admin_display_prepare_blocks(). - * @param $theme - * A string representing the name of the theme to edit blocks for. - * @param $block_regions - * (optional) An array of regions in which the blocks will be allowed to be - * placed. Defaults to all visible regions for the theme whose blocks are - * being configured. In all cases, a dummy region for disabled blocks will - * also be displayed. - * - * @return - * An array representing the form definition. - * - * @ingroup forms - * @see block_admin_display_form_submit() - */ -function block_admin_display_form($form, &$form_state, $blocks, $theme, $block_regions = NULL) { - $path = drupal_get_path('module', 'block'); - $form['#attached']['css'][] = $path . '/block.admin.css'; - $form['#attached']['library'][] = array('system', 'drupal.tableheader'); - $form['#attached']['library'][] = array('block', 'drupal.block'); - - // Get a list of block regions if one was not provided. - if (!isset($block_regions)) { - $block_regions = system_region_list($theme, REGIONS_VISIBLE); - } - // Add a last region for disabled blocks. - $block_regions_with_disabled = $block_regions + array(BLOCK_REGION_NONE => BLOCK_REGION_NONE); - - foreach ($block_regions_with_disabled as $region => $title) { - $form['#attached']['drupal_add_tabledrag'][] = array('blocks', 'match', 'sibling', 'block-region-select', 'block-region-' . $region, NULL, FALSE); - $form['#attached']['drupal_add_tabledrag'][] = array('blocks', 'order', 'sibling', 'block-weight', 'block-weight-' . $region); - } - - // 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($blocks) / 2); - - // Build the form tree. - $form['edited_theme'] = array( - '#type' => 'value', - '#value' => $theme, - ); - $form['block_regions'] = array( - '#type' => 'value', - '#value' => $block_regions_with_disabled, - ); - $form['blocks'] = array(); - $form['#tree'] = TRUE; - - foreach ($blocks as $key => $block) { - $info = $block->getPlugin()->getDefinition(); - $form['blocks'][$key]['info'] = array( - '#markup' => check_plain($info['subject']), - ); - $form['blocks'][$key]['theme'] = array( - '#type' => 'hidden', - '#value' => $theme, - ); - $form['blocks'][$key]['weight'] = array( - '#type' => 'weight', - '#default_value' => $block->get('weight'), - '#delta' => $weight_delta, - '#title_display' => 'invisible', - '#title' => t('Weight for @block block', array('@block' => $info['subject'])), - ); - $form['blocks'][$key]['region'] = array( - '#type' => 'select', - '#default_value' => $block->get('region') != BLOCK_REGION_NONE ? $block->get('region') : NULL, - '#empty_value' => BLOCK_REGION_NONE, - '#title_display' => 'invisible', - '#title' => t('Region for @block block', array('@block' => $info['subject'])), - '#options' => $block_regions, - ); - $links['configure'] = array( - 'title' => t('configure'), - 'href' => 'admin/structure/block/manage/' . $key . '/configure', - ); - $links['delete'] = array( - 'title' => t('delete'), - 'href' => 'admin/structure/block/manage/' . $key . '/delete', - ); - $form['blocks'][$key]['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; -} - -/** - * Form submission handler for block_admin_display_form(). + * @param string $theme + * The theme to display the administration page for. * - * @see block_admin_display_form() - */ -function block_admin_display_form_submit($form, &$form_state) { - $blocks = entity_load_multiple('block', array_keys($form_state['values']['blocks'])); - foreach ($blocks as $block_id => $block) { - $block->set('weight', $form_state['values']['blocks'][$block_id]['weight']); - $block->set('region', $form_state['values']['blocks'][$block_id]['region']); - $block->save(); - } - drupal_set_message(t('The block settings have been updated.')); - cache_invalidate_tags(array('content' => TRUE)); -} - -/** - * Sorts active blocks by region, then by weight; sorts inactive blocks by name. + * @return array + * A render array for a page containing a list of blocks. * - * Callback for usort() in block_admin_display_prepare_blocks(). + * @see block_menu() */ -function _block_compare(Block $a, Block $b) { - global $theme_key; - - // Theme should be set before calling this function, or the current theme - // is being used. - $theme = &drupal_static(__FUNCTION__ . ':theme'); - if (!isset($theme)) { - $theme = $theme_key; - } - - $regions = &drupal_static(__FUNCTION__ . ':regions'); - // We need the region list to correctly order by region. - if (!isset($regions)) { - $regions = array_flip(array_keys(system_region_list($theme))); - $regions[BLOCK_REGION_NONE] = count($regions); - } - - // Separate enabled from disabled. - $status = $b->get('status') - $a->get('status'); - if ($status) { - return $status; - } - // Sort by region (in the order defined by theme .info file). - $aregion = $a->get('region'); - $bregion = $b->get('region'); - if ((!empty($aregion) && !empty($bregion)) && ($place = ($regions[$aregion] - $regions[$bregion]))) { - return $place; - } - // Sort by weight, unless disabled. - if ($a->get('region') != BLOCK_REGION_NONE) { - $weight = $a->get('weight') - $b->get('weight'); - if ($weight) { - return $weight; - } - } - // Sort by label. - return strcmp($a->label(), $b->label()); +function block_admin_display($theme) { + return entity_list_controller('block')->render($theme); } /** diff --git a/core/modules/block/lib/Drupal/block/BlockListController.php b/core/modules/block/lib/Drupal/block/BlockListController.php new file mode 100644 index 0000000..d21e1a9 --- /dev/null +++ b/core/modules/block/lib/Drupal/block/BlockListController.php @@ -0,0 +1,200 @@ +theme) { + $this->theme = $GLOBALS['theme']; + } + + // Store the region list. + $this->regions = system_region_list($this->theme, REGIONS_VISIBLE); + + // Load only blocks for this theme, and sort them. + // @todo Move the functionality of _block_rehash() out of the listing page. + $entities = _block_rehash($this->theme); + uasort($entities, 'static::sort'); + return $entities; + } + + /** + * Overrides \Drupal\Core\Entity\EntityListController::render(). + */ + public function render($theme = NULL) { + // If no theme was specified, use the current theme. + $this->theme = $theme ?: $GLOBALS['theme_key']; + + $form_state = array(); + $form_state['build_info']['args'] = array(); + $form_state['build_info']['callback'] = array($this, 'form'); + return drupal_build_form('block_admin_display_form', $form_state); + } + + /** + * Sorts active blocks by region then weight; sorts inactive blocks by name. + */ + protected function sort(Block $a, Block $b) { + static $regions; + // We need the region list to correctly order by region. + if (!isset($regions)) { + $regions = array_flip(array_keys($this->regions)); + $regions[BLOCK_REGION_NONE] = count($regions); + } + + // Separate enabled from disabled. + $status = $b->get('status') - $a->get('status'); + if ($status) { + return $status; + } + // Sort by region (in the order defined by theme .info file). + $aregion = $a->get('region'); + $bregion = $b->get('region'); + if ((!empty($aregion) && !empty($bregion)) && ($place = ($regions[$aregion] - $regions[$bregion]))) { + return $place; + } + // Sort by weight, unless disabled. + if ($a->get('region') != BLOCK_REGION_NONE) { + $weight = $a->get('weight') - $b->get('weight'); + if ($weight) { + return $weight; + } + } + // Sort by label. + return strcmp($a->label(), $b->label()); + } + + /** + * Form constructor for the main block administration form. + */ + public function form($form, &$form_state) { + $entities = $this->load(); + $form['#attached']['css'][] = drupal_get_path('module', 'block') . '/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); + + foreach ($block_regions_with_disabled as $region => $title) { + $form['#attached']['drupal_add_tabledrag'][] = array('blocks', 'match', 'sibling', 'block-region-select', 'block-region-' . $region, NULL, FALSE); + $form['#attached']['drupal_add_tabledrag'][] = array('blocks', 'order', 'sibling', 'block-weight', 'block-weight-' . $region); + } + $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(); + $form['#tree'] = TRUE; + + foreach ($entities as $entity_id => $entity) { + $info = $entity->getPlugin()->getDefinition(); + $form['blocks'][$entity_id]['info'] = array( + '#markup' => check_plain($info['subject']), + ); + $form['blocks'][$entity_id]['theme'] = array( + '#type' => 'hidden', + '#value' => $this->theme, + ); + $form['blocks'][$entity_id]['weight'] = array( + '#type' => 'weight', + '#default_value' => $entity->get('weight'), + '#delta' => $weight_delta, + '#title_display' => 'invisible', + '#title' => t('Weight for @block block', array('@block' => $info['subject'])), + ); + $form['blocks'][$entity_id]['region'] = array( + '#type' => 'select', + '#default_value' => $entity->get('region') != BLOCK_REGION_NONE ? $entity->get('region') : NULL, + '#empty_value' => BLOCK_REGION_NONE, + '#title_display' => 'invisible', + '#title' => t('Region for @block block', array('@block' => $info['subject'])), + '#options' => $this->regions, + ); + $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', + '#submit' => array(array($this, 'submit')), + ); + return $form; + } + + /** + * Form submission handler for the main block administration form. + */ + public function submit($form, &$form_state) { + $entities = entity_load_multiple('block', array_keys($form_state['values']['blocks'])); + foreach ($entities as $entity_id => $entity) { + $entity->set('weight', $form_state['values']['blocks'][$entity_id]['weight']); + $entity->set('region', $form_state['values']['blocks'][$entity_id]['region']); + $entity->save(); + } + drupal_set_message(t('The block settings have been updated.')); + cache_invalidate_tags(array('content' => TRUE)); + } + +} diff --git a/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php b/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php index 3b45bba..9dffe3c 100644 --- a/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php +++ b/core/modules/block/lib/Drupal/block/Plugin/Core/Entity/Block.php @@ -22,6 +22,7 @@ * controller_class = "Drupal\block\BlockStorageController", * access_controller_class = "Drupal\block\BlockAccessController", * render_controller_class = "Drupal\block\BlockRenderController", + * list_controller_class = "Drupal\block\BlockListController", * form_controller_class = { * "default" = "Drupal\block\BlockFormController" * }, @@ -143,6 +144,19 @@ public function getPlugin() { } /** + * Overrides \Drupal\Core\Entity\Entity::uri(); + */ + public function uri() { + return array( + 'path' => 'admin/structure/block/manage/' . $this->id(), + 'options' => array( + 'entity_type' => $this->entityType, + 'entity' => $this, + ), + ); + } + + /** * Overrides \Drupal\Core\Config\Entity\ConfigEntityBase::get(); */ public function get($property_name, $langcode = NULL) {