core/modules/block/block.install | 12 ++++++---- .../block/custom_block/custom_block.install | 24 ++++++++++++++++++++ .../Drupal/custom_block/CustomBlockInterface.php | 3 ++- .../lib/Drupal/custom_block/Entity/CustomBlock.php | 20 ++++++++++++++++ .../custom_block/Tests/CustomBlockSaveTest.php | 4 +++- .../custom_block_test/custom_block_test.module | 2 ++ 6 files changed, 59 insertions(+), 6 deletions(-) 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..405b50c 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 @@ -75,6 +75,7 @@ public function testImport() { public function testDeterminingChanges() { // Initial creation. $block = $this->createCustomBlock('test_changes'); + $this->assertEqual($block->getChangedTime(), REQUEST_TIME, 'Creating a block sets default "changed" timestamp.'); // Update the block without applying changes. $block->save(); @@ -86,8 +87,9 @@ public function testDeterminingChanges() { // The hook implementations custom_block_test_custom_block_presave() and // custom_block_test_custom_block_update() determine changes and change the - // title. + // title as well as programatically set the 'changed' timestamp. $this->assertEqual($block->label(), 'updated_presave_update', 'Changes have been determined.'); + $this->assertEqual($block->getChangedTime(), 979534800, 'Saving a custom block uses "changed" timestamp set in presave hook.'); // Test the static block load cache to be cleared. $block = custom_block_load($block->id->value); diff --git a/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module b/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module index e649f54..277fae3 100644 --- a/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module +++ b/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module @@ -47,6 +47,8 @@ function custom_block_test_custom_block_presave(CustomBlock $custom_block) { if (!empty($custom_block->original) && $custom_block->original->info->value == 'test_changes') { if ($custom_block->original->info->value != $custom_block->info->value) { $custom_block->info->value .= '_presave'; + // Drupal 1.0 release. + $custom_block->changed = 979534800; } } }