Commit 1028ebdf authored by webchick's avatar webchick

Issue #2083877 by tim.plunkett: Add a theme selector to the place block form.

parent 044a4adf
......@@ -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'
......@@ -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;
......@@ -48,6 +49,13 @@ class BlockFormController extends EntityFormController {
*/
protected $languageManager;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* Constructs a BlockFormController object.
*
......@@ -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,11 +273,21 @@ 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' => '<div id="edit-block-region-wrapper">',
'#suffix' => '</div>',
);
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}
*/
......@@ -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())),
));
}
......
......@@ -144,6 +144,31 @@ function testBlock() {
$this->assertRaw(t('The block %name has been removed.', array('%name' => $block['settings[label]'])));
}
/**
* 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.
*/
......
......@@ -77,7 +77,11 @@ public function testGetUniqueMachineName() {
->disableOriginalConstructor()
->getMock();
$block_form_controller = new BlockFormController($entity_manager, $query_factory, $language_manager);
$config_factory = $this->getMockBuilder('Drupal\Core\Config\ConfigFactory')
->disableOriginalConstructor()
->getMock();
$block_form_controller = new BlockFormController($entity_manager, $query_factory, $language_manager, $config_factory);
// Ensure that the block with just one other instance gets the next available
// name suggestion.
......
......@@ -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);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment