Commit 694d95f6 authored by catch's avatar catch

Issue #2030571 by calebtr, daffie, filijonka, YesCT, Thomas Brekelmans,...

Issue #2030571 by calebtr, daffie, filijonka, YesCT, Thomas Brekelmans, alexpott, Mile23, tadityar, Sharique, tim.plunkett, boztek: Expand Block with methods
parent 31f3b0e7
......@@ -154,8 +154,8 @@ function hook_block_view_BASE_BLOCK_ID_alter(array &$build, \Drupal\Core\Block\B
function hook_block_access(\Drupal\block\Entity\Block $block, $operation, \Drupal\user\Entity\User $account, $langcode) {
// Example code that would prevent displaying the 'Powered by Drupal' block in
// a region different than the footer.
if ($operation == 'view' && $block->get('plugin') == 'system_powered_by_block') {
return AccessResult::forbiddenIf($block->get('region') != 'footer')->cacheUntilEntityChanges($block);
if ($operation == 'view' && $block->getPluginId() == 'system_powered_by_block') {
return AccessResult::forbiddenIf($block->getRegion() != 'footer')->cacheUntilEntityChanges($block);
}
// No opinion.
......
......@@ -103,7 +103,7 @@ function _block_rehash($theme = NULL) {
unset($blocks[$block_id]);
continue;
}
$region = $block->get('region');
$region = $block->getRegion();
$status = $block->status();
// Disable blocks in invalid regions.
if (!empty($region) && $region != BlockInterface::BLOCK_REGION_NONE && !isset($regions[$region]) && $status) {
......@@ -114,7 +114,7 @@ function _block_rehash($theme = NULL) {
}
// Set region to none if not enabled.
if (!$status && $region != BlockInterface::BLOCK_REGION_NONE) {
$block->set('region', BlockInterface::BLOCK_REGION_NONE);
$block->setRegion(BlockInterface::BLOCK_REGION_NONE);
$block->save();
}
}
......@@ -159,13 +159,11 @@ function block_theme_initialize($theme) {
else {
$id = $theme . '_' . $default_theme_block_id;
}
$block = $default_theme_block->createDuplicate();
$block->set('id', $id);
$block->set('theme', $theme);
$block = $default_theme_block->createDuplicateBlock($id, $theme);
// If the region isn't supported by the theme, assign the block to the
// theme's default region.
if (!isset($regions[$block->get('region')])) {
$block->set('region', system_default_region($theme));
if (!isset($regions[$block->getRegion()])) {
$block->setRegion(system_default_region($theme));
}
$block->save();
}
......@@ -282,7 +280,7 @@ function block_user_role_delete($role) {
function block_menu_delete(Menu $menu) {
if (!$menu->isSyncing()) {
foreach (Block::loadMultiple() as $block) {
if ($block->get('plugin') == 'system_menu_block:' . $menu->id()) {
if ($block->getPluginId() == 'system_menu_block:' . $menu->id()) {
$block->delete();
}
}
......
......@@ -98,7 +98,7 @@ public function form(array $form, FormStateInterface $form_state) {
$entity = $this->entity;
// Store theme settings in $form_state for use below.
if (!$theme = $entity->get('theme')) {
if (!$theme = $entity->getTheme()) {
$theme = $this->config('system.theme')->get('default');
}
$form_state->set('block_theme', $theme);
......@@ -127,7 +127,7 @@ public function form(array $form, FormStateInterface $form_state) {
);
// Theme settings.
if ($entity->get('theme')) {
if ($entity->getTheme()) {
$form['theme'] = array(
'#type' => 'value',
'#value' => $theme,
......@@ -157,7 +157,7 @@ public function form(array $form, FormStateInterface $form_state) {
'#type' => 'select',
'#title' => $this->t('Region'),
'#description' => $this->t('Select the region where this block should be displayed.'),
'#default_value' => $entity->get('region'),
'#default_value' => $entity->getRegion(),
'#empty_value' => BlockInterface::BLOCK_REGION_NONE,
'#options' => system_region_list($theme, REGIONS_VISIBLE),
'#prefix' => '<div id="edit-block-region-wrapper">',
......
......@@ -32,6 +32,30 @@ interface BlockInterface extends ConfigEntityInterface {
*/
public function getPlugin();
/**
* Returns the plugin ID.
*
* @return string
* The plugin ID for this block.
*/
public function getPluginId();
/**
* Returns the region this block is placed in.
*
* @return string
* The region this block is placed in.
*/
public function getRegion();
/**
* Returns the theme ID.
*
* @return string
* The theme ID for this block instance.
*/
public function getTheme();
/**
* Returns an array of visibility condition configurations.
*
......@@ -89,4 +113,46 @@ public function getContexts();
*/
public function setContexts(array $contexts);
/**
* Returns the weight of this block (used for sorting).
*
* @return int
* The block weight.
*/
public function getWeight();
/**
* Sets the region this block is placed in.
*
* @param string $region
* The region to place this block in.
*
* @return $this
*/
public function setRegion($region);
/**
* Sets the block weight.
*
* @param int $weight
* The desired weight.
*
* @return $this
*/
public function setWeight($weight);
/**
* Creates a duplicate of the block entity.
*
* @param string $new_id
* (optional) The new ID on the duplicate block.
* @param string $new_theme
* (optional) The theme on the duplicate block.
*
* @return static
* A clone of $this with all identifiers unset, so saving it inserts a new
* entity into the storage system.
*/
public function createDuplicateBlock($new_id = NULL, $new_theme = NULL);
}
......@@ -183,10 +183,10 @@ public function buildForm(array $form, FormStateInterface $form_state) {
// Build blocks first for each region.
foreach ($entities as $entity_id => $entity) {
$definition = $entity->getPlugin()->getPluginDefinition();
$blocks[$entity->get('region')][$entity_id] = array(
$blocks[$entity->getRegion()][$entity_id] = array(
'label' => $entity->label(),
'entity_id' => $entity_id,
'weight' => $entity->get('weight'),
'weight' => $entity->getWeight(),
'entity' => $entity,
'category' => $definition['category'],
);
......@@ -401,9 +401,9 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
$entities = $this->storage->loadMultiple(array_keys($form_state->getValue('blocks')));
foreach ($entities as $entity_id => $entity) {
$entity_values = $form_state->getValue(array('blocks', $entity_id));
$entity->set('weight', $entity_values['weight']);
$entity->set('region', $entity_values['region']);
if ($entity->get('region') == BlockInterface::BLOCK_REGION_NONE) {
$entity->setWeight($entity_values['weight']);
$entity->setRegion($entity_values['region']);
if ($entity->getRegion() == BlockInterface::BLOCK_REGION_NONE) {
$entity->disable();
}
else {
......
......@@ -78,7 +78,7 @@ public function getVisibleBlocksPerRegion(array $contexts) {
/** @var \Drupal\block\BlockInterface $block */
// Set the contexts on the block before checking access.
if ($block->setContexts($contexts)->access('view')) {
$full[$block->get('region')][$block_id] = $block;
$full[$block->getRegion()][$block_id] = $block;
}
}
......
......@@ -57,7 +57,7 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
'route_parameters' => array('block' => $entity->id()),
),
),
'#weight' => $entity->get('weight'),
'#weight' => $entity->getWeight(),
'#configuration' => $configuration,
'#plugin_id' => $plugin_id,
'#base_plugin_id' => $base_id,
......
......@@ -48,7 +48,7 @@ class Block extends ConfigEntityBase implements BlockInterface, EntityWithPlugin
*
* @var string
*/
public $id;
protected $id;
/**
* The plugin instance settings.
......@@ -69,7 +69,7 @@ class Block extends ConfigEntityBase implements BlockInterface, EntityWithPlugin
*
* @var int
*/
public $weight;
protected $weight;
/**
* The plugin instance ID.
......@@ -113,6 +113,13 @@ class Block extends ConfigEntityBase implements BlockInterface, EntityWithPlugin
*/
protected $conditionPluginManager;
/**
* The theme that includes the block plugin for this entity.
*
* @var string
*/
protected $theme;
/**
* {@inheritdoc}
*/
......@@ -144,7 +151,35 @@ public function getPluginCollections() {
}
/**
* Overrides \Drupal\Core\Entity\Entity::label();
* {@inheritdoc}
*/
public function getPluginId() {
return $this->plugin;
}
/**
* {@inheritdoc}
*/
public function getRegion() {
return $this->region;
}
/**
* {@inheritdoc}
*/
public function getTheme() {
return $this->theme;
}
/**
* {@inheritdoc}
*/
public function getWeight() {
return $this->weight;
}
/**
* {@inheritdoc}
*/
public function label() {
$settings = $this->get('settings');
......@@ -162,13 +197,13 @@ public function label() {
*/
public static function sort(ConfigEntityInterface $a, ConfigEntityInterface $b) {
// Separate enabled from disabled.
$status = $b->get('status') - $a->get('status');
if ($status) {
$status = (int) $b->status() - (int) $a->status();
if ($status !== 0) {
return $status;
}
// Sort by weight, unless disabled.
if ($a->get('region') != static::BLOCK_REGION_NONE) {
$weight = $a->get('weight') - $b->get('weight');
if ($a->getRegion() != static::BLOCK_REGION_NONE) {
$weight = $a->getWeight() - $b->getWeight();
if ($weight) {
return $weight;
}
......@@ -283,4 +318,34 @@ protected function conditionPluginManager() {
return $this->conditionPluginManager;
}
/**
* {@inheritdoc}
*/
public function setRegion($region) {
$this->region = $region;
return $this;
}
/**
* {@inheritdoc}
*/
public function setWeight($weight) {
$this->weight = $weight;
return $this;
}
/**
* {@inheritdoc}
*/
public function createDuplicateBlock($new_id = NULL, $new_theme = NULL) {
$duplicate = parent::createDuplicate();
if (!empty($new_id)) {
$duplicate->id = $new_id;
}
if (!empty($new_theme)) {
$duplicate->theme = $new_theme;
}
return $duplicate;
}
}
......@@ -42,7 +42,7 @@ protected function setUp() {
function testBlockInInvalidRegion() {
// Enable a test block and place it in an invalid region.
$block = $this->drupalPlaceBlock('test_html');
$block->set('region', 'invalid_region');
$block->setRegion('invalid_region');
$block->save();
$warning_message = t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $block->id(), '%region' => 'invalid_region'));
......@@ -57,7 +57,7 @@ function testBlockInInvalidRegion() {
// Place disabled test block in the invalid region of the default theme.
$block = Block::load($block->id());
$block->set('region', 'invalid_region');
$block->setRegion('invalid_region');
$block->save();
// Clear the cache to check if the warning message is not triggered.
......
......@@ -72,8 +72,8 @@ function testBlockRenderOrder() {
$controller = $this->container->get('entity.manager')->getStorage('block');
foreach ($controller->loadMultiple() as $return_block) {
$id = $return_block->get('id');
if ($return_block_weight = $return_block->get('weight')) {
$id = $return_block->id();
if ($return_block_weight = $return_block->getWeight()) {
$this->assertTrue($test_blocks[$id]['weight'] == $return_block_weight, 'Block weight is set as "' . $return_block_weight . '" for ' . $id . ' block.');
$position[$id] = strpos($test_content, Html::getClass('block-' . $test_blocks[$id]['id']));
}
......
......@@ -120,9 +120,9 @@ protected function loadTests() {
$this->assertTrue($entity instanceof Block, 'The loaded entity is a Block.');
// Verify several properties of the block.
$this->assertEqual($entity->get('region'), '-1');
$this->assertTrue($entity->get('status'));
$this->assertEqual($entity->get('theme'), 'stark');
$this->assertEqual($entity->getRegion(), '-1');
$this->assertTrue($entity->status());
$this->assertEqual($entity->getTheme(), 'stark');
$this->assertTrue($entity->uuid());
}
......
......@@ -348,7 +348,7 @@ public function testBlockCacheTags() {
$this->assertIdentical($cache_entry->tags, $expected_cache_tags);
// The "Powered by Drupal" block is modified; verify a cache miss.
$block->set('region', 'content');
$block->setRegion('content');
$block->save();
$this->drupalGet('<front>');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
......
......@@ -93,12 +93,8 @@ public function testGetVisibleBlocksPerRegion(array $blocks_config, array $expec
->method('access')
->will($this->returnValue($block_config[0]));
$block->expects($block_config[0] ? $this->atLeastOnce() : $this->never())
->method('get')
->will($this->returnValueMap(array(
array('region', $block_config[1]),
array('weight', $block_config[2]),
array('status', TRUE),
)));
->method('getRegion')
->willReturn($block_config[1]);
$blocks[$block_id] = $block;
}
......@@ -159,8 +155,7 @@ public function testGetVisibleBlocksPerRegionWithContext() {
->method('access')
->willReturn(TRUE);
$block->expects($this->once())
->method('get')
->with('region')
->method('getRegion')
->willReturn('top');
$blocks['block_id'] = $block;
......
......@@ -61,7 +61,7 @@ public function getFilterLabels() {
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$theme = $entity->get('theme');
$theme = $entity->getTheme();
$plugin_definition = $entity->getPlugin()->getPluginDefinition();
$row['label'] = array(
......
......@@ -77,7 +77,7 @@ function language_help($route_name, RouteMatchInterface $route_match) {
return $output;
case 'entity.block.edit_form':
if (($block = $route_match->getParameter('block')) && $block->get('plugin') == 'language_block:language_interface') {
if (($block = $route_match->getParameter('block')) && $block->getPluginId() == 'language_block:language_interface') {
return '<p>' . t('With multiple languages added, registered users can select their preferred language and authors can assign a specific language to content.') . '</p>';
}
break;
......
......@@ -88,69 +88,69 @@ public function testBlockMigration() {
// User blocks
$test_block_user = $blocks['user'];
$this->assertNotNull($test_block_user);
$this->assertEqual('sidebar_first', $test_block_user->get('region'));
$this->assertEqual('bartik', $test_block_user->get('theme'));
$this->assertEqual('sidebar_first', $test_block_user->getRegion());
$this->assertEqual('bartik', $test_block_user->getTheme());
$visibility = $test_block_user->getVisibility();
$this->assertTrue(empty($visibility['request_path']['pages']));
$this->assertEqual(0, $test_block_user->weight);
$this->assertEqual(0, $test_block_user->getWeight());
$test_block_user_1 = $blocks['user_1'];
$this->assertNotNull($test_block_user_1);
$this->assertEqual('sidebar_first', $test_block_user_1->get('region'));
$this->assertEqual('bartik', $test_block_user_1->get('theme'));
$this->assertEqual('sidebar_first', $test_block_user_1->getRegion());
$this->assertEqual('bartik', $test_block_user_1->getTheme());
$visibility = $test_block_user_1->getVisibility();
$this->assertTrue(empty($visibility['request_path']['pages']));
$this->assertEqual(0, $test_block_user_1->weight);
$this->assertEqual(0, $test_block_user_1->getWeight());
// Check system block
$test_block_system = $blocks['system'];
$this->assertNotNull($test_block_system);
$this->assertEqual('footer', $test_block_system->get('region'));
$this->assertEqual('bartik', $test_block_system->get('theme'));
$this->assertEqual('footer', $test_block_system->getRegion());
$this->assertEqual('bartik', $test_block_system->getTheme());
$visibility = $test_block_system->getVisibility();
$this->assertTrue(empty($visibility['request_path']['pages']));
$this->assertEqual(-5, $test_block_system->weight);
$this->assertEqual(-5, $test_block_system->getWeight());
// Check menu blocks
$test_block_menu = $blocks['menu'];
$this->assertNotNull($test_block_menu);
$this->assertEqual('header', $test_block_menu->get('region'));
$this->assertEqual('bartik', $test_block_menu->get('theme'));
$this->assertEqual('header', $test_block_menu->getRegion());
$this->assertEqual('bartik', $test_block_menu->getTheme());
$visibility = $test_block_menu->getVisibility();
$this->assertTrue(empty($visibility['request_path']['pages']));
$this->assertEqual(-5, $test_block_menu->weight);
$this->assertEqual(-5, $test_block_menu->getWeight());
// Check custom blocks
$test_block_block = $blocks['block'];
$this->assertNotNull($test_block_block);
$this->assertEqual('content', $test_block_block->get('region'));
$this->assertEqual('bartik', $test_block_block->get('theme'));
$this->assertEqual('content', $test_block_block->getRegion());
$this->assertEqual('bartik', $test_block_block->getTheme());
$visibility = $test_block_block->getVisibility();
$this->assertEqual($visibility['request_path']['pages'], '<front>');
$this->assertEqual(0, $test_block_block->weight);
$this->assertEqual(0, $test_block_block->getWeight());
$test_block_block_1 = $blocks['block_1'];
$this->assertNotNull($test_block_block_1);
$this->assertEqual('right', $test_block_block_1->get('region'));
$this->assertEqual('bluemarine', $test_block_block_1->get('theme'));
$this->assertEqual('right', $test_block_block_1->getRegion());
$this->assertEqual('bluemarine', $test_block_block_1->getTheme());
$visibility = $test_block_block_1->getVisibility();
$this->assertEqual($visibility['request_path']['pages'], 'node');
$this->assertEqual(-4, $test_block_block_1->weight);
$this->assertEqual(-4, $test_block_block_1->getWeight());
$test_block_block_2 = $blocks['block_2'];
$this->assertNotNull($test_block_block_2);
$this->assertEqual('right', $test_block_block_2->get('region'));
$this->assertEqual('test_theme', $test_block_block_2->get('theme'));
$this->assertEqual('right', $test_block_block_2->getRegion());
$this->assertEqual('test_theme', $test_block_block_2->getTheme());
$visibility = $test_block_block_2->getVisibility();
$this->assertTrue(empty($visibility['request_path']['pages']));
$this->assertEqual(-7, $test_block_block_2->weight);
$this->assertEqual(-7, $test_block_block_2->getWeight());
$test_block_block_3 = $blocks['block_3'];
$this->assertNotNull($test_block_block_3);
$this->assertEqual('left', $test_block_block_3->get('region'));
$this->assertEqual('test_theme', $test_block_block_3->get('theme'));
$this->assertEqual('left', $test_block_block_3->getRegion());
$this->assertEqual('test_theme', $test_block_block_3->getTheme());
$visibility = $test_block_block_3->getVisibility();
$this->assertTrue(empty($visibility['request_path']['pages']));
$this->assertEqual(-2, $test_block_block_3->weight);
$this->assertEqual(-2, $test_block_block_3->getWeight());
}
}
......@@ -126,7 +126,7 @@ function system_help($route_name, RouteMatchInterface $route_match) {
return '<p>' . t('The uninstall process removes all data related to a module.') . '</p>';
case 'entity.block.edit_form':
if (($block = $route_match->getParameter('block')) && $block->get('plugin') == 'system_powered_by_block') {
if (($block = $route_match->getParameter('block')) && $block->getPluginId() == 'system_powered_by_block') {
return '<p>' . t('The <em>Powered by Drupal</em> block is an optional link to the home page of the Drupal project. While there is absolutely no requirement that sites feature this link, it may be used to show support for Drupal.') . '</p>';
}
break;
......
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