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 85d295e..25e1c8b 100644 --- a/core/modules/block/block.module +++ b/core/modules/block/block.module @@ -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->createDuplicate($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(); } diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php index fd8eeab..6b95cbd 100644 --- a/core/modules/block/src/BlockForm.php +++ b/core/modules/block/src/BlockForm.php @@ -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' => '
', diff --git a/core/modules/block/src/BlockInterface.php b/core/modules/block/src/BlockInterface.php index d5dd321..5ed4fac 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 @@ -89,4 +113,32 @@ 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); + } diff --git a/core/modules/block/src/BlockListBuilder.php b/core/modules/block/src/BlockListBuilder.php index 9afbef2..86300bf 100644 --- a/core/modules/block/src/BlockListBuilder.php +++ b/core/modules/block/src/BlockListBuilder.php @@ -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'], ); @@ -400,8 +400,8 @@ 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']); + $entity->setWeight($entity_values['weight']); + $entity->setRegion($entity_values['region']); if ($entity->get('region') == BlockInterface::BLOCK_REGION_NONE) { $entity->disable(); } 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 0edc961..4a69fd9 100644 --- a/core/modules/block/src/Entity/Block.php +++ b/core/modules/block/src/Entity/Block.php @@ -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. @@ -114,6 +114,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} */ public function getPlugin() { @@ -144,6 +151,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() { @@ -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,43 @@ 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; + } + + /** + * Creates a duplicate of the entity. + * + * @param string $new_id + * (optional) Sets a new id on the duplicate block. + * @param null $new_theme + * (optional) Sets 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 createDuplicate($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; + } + } diff --git a/core/modules/block/src/Tests/BlockInvalidRegionTest.php b/core/modules/block/src/Tests/BlockInvalidRegionTest.php index 0f5d38d..722b6eb 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/BlockRenderOrderTest.php b/core/modules/block/src/Tests/BlockRenderOrderTest.php index a926864..11da191 100644 --- a/core/modules/block/src/Tests/BlockRenderOrderTest.php +++ b/core/modules/block/src/Tests/BlockRenderOrderTest.php @@ -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'])); } diff --git a/core/modules/block/src/Tests/BlockStorageUnitTest.php b/core/modules/block/src/Tests/BlockStorageUnitTest.php index b550bcc..56c2277 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->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()); } diff --git a/core/modules/block/src/Tests/BlockTest.php b/core/modules/block/src/Tests/BlockTest.php index 809f289..1a08c11 100644 --- a/core/modules/block/src/Tests/BlockTest.php +++ b/core/modules/block/src/Tests/BlockTest.php @@ -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(''); $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS'); diff --git a/core/modules/language/language.module b/core/modules/language/language.module index bb5cbef..9ef6a73 100644 --- a/core/modules/language/language.module +++ b/core/modules/language/language.module @@ -74,7 +74,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 '

' . t('With multiple languages added, registered users can select their preferred language and authors can assign a specific language to content.') . '

'; } break; diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php index 48615f2..896489c 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateBlockTest.php @@ -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'], ''); - $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()); } } diff --git a/core/modules/system/system.module b/core/modules/system/system.module index 8f2a8ab..c1e4d5b 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -126,7 +126,7 @@ function system_help($route_name, RouteMatchInterface $route_match) { return '

' . t('The uninstall process removes all data related to a module.') . '

'; 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 '

' . t('The Powered by Drupal 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.') . '

'; } break;