diff --git a/core/modules/block/block.api.php b/core/modules/block/block.api.php index 954402a..7f5269f52 100644 --- a/core/modules/block/block.api.php +++ b/core/modules/block/block.api.php @@ -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. diff --git a/core/modules/block/block.module b/core/modules/block/block.module index 39e15ea..da040db 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -124,7 +124,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) { @@ -135,7 +135,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(); } } @@ -185,8 +185,8 @@ function block_theme_initialize($theme) { $block->set('theme', $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(); } @@ -303,7 +303,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(); } } diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php index 73e7ea4..fe55d94 100644 --- a/core/modules/block/src/BlockForm.php +++ b/core/modules/block/src/BlockForm.php @@ -58,7 +58,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); @@ -82,7 +82,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, @@ -112,7 +112,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' => '
', diff --git a/core/modules/block/src/BlockInterface.php b/core/modules/block/src/BlockInterface.php index 432e799..bb83b34 100644 --- a/core/modules/block/src/BlockInterface.php +++ b/core/modules/block/src/BlockInterface.php @@ -33,6 +33,30 @@ 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. * * @return array @@ -40,4 +64,32 @@ public function getPlugin(); */ public function getVisibility(); + /** + * 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); + } diff --git a/core/modules/block/src/BlockListBuilder.php b/core/modules/block/src/BlockListBuilder.php index 1d11f99..1d094d6 100644 --- a/core/modules/block/src/BlockListBuilder.php +++ b/core/modules/block/src/BlockListBuilder.php @@ -185,10 +185,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'], ); @@ -402,9 +402,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 { diff --git a/core/modules/block/src/BlockViewBuilder.php b/core/modules/block/src/BlockViewBuilder.php index 8c781c1..6f2a71d 100644 --- a/core/modules/block/src/BlockViewBuilder.php +++ b/core/modules/block/src/BlockViewBuilder.php @@ -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, diff --git a/core/modules/block/src/Entity/Block.php b/core/modules/block/src/Entity/Block.php index 6f7f276..a4141b1 100644 --- a/core/modules/block/src/Entity/Block.php +++ b/core/modules/block/src/Entity/Block.php @@ -68,7 +68,7 @@ class Block extends ConfigEntityBase implements BlockInterface, EntityWithPlugin * * @var int */ - public $weight; + protected $weight; /** * The plugin instance ID. @@ -85,6 +85,13 @@ class Block extends ConfigEntityBase implements BlockInterface, EntityWithPlugin protected $pluginCollection; /** + * The theme that includes the block plugin for this entity. + * + * @var string + */ + protected $theme; + + /** * {@inheritdoc} */ public function getPlugin() { @@ -112,6 +119,34 @@ public function getPluginCollections() { } /** + * {@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; + } + + /** * Overrides \Drupal\Core\Entity\Entity::label(); */ public function label() { @@ -190,4 +225,20 @@ public function getVisibility() { return $this->getPlugin()->getVisibilityConditions()->getConfiguration(); } + /** + * {@inheritdoc} + */ + public function setRegion($region) { + $this->region = $region; + return $this; + } + + /** + * {@inheritdoc} + */ + public function setWeight($weight) { + $this->weight = $weight; + return $this; + } + } diff --git a/core/modules/block/src/Plugin/DisplayVariant/FullPageVariant.php b/core/modules/block/src/Plugin/DisplayVariant/FullPageVariant.php index 7243362..2c77e29 100644 --- a/core/modules/block/src/Plugin/DisplayVariant/FullPageVariant.php +++ b/core/modules/block/src/Plugin/DisplayVariant/FullPageVariant.php @@ -155,7 +155,7 @@ protected function getRegionAssignments() { $full = array(); foreach ($this->blockStorage->loadByProperties(array('theme' => $this->getTheme())) as $block_id => $block) { - $full[$block->get('region')][$block_id] = $block; + $full[$block->getRegion()][$block_id] = $block; } // Merge it with the actual values to maintain the region ordering. diff --git a/core/modules/block/src/Tests/BlockInvalidRegionTest.php b/core/modules/block/src/Tests/BlockInvalidRegionTest.php index 246684b..43075d1 100644 --- a/core/modules/block/src/Tests/BlockInvalidRegionTest.php +++ b/core/modules/block/src/Tests/BlockInvalidRegionTest.php @@ -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. diff --git a/core/modules/block/src/Tests/BlockStorageUnitTest.php b/core/modules/block/src/Tests/BlockStorageUnitTest.php index 5fb4e8b..5868d3c 100644 --- a/core/modules/block/src/Tests/BlockStorageUnitTest.php +++ b/core/modules/block/src/Tests/BlockStorageUnitTest.php @@ -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->assertEqual($entity->getRegion(), '-1'); $this->assertTrue($entity->get('status')); - $this->assertEqual($entity->get('theme'), 'stark'); + $this->assertEqual($entity->getTheme(), 'stark'); $this->assertTrue($entity->uuid()); } diff --git a/core/modules/block/src/Tests/BlockTest.php b/core/modules/block/src/Tests/BlockTest.php index 1996c9b..9b504d0 100644 --- a/core/modules/block/src/Tests/BlockTest.php +++ b/core/modules/block/src/Tests/BlockTest.php @@ -314,7 +314,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(''); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS'); diff --git a/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/FullPageVariantTest.php b/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/FullPageVariantTest.php index 1cbcfe5..91c0697 100644 --- a/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/FullPageVariantTest.php +++ b/core/modules/block/tests/src/Unit/Plugin/DisplayVariant/FullPageVariantTest.php @@ -108,12 +108,14 @@ public function testBuild() { ->method('access') ->will($this->returnValue($block_config[0])); $block->expects($this->any()) - ->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]); + $block->expects($this->any()) + ->method('getWeight') + ->willReturn($block_config[2]); + $block->expects($this->any()) + ->method('status') + ->willReturn(TRUE); $blocks[$block_id] = $block; } diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php index 4b15b05..53c2bd3 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php @@ -88,77 +88,77 @@ 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->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $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->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $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->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $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->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $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(FALSE, $visibility['request_path']['negate']); $this->assertEqual('', $visibility['request_path']['pages']); - $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(FALSE, $visibility['request_path']['negate']); $this->assertEqual('node', $visibility['request_path']['pages']); - $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->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $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->assertEqual(TRUE, $visibility['request_path']['negate']); $this->assertEqual('', $visibility['request_path']['pages']); - $this->assertEqual(-2, $test_block_block_3->weight); + $this->assertEqual(-2, $test_block_block_3->getWeight()); } }