Commit 24e2dc26 authored by Dries's avatar Dries

Issue #2074193 by Gábor Hojtsy, Wim Leers: Add changed time tracking to custom blocks.

parent 5f936edc
......@@ -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);
......
......@@ -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'),
);
......
......@@ -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.
......
......@@ -187,6 +187,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}
*/
......@@ -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;
}
}
......@@ -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);
......
......@@ -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;
}
}
}
......
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