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

8.0.x
Nathaniel Catchpole 2013-12-10 14:13:23 +00:00
parent 5ca04d6782
commit 384aae19c0
11 changed files with 112 additions and 140 deletions

View File

@ -27,10 +27,9 @@ class CustomBlockFormController extends ContentEntityFormController {
protected function prepareEntity() { protected function prepareEntity() {
$block = $this->entity; $block = $this->entity;
// Set up default values, if required. // Set up default values, if required.
$block_type = entity_load('custom_block_type', $block->type->value); $block_type = entity_load('custom_block_type', $block->bundle());
// If this is a new custom block, fill in the default values. if (!$block->isNew()) {
if (isset($block->id->value)) { $block->setRevisionLog(NULL);
$block->set('log', NULL);
} }
// Always use the default revision setting. // Always use the default revision setting.
$block->setNewRevision($block_type->revision); $block->setNewRevision($block_type->revision);
@ -48,27 +47,19 @@ class CustomBlockFormController extends ContentEntityFormController {
// Override the default CSS class name, since the user-defined custom block // Override the default CSS class name, since the user-defined custom block
// type name in 'TYPE-block-form' potentially clashes with third-party class // type name in 'TYPE-block-form' potentially clashes with third-party class
// names. // 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. // 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( $form['info'] = array(
'#type' => 'textfield', '#type' => 'textfield',
'#title' => t('Block description'), '#title' => t('Block description'),
'#required' => TRUE, '#required' => TRUE,
'#default_value' => $block->info->value, '#default_value' => $block->label(),
'#weight' => -5, '#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'))), '#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. // Set the correct default language.
if ($block->isNew() && !empty($language_configuration['langcode'])) { if ($block->isNew() && !empty($language_configuration['langcode'])) {
@ -79,7 +70,7 @@ class CustomBlockFormController extends ContentEntityFormController {
$form['langcode'] = array( $form['langcode'] = array(
'#title' => t('Language'), '#title' => t('Language'),
'#type' => 'language_select', '#type' => 'language_select',
'#default_value' => $block->langcode->value, '#default_value' => $block->getUntranslated()->language()->id,
'#languages' => Language::STATE_ALL, '#languages' => Language::STATE_ALL,
'#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'], '#access' => isset($language_configuration['language_show']) && $language_configuration['language_show'],
); );
@ -130,7 +121,7 @@ class CustomBlockFormController extends ContentEntityFormController {
'#type' => 'textarea', '#type' => 'textarea',
'#title' => t('Revision log message'), '#title' => t('Revision log message'),
'#rows' => 4, '#rows' => 4,
'#default_value' => $block->log->value, '#default_value' => $block->getRevisionLog(),
'#description' => t('Briefly describe the changes you have made.'), '#description' => t('Briefly describe the changes you have made.'),
); );
@ -163,10 +154,10 @@ class CustomBlockFormController extends ContentEntityFormController {
*/ */
public function save(array $form, array &$form_state) { public function save(array $form, array &$form_state) {
$block = $this->entity; $block = $this->entity;
$insert = empty($block->id->value); $insert = $block->isNew();
$block->save(); $block->save();
$watchdog_args = array('@type' => $block->bundle(), '%info' => $block->label()); $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()); $t_args = array('@type' => $block_type->label(), '%info' => $block->label());
if ($insert) { if ($insert) {
@ -178,9 +169,9 @@ class CustomBlockFormController extends ContentEntityFormController {
drupal_set_message(t('@type %info has been updated.', $t_args)); drupal_set_message(t('@type %info has been updated.', $t_args));
} }
if ($block->id->value) { if ($block->id()) {
$form_state['values']['id'] = $block->id->value; $form_state['values']['id'] = $block->id();
$form_state['id'] = $block->id->value; $form_state['id'] = $block->id();
if ($insert) { if ($insert) {
if (!$theme = $block->getTheme()) { if (!$theme = $block->getTheme()) {
$theme = $this->config('system.theme')->get('default'); $theme = $this->config('system.theme')->get('default');

View File

@ -15,6 +15,36 @@ use Drupal\Core\Entity\EntityChangedInterface;
*/ */
interface CustomBlockInterface extends ContentEntityInterface, EntityChangedInterface { 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. * Sets the theme value.
* *
@ -24,6 +54,9 @@ interface CustomBlockInterface extends ContentEntityInterface, EntityChangedInte
* *
* @param string $theme * @param string $theme
* The theme name. * The theme name.
*
* @return \Drupal\custom_block\CustomBlockInterface
* The class instance that this method is called on.
*/ */
public function setTheme($theme); public function setTheme($theme);

View File

@ -36,7 +36,7 @@ class CustomBlockTranslationController extends ContentTranslationController {
* Overrides ContentTranslationController::entityFormTitle(). * Overrides ContentTranslationController::entityFormTitle().
*/ */
protected function entityFormTitle(EntityInterface $entity) { 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())); return t('<em>Edit @type</em> @title', array('@type' => $block_type->label(), '@title' => $entity->label()));
} }

View File

@ -22,7 +22,7 @@ class CustomBlockViewBuilder extends EntityViewBuilder {
protected function alterBuild(array &$build, EntityInterface $entity, EntityDisplay $display, $view_mode, $langcode = NULL) { protected function alterBuild(array &$build, EntityInterface $entity, EntityDisplay $display, $view_mode, $langcode = NULL) {
parent::alterBuild($build, $entity, $display, $view_mode, $langcode); parent::alterBuild($build, $entity, $display, $view_mode, $langcode);
// Add contextual links for this custom block. // 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( $build['#contextual_links']['custom_block'] = array(
'route_parameters' => array('custom_block' => $entity->id()), 'route_parameters' => array('custom_block' => $entity->id()),
); );

View File

@ -59,66 +59,6 @@ use Drupal\custom_block\CustomBlockInterface;
*/ */
class CustomBlock extends ContentEntityBase implements CustomBlockInterface { 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. * The theme the block is being created in.
* *
@ -144,7 +84,7 @@ class CustomBlock extends ContentEntityBase implements CustomBlockInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getRevisionId() { public function getRevisionId() {
return $this->revision_id->value; return $this->get('revision_id')->value;
} }
/** /**
@ -152,6 +92,7 @@ class CustomBlock extends ContentEntityBase implements CustomBlockInterface {
*/ */
public function setTheme($theme) { public function setTheme($theme) {
$this->theme = $theme; $this->theme = $theme;
return $this;
} }
/** /**
@ -161,23 +102,6 @@ class CustomBlock extends ContentEntityBase implements CustomBlockInterface {
return $this->theme; 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} * {@inheritdoc}
*/ */
@ -185,7 +109,7 @@ class CustomBlock extends ContentEntityBase implements CustomBlockInterface {
parent::preSave($storage_controller); parent::preSave($storage_controller);
// Before saving the custom block, set changed time. // Before saving the custom block, set changed time.
$this->changed->value = REQUEST_TIME; $this->set('changed', REQUEST_TIME);
} }
/** /**
@ -202,7 +126,7 @@ class CustomBlock extends ContentEntityBase implements CustomBlockInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getInstances() { 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 @@ class CustomBlock extends ContentEntityBase implements CustomBlockInterface {
elseif (isset($this->original) && (!isset($record->log) || $record->log === '')) { elseif (isset($this->original) && (!isset($record->log) || $record->log === '')) {
// If we are updating an existing custom_block without adding a new // 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. // 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 @@ class CustomBlock extends ContentEntityBase implements CustomBlockInterface {
return $this->get('changed')->value; 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;
}
} }

View File

@ -20,8 +20,8 @@ class CustomBlock extends DerivativeBase {
public function getDerivativeDefinitions(array $base_plugin_definition) { public function getDerivativeDefinitions(array $base_plugin_definition) {
$custom_blocks = entity_load_multiple('custom_block'); $custom_blocks = entity_load_multiple('custom_block');
foreach ($custom_blocks as $custom_block) { foreach ($custom_blocks as $custom_block) {
$this->derivatives[$custom_block->uuid->value] = $base_plugin_definition; $this->derivatives[$custom_block->uuid()] = $base_plugin_definition;
$this->derivatives[$custom_block->uuid->value]['admin_label'] = $custom_block->info->value; $this->derivatives[$custom_block->uuid()]['admin_label'] = $custom_block->label();
} }
return parent::getDerivativeDefinitions($base_plugin_definition); return parent::getDerivativeDefinitions($base_plugin_definition);
} }

View File

@ -48,9 +48,9 @@ class CustomBlockLoadHooksTest extends CustomBlockTestBase {
$custom_blocks = entity_load_multiple_by_properties('custom_block', array('type' => 'basic')); $custom_blocks = entity_load_multiple_by_properties('custom_block', array('type' => 'basic'));
$loaded_custom_block = end($custom_blocks); $loaded_custom_block = end($custom_blocks);
$this->assertEqual($loaded_custom_block->custom_block_test_loaded_ids, array( $this->assertEqual($loaded_custom_block->custom_block_test_loaded_ids, array(
$custom_block1->id->value, $custom_block1->id(),
$custom_block2->id->value, $custom_block2->id(),
$custom_block3->id->value $custom_block3->id(),
), 'hook_custom_block_load() received the correct list of custom_block IDs the first time it was called.'); ), '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.'); $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 @@ class CustomBlockLoadHooksTest extends CustomBlockTestBase {
$custom_blocks = entity_load_multiple('custom_block', \Drupal::entityQuery('custom_block')->execute(), TRUE); $custom_blocks = entity_load_multiple('custom_block', \Drupal::entityQuery('custom_block')->execute(), TRUE);
$loaded_custom_block = end($custom_blocks); $loaded_custom_block = end($custom_blocks);
$this->assertEqual($loaded_custom_block->custom_block_test_loaded_ids, array( $this->assertEqual($loaded_custom_block->custom_block_test_loaded_ids, array(
$custom_block1->id->value, $custom_block1->id(),
$custom_block2->id->value, $custom_block2->id(),
$custom_block3->id->value, $custom_block3->id(),
$custom_block4->id->value $custom_block4->id(),
), 'hook_custom_block_load() received the correct list of custom_block IDs the second time it was called.'); ), '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.'); $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.');
} }

View File

@ -48,16 +48,17 @@ class CustomBlockRevisionsTest extends CustomBlockTestBase {
$logs = array(); $logs = array();
// Get original block. // Get original block.
$blocks[] = $block->revision_id->value; $blocks[] = $block->getRevisionId();
$logs[] = ''; $logs[] = '';
// Create three revisions. // Create three revisions.
$revision_count = 3; $revision_count = 3;
for ($i = 0; $i < $revision_count; $i++) { for ($i = 0; $i < $revision_count; $i++) {
$block->setNewRevision(TRUE); $block->setNewRevision(TRUE);
$logs[] = $block->log->value = $this->randomName(32); $block->setRevisionLog($this->randomName(32));
$logs[] = $block->getRevisionLog();
$block->save(); $block->save();
$blocks[] = $block->revision_id->value; $blocks[] = $block->getRevisionId();
} }
$this->blocks = $blocks; $this->blocks = $blocks;
@ -75,8 +76,8 @@ class CustomBlockRevisionsTest extends CustomBlockTestBase {
// Confirm the correct revision text appears. // Confirm the correct revision text appears.
$loaded = entity_revision_load('custom_block', $revision_id); $loaded = entity_revision_load('custom_block', $revision_id);
// Verify log is the same. // Verify log is the same.
$this->assertEqual($loaded->log->value, $logs[$delta], format_string('Correct log message found for revision !revision', array( $this->assertEqual($loaded->getRevisionLog(), $logs[$delta], format_string('Correct log message found for revision !revision', array(
'!revision' => $loaded->revision_id->value '!revision' => $loaded->getRevisionId(),
))); )));
} }
@ -87,17 +88,17 @@ class CustomBlockRevisionsTest extends CustomBlockTestBase {
// This will create a new revision that is not "front facing". // This will create a new revision that is not "front facing".
// Save this as a non-default revision. // Save this as a non-default revision.
$loaded->setNewRevision(); $loaded->setNewRevision();
$loaded->isDefaultRevision = FALSE; $loaded->isDefaultRevision(FALSE);
$loaded->body = $this->randomName(8); $loaded->body = $this->randomName(8);
$loaded->save(); $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.'); $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 // Verify that the non-default revision id is greater than the default
// revision id. // revision id.
$default_revision = entity_load('custom_block', $loaded->id->value); $default_revision = entity_load('custom_block', $loaded->id());
$this->assertTrue($loaded->revision_id->value > $default_revision->revision_id->value, 'Revision id is greater than default revision id.'); $this->assertTrue($loaded->getRevisionId() > $default_revision->getRevisionId(), 'Revision id is greater than default revision id.');
} }
} }

View File

@ -60,7 +60,7 @@ class CustomBlockSaveTest extends CustomBlockTestBase {
$block->save(); $block->save();
// Verify that block_submit did not wipe the provided id. // 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. // Test the import saved.
$block_by_id = custom_block_load($test_id); $block_by_id = custom_block_load($test_id);
@ -82,7 +82,7 @@ class CustomBlockSaveTest extends CustomBlockTestBase {
$this->assertEqual($block->label(), 'test_changes', 'No changes have been determined.'); $this->assertEqual($block->label(), 'test_changes', 'No changes have been determined.');
// Apply changes. // Apply changes.
$block->info->value = 'updated'; $block->setInfo('updated');
$block->save(); $block->save();
// The hook implementations custom_block_test_custom_block_presave() and // The hook implementations custom_block_test_custom_block_presave() and
@ -92,7 +92,7 @@ class CustomBlockSaveTest extends CustomBlockTestBase {
$this->assertEqual($block->getChangedTime(), 979534800, 'Saving a custom block uses "changed" timestamp set in presave hook.'); $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. // 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.'); $this->assertEqual($block->label(), 'updated_presave', 'Static cache has been cleared.');
} }
@ -109,7 +109,7 @@ class CustomBlockSaveTest extends CustomBlockTestBase {
// custom_block_test_custom_block_insert() tiggers a save on insert if the // custom_block_test_custom_block_insert() tiggers a save on insert if the
// title equals 'new'. // title equals 'new'.
$block = $this->createCustomBlock('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.');
} }
} }

View File

@ -65,8 +65,8 @@ class PageEditTest extends CustomBlockTestBase {
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
// Ensure that the block revision has been created. // Ensure that the block revision has been created.
$revised_block = entity_load('custom_block', $block->id->value, TRUE); $revised_block = entity_load('custom_block', $block->id(), TRUE);
$this->assertNotIdentical($block->revision_id->value, $revised_block->revision_id->value, 'A new revision has been created.'); $this->assertNotIdentical($block->getRevisionId(), $revised_block->getRevisionId(), 'A new revision has been created.');
// Test deleting the block. // Test deleting the block.
$this->drupalGet("block/" . $revised_block->id()); $this->drupalGet("block/" . $revised_block->id());

View File

@ -40,13 +40,13 @@ function custom_block_test_custom_block_view(CustomBlock $custom_block, $view_mo
* Implements hook_custom_block_presave(). * Implements hook_custom_block_presave().
*/ */
function custom_block_test_custom_block_presave(CustomBlock $custom_block) { function custom_block_test_custom_block_presave(CustomBlock $custom_block) {
if ($custom_block->info->value == 'testing_custom_block_presave') { if ($custom_block->label() == 'testing_custom_block_presave') {
$custom_block->info->value .= '_presave'; $custom_block->setInfo($custom_block->label() .'_presave');
} }
// Determine changes. // Determine changes.
if (!empty($custom_block->original) && $custom_block->original->info->value == 'test_changes') { if (!empty($custom_block->original) && $custom_block->original->label() == 'test_changes') {
if ($custom_block->original->info->value != $custom_block->info->value) { if ($custom_block->original->label() != $custom_block->label()) {
$custom_block->info->value .= '_presave'; $custom_block->setInfo($custom_block->label() .'_presave');
// Drupal 1.0 release. // Drupal 1.0 release.
$custom_block->changed = 979534800; $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) { function custom_block_test_custom_block_update(CustomBlock $custom_block) {
// Determine changes on update. // Determine changes on update.
if (!empty($custom_block->original) && $custom_block->original->info->value == 'test_changes') { if (!empty($custom_block->original) && $custom_block->original->label() == 'test_changes') {
if ($custom_block->original->info->value != $custom_block->info->value) { if ($custom_block->original->label() != $custom_block->label()) {
$custom_block->info->value .= '_update'; $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) { function custom_block_test_custom_block_insert(CustomBlock $custom_block) {
// Set the custom_block title to the custom_block ID and save. // Set the custom_block title to the custom_block ID and save.
if ($custom_block->info->value == 'new') { if ($custom_block->label() == 'new') {
$custom_block->info->value = 'CustomBlock ' . $custom_block->id->value; $custom_block->setInfo('CustomBlock ' . $custom_block->id());
$custom_block->save(); $custom_block->save();
} }
if ($custom_block->info->value == 'fail_creation') { if ($custom_block->label() == 'fail_creation') {
throw new Exception('Test exception for rollback.'); throw new Exception('Test exception for rollback.');
} }
} }