Commit 384aae19 authored by catch's avatar catch

Issue #2028035 by vladan.me, ceng, Berdir, David Hernández, larowlan: Expand...

Issue #2028035 by vladan.me, ceng, Berdir, David Hernández, larowlan: Expand CustomBlockInterface with methods.
parent 5ca04d67
......@@ -27,10 +27,9 @@ class CustomBlockFormController extends ContentEntityFormController {
protected function prepareEntity() {
$block = $this->entity;
// Set up default values, if required.
$block_type = entity_load('custom_block_type', $block->type->value);
// If this is a new custom block, fill in the default values.
if (isset($block->id->value)) {
$block->set('log', NULL);
$block_type = entity_load('custom_block_type', $block->bundle());
if (!$block->isNew()) {
$block->setRevisionLog(NULL);
}
// Always use the default revision setting.
$block->setNewRevision($block_type->revision);
......@@ -48,27 +47,19 @@ public function form(array $form, array &$form_state) {
// Override the default CSS class name, since the user-defined custom block
// type name in 'TYPE-block-form' potentially clashes with third-party class
// names.
$form['#attributes']['class'][0] = drupal_html_class('block-' . $block->type->value . '-form');
$form['#attributes']['class'][0] = drupal_html_class('block-' . $block->bundle() . '-form');
// Basic block information.
// These elements are just values so they are not even sent to the client.
foreach (array('revision_id', 'id') as $key) {
$form[$key] = array(
'#type' => 'value',
'#value' => $block->$key->value,
);
}
$form['info'] = array(
'#type' => 'textfield',
'#title' => t('Block description'),
'#required' => TRUE,
'#default_value' => $block->info->value,
'#default_value' => $block->label(),
'#weight' => -5,
'#description' => t('A brief description of your block. Used on the <a href="@overview">Blocks administration page</a>.', array('@overview' => url('admin/structure/block'))),
);
$language_configuration = module_invoke('language', 'get_default_configuration', 'custom_block', $block->type->value);
$language_configuration = module_invoke('language', 'get_default_configuration', 'custom_block', $block->bundle());
// Set the correct default language.
if ($block->isNew() && !empty($language_configuration['langcode'])) {
......@@ -79,7 +70,7 @@ public function form(array $form, array &$form_state) {
$form['langcode'] = array(
'#title' => t('Language'),
'#type' => 'language_select',
'#default_value' => $block->langcode->value,
'#default_value' => $block->getUntranslated()->language()->id,
'#languages' => Language::STATE_ALL,
'#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'],
);
......@@ -130,7 +121,7 @@ public function form(array $form, array &$form_state) {
'#type' => 'textarea',
'#title' => t('Revision log message'),
'#rows' => 4,
'#default_value' => $block->log->value,
'#default_value' => $block->getRevisionLog(),
'#description' => t('Briefly describe the changes you have made.'),
);
......@@ -163,10 +154,10 @@ public function submit(array $form, array &$form_state) {
*/
public function save(array $form, array &$form_state) {
$block = $this->entity;
$insert = empty($block->id->value);
$insert = $block->isNew();
$block->save();
$watchdog_args = array('@type' => $block->bundle(), '%info' => $block->label());
$block_type = entity_load('custom_block_type', $block->type->value);
$block_type = entity_load('custom_block_type', $block->bundle());
$t_args = array('@type' => $block_type->label(), '%info' => $block->label());
if ($insert) {
......@@ -178,9 +169,9 @@ public function save(array $form, array &$form_state) {
drupal_set_message(t('@type %info has been updated.', $t_args));
}
if ($block->id->value) {
$form_state['values']['id'] = $block->id->value;
$form_state['id'] = $block->id->value;
if ($block->id()) {
$form_state['values']['id'] = $block->id();
$form_state['id'] = $block->id();
if ($insert) {
if (!$theme = $block->getTheme()) {
$theme = $this->config('system.theme')->get('default');
......
......@@ -15,6 +15,36 @@
*/
interface CustomBlockInterface extends ContentEntityInterface, EntityChangedInterface {
/**
* Returns the block revision log message.
*
* @return string
* The revision log message.
*/
public function getRevisionLog();
/**
* Sets the block description.
*
* @param string $info
* The block description.
*
* @return \Drupal\custom_block\CustomBlockInterface
* The class instance that this method is called on.
*/
public function setInfo($info);
/**
* Sets the block revision log message.
*
* @param string $log
* The revision log message.
*
* @return \Drupal\custom_block\CustomBlockInterface
* The class instance that this method is called on.
*/
public function setRevisionLog($log);
/**
* Sets the theme value.
*
......@@ -24,6 +54,9 @@ interface CustomBlockInterface extends ContentEntityInterface, EntityChangedInte
*
* @param string $theme
* The theme name.
*
* @return \Drupal\custom_block\CustomBlockInterface
* The class instance that this method is called on.
*/
public function setTheme($theme);
......
......@@ -36,7 +36,7 @@ public function entityFormAlter(array &$form, array &$form_state, EntityInterfac
* Overrides ContentTranslationController::entityFormTitle().
*/
protected function entityFormTitle(EntityInterface $entity) {
$block_type = entity_load('custom_block_type', $entity->type->value);
$block_type = entity_load('custom_block_type', $entity->bundle());
return t('<em>Edit @type</em> @title', array('@type' => $block_type->label(), '@title' => $entity->label()));
}
......
......@@ -22,7 +22,7 @@ class CustomBlockViewBuilder extends EntityViewBuilder {
protected function alterBuild(array &$build, EntityInterface $entity, EntityDisplay $display, $view_mode, $langcode = NULL) {
parent::alterBuild($build, $entity, $display, $view_mode, $langcode);
// Add contextual links for this custom block.
if (!empty($entity->id->value) && $view_mode == 'full') {
if (!$entity->isNew() && $view_mode == 'full') {
$build['#contextual_links']['custom_block'] = array(
'route_parameters' => array('custom_block' => $entity->id()),
);
......
......@@ -59,66 +59,6 @@
*/
class CustomBlock extends ContentEntityBase implements CustomBlockInterface {
/**
* The block ID.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $id;
/**
* The block revision ID.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $revision_id;
/**
* Indicates whether this is the default block revision.
*
* The default revision of a block is the one loaded when no specific revision
* has been specified. Only default revisions are saved to the block_custom
* table.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $isDefaultRevision = TRUE;
/**
* The block UUID.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $uuid;
/**
* The custom block type (bundle).
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $type;
/**
* The block language code.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $langcode;
/**
* The block description.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $info;
/**
* The block revision log message.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $log;
/**
* The theme the block is being created in.
*
......@@ -144,7 +84,7 @@ public function createDuplicate() {
* {@inheritdoc}
*/
public function getRevisionId() {
return $this->revision_id->value;
return $this->get('revision_id')->value;
}
/**
......@@ -152,6 +92,7 @@ public function getRevisionId() {
*/
public function setTheme($theme) {
$this->theme = $theme;
return $this;
}
/**
......@@ -161,23 +102,6 @@ public function getTheme() {
return $this->theme;
}
/**
* Initialize the object. Invoked upon construction and wake up.
*/
protected function init() {
parent::init();
// We unset all defined properties except theme, so magic getters apply.
// $this->theme is a special use-case that is only used in the lifecycle of
// adding a new block using the block library.
unset($this->id);
unset($this->info);
unset($this->revision_id);
unset($this->log);
unset($this->uuid);
unset($this->type);
unset($this->new);
}
/**
* {@inheritdoc}
*/
......@@ -185,7 +109,7 @@ public function preSave(EntityStorageControllerInterface $storage_controller) {
parent::preSave($storage_controller);
// Before saving the custom block, set changed time.
$this->changed->value = REQUEST_TIME;
$this->set('changed', REQUEST_TIME);
}
/**
......@@ -202,7 +126,7 @@ public function postSave(EntityStorageControllerInterface $storage_controller, $
* {@inheritdoc}
*/
public function getInstances() {
return entity_load_multiple_by_properties('block', array('plugin' => 'custom_block:' . $this->uuid->value));
return entity_load_multiple_by_properties('block', array('plugin' => 'custom_block:' . $this->uuid()));
}
/**
......@@ -227,7 +151,7 @@ public function preSaveRevision(EntityStorageControllerInterface $storage_contro
elseif (isset($this->original) && (!isset($record->log) || $record->log === '')) {
// If we are updating an existing custom_block without adding a new
// revision and the user did not supply a log, keep the existing one.
$record->log = $this->original->log->value;
$record->log = $this->original->getRevisionLog();
}
}
......@@ -292,4 +216,27 @@ public function getChangedTime() {
return $this->get('changed')->value;
}
/**
* {@inheritdoc}
*/
public function getRevisionLog() {
return $this->get('log')->value;
}
/**
* {@inheritdoc}
*/
public function setInfo($info) {
$this->set('info', $info);
return $this;
}
/**
* {@inheritdoc}
*/
public function setRevisionLog($log) {
$this->set('log', $log);
return $this;
}
}
......@@ -20,8 +20,8 @@ class CustomBlock extends DerivativeBase {
public function getDerivativeDefinitions(array $base_plugin_definition) {
$custom_blocks = entity_load_multiple('custom_block');
foreach ($custom_blocks as $custom_block) {
$this->derivatives[$custom_block->uuid->value] = $base_plugin_definition;
$this->derivatives[$custom_block->uuid->value]['admin_label'] = $custom_block->info->value;
$this->derivatives[$custom_block->uuid()] = $base_plugin_definition;
$this->derivatives[$custom_block->uuid()]['admin_label'] = $custom_block->label();
}
return parent::getDerivativeDefinitions($base_plugin_definition);
}
......
......@@ -48,9 +48,9 @@ public function testHookCustomBlockLoad() {
$custom_blocks = entity_load_multiple_by_properties('custom_block', array('type' => 'basic'));
$loaded_custom_block = end($custom_blocks);
$this->assertEqual($loaded_custom_block->custom_block_test_loaded_ids, array(
$custom_block1->id->value,
$custom_block2->id->value,
$custom_block3->id->value
$custom_block1->id(),
$custom_block2->id(),
$custom_block3->id(),
), 'hook_custom_block_load() received the correct list of custom_block IDs the first time it was called.');
$this->assertEqual($loaded_custom_block->custom_block_test_loaded_types, array('basic'), 'hook_custom_block_load() received the correct list of custom block types the first time it was called.');
......@@ -60,10 +60,10 @@ public function testHookCustomBlockLoad() {
$custom_blocks = entity_load_multiple('custom_block', \Drupal::entityQuery('custom_block')->execute(), TRUE);
$loaded_custom_block = end($custom_blocks);
$this->assertEqual($loaded_custom_block->custom_block_test_loaded_ids, array(
$custom_block1->id->value,
$custom_block2->id->value,
$custom_block3->id->value,
$custom_block4->id->value
$custom_block1->id(),
$custom_block2->id(),
$custom_block3->id(),
$custom_block4->id(),
), 'hook_custom_block_load() received the correct list of custom_block IDs the second time it was called.');
$this->assertEqual($loaded_custom_block->custom_block_test_loaded_types, array('basic', 'other'), 'hook_custom_block_load() received the correct list of custom_block types the second time it was called.');
}
......
......@@ -48,16 +48,17 @@ protected function setUp() {
$logs = array();
// Get original block.
$blocks[] = $block->revision_id->value;
$blocks[] = $block->getRevisionId();
$logs[] = '';
// Create three revisions.
$revision_count = 3;
for ($i = 0; $i < $revision_count; $i++) {
$block->setNewRevision(TRUE);
$logs[] = $block->log->value = $this->randomName(32);
$block->setRevisionLog($this->randomName(32));
$logs[] = $block->getRevisionLog();
$block->save();
$blocks[] = $block->revision_id->value;
$blocks[] = $block->getRevisionId();
}
$this->blocks = $blocks;
......@@ -75,8 +76,8 @@ public function testRevisions() {
// Confirm the correct revision text appears.
$loaded = entity_revision_load('custom_block', $revision_id);
// Verify log is the same.
$this->assertEqual($loaded->log->value, $logs[$delta], format_string('Correct log message found for revision !revision', array(
'!revision' => $loaded->revision_id->value
$this->assertEqual($loaded->getRevisionLog(), $logs[$delta], format_string('Correct log message found for revision !revision', array(
'!revision' => $loaded->getRevisionId(),
)));
}
......@@ -87,17 +88,17 @@ public function testRevisions() {
// This will create a new revision that is not "front facing".
// Save this as a non-default revision.
$loaded->setNewRevision();
$loaded->isDefaultRevision = FALSE;
$loaded->isDefaultRevision(FALSE);
$loaded->body = $this->randomName(8);
$loaded->save();
$this->drupalGet('block/' . $loaded->id->value);
$this->drupalGet('block/' . $loaded->id());
$this->assertNoText($loaded->body->value, 'Revision body text is not present on default version of block.');
// Verify that the non-default revision id is greater than the default
// revision id.
$default_revision = entity_load('custom_block', $loaded->id->value);
$this->assertTrue($loaded->revision_id->value > $default_revision->revision_id->value, 'Revision id is greater than default revision id.');
$default_revision = entity_load('custom_block', $loaded->id());
$this->assertTrue($loaded->getRevisionId() > $default_revision->getRevisionId(), 'Revision id is greater than default revision id.');
}
}
......@@ -60,7 +60,7 @@ public function testImport() {
$block->save();
// Verify that block_submit did not wipe the provided id.
$this->assertEqual($block->id->value, $test_id, 'Block imported using provide id');
$this->assertEqual($block->id(), $test_id, 'Block imported using provide id');
// Test the import saved.
$block_by_id = custom_block_load($test_id);
......@@ -82,7 +82,7 @@ public function testDeterminingChanges() {
$this->assertEqual($block->label(), 'test_changes', 'No changes have been determined.');
// Apply changes.
$block->info->value = 'updated';
$block->setInfo('updated');
$block->save();
// The hook implementations custom_block_test_custom_block_presave() and
......@@ -92,7 +92,7 @@ public function testDeterminingChanges() {
$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);
$block = custom_block_load($block->id());
$this->assertEqual($block->label(), 'updated_presave', 'Static cache has been cleared.');
}
......@@ -109,7 +109,7 @@ public function testCustomBlockSaveOnInsert() {
// custom_block_test_custom_block_insert() tiggers a save on insert if the
// title equals 'new'.
$block = $this->createCustomBlock('new');
$this->assertEqual($block->label(), 'CustomBlock ' . $block->id->value, 'Custom block saved on block insert.');
$this->assertEqual($block->label(), 'CustomBlock ' . $block->id(), 'Custom block saved on block insert.');
}
}
......@@ -65,8 +65,8 @@ public function testPageEdit() {
$this->drupalPostForm(NULL, $edit, t('Save'));
// Ensure that the block revision has been created.
$revised_block = entity_load('custom_block', $block->id->value, TRUE);
$this->assertNotIdentical($block->revision_id->value, $revised_block->revision_id->value, 'A new revision has been created.');
$revised_block = entity_load('custom_block', $block->id(), TRUE);
$this->assertNotIdentical($block->getRevisionId(), $revised_block->getRevisionId(), 'A new revision has been created.');
// Test deleting the block.
$this->drupalGet("block/" . $revised_block->id());
......
......@@ -40,13 +40,13 @@ function custom_block_test_custom_block_view(CustomBlock $custom_block, $view_mo
* Implements hook_custom_block_presave().
*/
function custom_block_test_custom_block_presave(CustomBlock $custom_block) {
if ($custom_block->info->value == 'testing_custom_block_presave') {
$custom_block->info->value .= '_presave';
if ($custom_block->label() == 'testing_custom_block_presave') {
$custom_block->setInfo($custom_block->label() .'_presave');
}
// Determine changes.
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';
if (!empty($custom_block->original) && $custom_block->original->label() == 'test_changes') {
if ($custom_block->original->label() != $custom_block->label()) {
$custom_block->setInfo($custom_block->label() .'_presave');
// Drupal 1.0 release.
$custom_block->changed = 979534800;
}
......@@ -58,9 +58,9 @@ function custom_block_test_custom_block_presave(CustomBlock $custom_block) {
*/
function custom_block_test_custom_block_update(CustomBlock $custom_block) {
// Determine changes on update.
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 .= '_update';
if (!empty($custom_block->original) && $custom_block->original->label() == 'test_changes') {
if ($custom_block->original->label() != $custom_block->label()) {
$custom_block->setInfo($custom_block->label() .'_update');
}
}
}
......@@ -74,11 +74,11 @@ function custom_block_test_custom_block_update(CustomBlock $custom_block) {
*/
function custom_block_test_custom_block_insert(CustomBlock $custom_block) {
// Set the custom_block title to the custom_block ID and save.
if ($custom_block->info->value == 'new') {
$custom_block->info->value = 'CustomBlock ' . $custom_block->id->value;
if ($custom_block->label() == 'new') {
$custom_block->setInfo('CustomBlock ' . $custom_block->id());
$custom_block->save();
}
if ($custom_block->info->value == 'fail_creation') {
if ($custom_block->label() == 'fail_creation') {
throw new Exception('Test exception for rollback.');
}
}
......
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