diff --git a/core/modules/block/block.install b/core/modules/block/block.install index 4363ef8..2c15a2a 100644 --- a/core/modules/block/block.install +++ b/core/modules/block/block.install @@ -206,13 +206,15 @@ function block_update_8007() { 'info', 'revision_id', 'langcode', - 'type' + 'type', + 'changed', )); $revision_insert = db_insert('custom_block_revision')->fields(array( 'id', 'revision_id', 'log', - 'info' + 'info', + 'changed', )); foreach ($results as $block) { $custom_block = array( @@ -221,13 +223,15 @@ function block_update_8007() { 'info' => $block->info, 'revision_id' => $block->bid, 'langcode' => Language::LANGCODE_NOT_SPECIFIED, - 'type' => 'basic' + 'type' => 'basic', + 'changed' => REQUEST_TIME, ); $revision = array( 'id' => $block->bid, 'revision_id' => $block->bid, 'info' => $block->info, - 'log' => 'Initial value from 7.x to 8.x upgrade' + 'log' => 'Initial value from 7.x to 8.x upgrade', + 'changed' => REQUEST_TIME, ); $block_insert->values($custom_block); $revision_insert->values($revision); diff --git a/core/modules/block/custom_block/custom_block.install b/core/modules/block/custom_block/custom_block.install index 0e87811..3474559 100644 --- a/core/modules/block/custom_block/custom_block.install +++ b/core/modules/block/custom_block/custom_block.install @@ -48,6 +48,12 @@ function custom_block_schema() { 'not null' => TRUE, 'default' => '', ), + 'changed' => array( + 'description' => 'The Unix timestamp when the custom block was most recently saved.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), 'langcode' => array( 'description' => 'The {language}.langcode of this node.', 'type' => 'varchar', @@ -104,6 +110,12 @@ function custom_block_schema() { 'default' => '', 'description' => 'Block description.', ), + 'changed' => array( + 'description' => 'The Unix timestamp when the version was most recently saved.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), ), 'primary key' => array('revision_id'), ); @@ -153,6 +165,12 @@ function custom_block_schema_0() { 'not null' => TRUE, 'default' => '', ), + 'changed' => array( + 'description' => 'The Unix timestamp when the custom block was most recently saved.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), 'langcode' => array( 'description' => 'The {language}.langcode of this node.', 'type' => 'varchar', @@ -209,6 +227,12 @@ function custom_block_schema_0() { 'default' => '', 'description' => 'Block description.', ), + 'changed' => array( + 'description' => 'The Unix timestamp when the version was most recently saved.', + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), ), 'primary key' => array('revision_id'), ); diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockInterface.php b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockInterface.php index 23e07e0..9343c15 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockInterface.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/CustomBlockInterface.php @@ -8,11 +8,12 @@ namespace Drupal\custom_block; use Drupal\Core\Entity\ContentEntityInterface; +use Drupal\Core\Entity\EntityChangedInterface; /** * Provides an interface defining a custom block entity. */ -interface CustomBlockInterface extends ContentEntityInterface { +interface CustomBlockInterface extends ContentEntityInterface, EntityChangedInterface { /** * Sets the theme value. diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php index 698c39c..361bb21 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Entity/CustomBlock.php @@ -190,6 +190,14 @@ public function uri() { /** * {@inheritdoc} */ + public function preSave(EntityStorageControllerInterface $storage_controller) { + // Before saving the custom block, set changed time. + $this->changed->value = REQUEST_TIME; + } + + /** + * {@inheritdoc} + */ public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) { // Invalidate the block cache to update custom block-based derivatives. \Drupal::service('plugin.manager.block')->clearCachedDefinitions(); @@ -276,7 +284,19 @@ public static function baseFieldDefinitions($entity_type) { 'description' => t('The revision log message.'), 'type' => 'string_field', ); + $properties['changed'] = array( + 'label' => t('Changed'), + 'description' => t('The time that the custom block was last edited.'), + 'type' => 'integer_field', + ); return $properties; } + /** + * {@inheritdoc} + */ + public function getChangedTime() { + return $this->get('changed')->value; + } + } diff --git a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php index b74de5c..3efb47d 100644 --- a/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php +++ b/core/modules/block/custom_block/lib/Drupal/custom_block/Tests/CustomBlockSaveTest.php @@ -84,6 +84,8 @@ public function testDeterminingChanges() { $block->info->value = 'updated'; $block->save(); + $this->assertNotEqual($block->getChangedTime(), 0, 'Changed time maintained.'); + // The hook implementations custom_block_test_custom_block_presave() and // custom_block_test_custom_block_update() determine changes and change the // title. diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/BlockUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/BlockUpgradePathTest.php index bde6f60..fbba3ad 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/BlockUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/BlockUpgradePathTest.php @@ -60,6 +60,32 @@ public function testBlockUpgradeTitleLength() { // Confirm that the custom block cannot be created with title longer than // the maximum number of characters. $this->assertText('Title cannot be longer than 255 characters'); + + $block = $this->createCustomBlock(); + $this->assertNotEqual($block->getChangedTime(), 0, 'Changed time maintained.'); } + /** + * Creates a custom block. + * + * @param string $title + * (optional) Title of block. When no value is given uses a random name. + * Defaults to FALSE. + * @param string $bundle + * (optional) Bundle name. Defaults to 'basic'. + * + * @return Drupal\custom_block\Entity\CustomBlock + * Created custom block. + */ + protected function createCustomBlock($title = FALSE, $bundle = 'basic') { + $title = ($title ? : $this->randomName()); + if ($custom_block = entity_create('custom_block', array( + 'info' => $title, + 'type' => $bundle, + 'langcode' => 'en' + ))) { + $custom_block->save(); + } + return $custom_block; + } }