diff --git a/core/modules/block/block.routing.yml b/core/modules/block/block.routing.yml index a2ca6e5..6e7e12d 100644 --- a/core/modules/block/block.routing.yml +++ b/core/modules/block/block.routing.yml @@ -24,6 +24,7 @@ block_admin_add: pattern: '/admin/structure/block/add/{plugin_id}/{theme}' defaults: _content: '\Drupal\block\Controller\BlockAddController::blockAddConfigureForm' + theme: null _title: 'Configure block' requirements: _permission: 'administer blocks' diff --git a/core/modules/block/lib/Drupal/block/BlockFormController.php b/core/modules/block/lib/Drupal/block/BlockFormController.php index e873888..ed8f28b 100644 --- a/core/modules/block/lib/Drupal/block/BlockFormController.php +++ b/core/modules/block/lib/Drupal/block/BlockFormController.php @@ -8,6 +8,7 @@ namespace Drupal\block; use Drupal\Core\Cache\Cache; +use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Entity\EntityFormController; use Drupal\Core\Entity\EntityManager; use Drupal\Core\Entity\Query\QueryFactory; @@ -49,6 +50,13 @@ class BlockFormController extends EntityFormController { protected $languageManager; /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactory + */ + protected $configFactory; + + /** * Constructs a BlockFormController object. * * @param \Drupal\Core\Entity\EntityManager $entity_manager @@ -57,11 +65,14 @@ class BlockFormController extends EntityFormController { * The entity query factory. * @param \Drupal\Core\Language\LanguageManager $language_manager * The language manager. + * @param \Drupal\Core\Config\ConfigFactory $config_factory + * The config factory. */ - public function __construct(EntityManager $entity_manager, QueryFactory $entity_query_factory, LanguageManager $language_manager) { + public function __construct(EntityManager $entity_manager, QueryFactory $entity_query_factory, LanguageManager $language_manager, ConfigFactory $config_factory) { $this->storageController = $entity_manager->getStorageController('block'); $this->entityQueryFactory = $entity_query_factory; $this->languageManager = $language_manager; + $this->configFactory = $config_factory; } /** @@ -71,7 +82,8 @@ public static function create(ContainerInterface $container) { return new static( $container->get('entity.manager'), $container->get('entity.query'), - $container->get('language_manager') + $container->get('language_manager'), + $container->get('config.factory') ); } @@ -228,6 +240,32 @@ public function form(array $form, array &$form_state) { '#description' => $this->t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'), ); + // Theme settings. + if ($theme = $entity->get('theme')) { + $form['theme'] = array( + '#type' => 'value', + '#value' => $entity->get('theme'), + ); + } + else { + $theme_options = array(); + foreach (list_themes() as $theme_name => $theme_info) { + if (!empty($theme_info->status)) { + $theme_options[$theme_name] = $theme_info->info['name']; + } + } + $theme = $this->configFactory->get('system.theme')->get('default'); + $form['theme'] = array( + '#type' => 'select', + '#options' => $theme_options, + '#title' => t('Theme'), + '#default_value' => $theme, + '#ajax' => array( + 'callback' => array($this, 'themeSwitch'), + 'wrapper' => 'edit-block-region-wrapper', + ), + ); + } // Region settings. $form['region'] = array( '#type' => 'select', @@ -235,12 +273,22 @@ public function form(array $form, array &$form_state) { '#description' => $this->t('Select the region where this block should be displayed.'), '#default_value' => $entity->get('region'), '#empty_value' => BLOCK_REGION_NONE, - '#options' => system_region_list($entity->get('theme'), REGIONS_VISIBLE), + '#options' => system_region_list($theme, REGIONS_VISIBLE), + '#prefix' => '
', + '#suffix' => '
', ); return $form; } /** + * Handles switching the available regions based on the selected theme. + */ + public function themeSwitch($form, &$form_state) { + $form['region']['#options'] = system_region_list($form_state['values']['theme'], REGIONS_VISIBLE); + return $form['region']; + } + + /** * {@inheritdoc} */ protected function actions(array $form, array &$form_state) { @@ -257,7 +305,7 @@ public function validate(array $form, array &$form_state) { $entity = $this->entity; if ($entity->isNew()) { - form_set_value($form['id'], $entity->get('theme') . '.' . $form_state['values']['machine_name'], $form_state); + form_set_value($form['id'], $form_state['values']['theme'] . '.' . $form_state['values']['machine_name'], $form_state); } if (!empty($form['machine_name']['#disabled'])) { $config_id = explode('.', $form_state['values']['machine_name']); @@ -293,7 +341,7 @@ public function submit(array $form, array &$form_state) { drupal_set_message($this->t('The block configuration has been saved.')); Cache::invalidateTags(array('content' => TRUE)); - $form_state['redirect'] = array('admin/structure/block/list/' . $entity->get('theme'), array( + $form_state['redirect'] = array('admin/structure/block/list/' . $form_state['values']['theme'], array( 'query' => array('block-placement' => drupal_html_class($this->entity->id())), )); } diff --git a/core/modules/block/lib/Drupal/block/Tests/BlockTest.php b/core/modules/block/lib/Drupal/block/Tests/BlockTest.php index 94c8b9e..e90e407 100644 --- a/core/modules/block/lib/Drupal/block/Tests/BlockTest.php +++ b/core/modules/block/lib/Drupal/block/Tests/BlockTest.php @@ -145,6 +145,31 @@ function testBlock() { } /** + * Tests that the block form has a theme selector when not passed via the URL. + */ + public function testBlockThemeSelector() { + // Enable all themes. + theme_enable(array('bartik', 'seven')); + $theme_settings = $this->container->get('config.factory')->get('system.theme'); + foreach (array('bartik', 'stark', 'seven') as $theme) { + // Select the 'Powered by Drupal' block to be placed. + $block = array(); + $block['machine_name'] = strtolower($this->randomName(8)); + $block['theme'] = $theme; + $block['region'] = 'content'; + $this->drupalPost('admin/structure/block/add/system_powered_by_block', $block, t('Save block')); + $this->assertText(t('The block configuration has been saved.')); + $this->assertUrl('admin/structure/block/list/' . $theme . '?block-placement=' . drupal_html_class($theme . ':' . $block['machine_name'])); + + // Set the default theme and ensure the block is placed. + $theme_settings->set('default', $theme)->save(); + $this->drupalGet(''); + $elements = $this->xpath('//div[@id = :id]', array(':id' => drupal_html_id('block-' . $block['machine_name']))); + $this->assertTrue(!empty($elements), 'The block was found.'); + } + } + + /** * Test block title display settings. */ function testHideBlockTitle() { diff --git a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php index 1ebccca..5e32613 100644 --- a/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php +++ b/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php @@ -2377,7 +2377,11 @@ protected function assertUrl($path, array $options = array(), $message = '', $gr )); } $options['absolute'] = TRUE; - return $this->assertEqual($this->getUrl(), $this->container->get('url_generator')->generateFromPath($path, $options), $message, $group); + // Paths in query strings can be encoded or decoded with no functional + // difference, decode them for comparison purposes. + $actual_url = urldecode($this->getUrl()); + $expected_url = urldecode($this->container->get('url_generator')->generateFromPath($path, $options)); + return $this->assertEqual($actual_url, $expected_url, $message, $group); } /**