Commit 4338c8bb authored by alexpott's avatar alexpott

Issue #2312153 by plach: Refactor block_content entity schema to multilingual.

parent 52f02da5
......@@ -86,7 +86,7 @@ public function viewMultiple(array $entities = array(), $view_mode = 'full', $la
'entity_view',
'block',
$entity->id(),
$entity->language()->getId(),
$this->languageManager->getCurrentLanguage()->getId(),
// Blocks are always rendered in a "per theme" cache context.
'cache_context.theme',
);
......
<?php
/**
* @file
* Install, update and uninstall functions for the custom block module.
*/
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Implements hook_schema().
*/
function block_content_schema() {
$schema = array();
$schema['block_content'] = array(
'description' => 'Stores contents of custom-made blocks.',
'fields' => array(
'id' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => "The block's {block_content}.id.",
),
'uuid' => array(
'description' => 'Unique Key: Universally unique identifier for this entity.',
'type' => 'varchar',
'length' => 128,
'not null' => FALSE,
),
'info' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'default' => '',
'description' => 'Block description.',
),
// Defaults to NULL in order to avoid a brief period of potential
// deadlocks on the index.
'revision_id' => array(
'description' => 'The current {block_content_revision}.revision_id version identifier.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => NULL,
),
'type' => array(
'description' => 'The type of this custom block.',
'type' => 'varchar',
'length' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
'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',
'length' => 12,
'not null' => TRUE,
'default' => '',
),
),
'primary key' => array('id'),
'indexes' => array(
'block_content_type' => array('type'),
),
'unique keys' => array(
'revision_id' => array('revision_id'),
'uuid' => array('uuid'),
'info' => array('info'),
),
'foreign keys' => array(
'block_content_revision' => array(
'table' => 'block_content_revision',
'columns' => array('revision_id' => 'revision_id'),
),
),
);
$schema['block_content_revision'] = array(
'description' => 'Stores contents of custom-made blocks.',
'fields' => array(
'id' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => "The block's {block_content}.id.",
),
// Defaults to NULL in order to avoid a brief period of potential
// deadlocks on the index.
'revision_id' => array(
'description' => 'The current version identifier.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'revision_log' => array(
'description' => 'The log entry explaining the changes in this version.',
'type' => 'text',
'not null' => FALSE,
'size' => 'big',
),
'info' => array(
'type' => 'varchar',
'length' => 128,
'not null' => TRUE,
'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'),
);
return $schema;
}
......@@ -22,10 +22,10 @@ public function getSchema() {
// Marking the respective fields as NOT NULL makes the indexes more
// performant.
$schema['block_content']['fields']['info']['not null'] = TRUE;
$schema['block_content_field_data']['fields']['info']['not null'] = TRUE;
$schema['block_content']['unique keys'] += array(
'block_content__info' => array('info'),
$schema['block_content_field_data']['unique keys'] += array(
'block_content__info' => array('info', 'langcode'),
);
return $schema;
......
......@@ -36,6 +36,7 @@
* admin_permission = "administer blocks",
* base_table = "block_content",
* revision_table = "block_content_revision",
* data_table = "block_content_field_data",
* links = {
* "canonical" = "block_content.edit",
* "delete-form" = "block_content.delete",
......@@ -156,12 +157,14 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields['langcode'] = FieldDefinition::create('language')
->setLabel(t('Language code'))
->setDescription(t('The custom block language code.'));
->setDescription(t('The custom block language code.'))
->setRevisionable(TRUE);
$fields['info'] = FieldDefinition::create('string')
->setLabel(t('Block description'))
->setDescription(t('A brief description of your block.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setRequired(TRUE)
->setDisplayOptions('form', array(
'type' => 'string',
......
......@@ -129,7 +129,7 @@ public function testFailedBlockCreation() {
if (Database::getConnection()->supportsTransactions()) {
// Check that the block does not exist in the database.
$id = db_select('block_content', 'b')
$id = db_select('block_content_field_data', 'b')
->fields('b', array('id'))
->condition('info', 'fail_creation')
->execute()
......@@ -138,7 +138,7 @@ public function testFailedBlockCreation() {
}
else {
// Check that the block exists in the database.
$id = db_select('block_content', 'b')
$id = db_select('block_content_field_data', 'b')
->fields('b', array('id'))
->condition('info', 'fail_creation')
->execute()
......
......@@ -7,8 +7,8 @@
namespace Drupal\block_content\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\content_translation\Tests\ContentTranslationUITest;
use Drupal\block_content\Entity\BlockContent;
/**
* Tests the node translation UI.
......@@ -17,11 +17,6 @@
*/
class BlockContentTranslationUITest extends ContentTranslationUITest {
/**
* The name of the test block.
*/
protected $name;
/**
* Modules to enable.
*
......@@ -41,7 +36,6 @@ class BlockContentTranslationUITest extends ContentTranslationUITest {
public function setUp() {
$this->entityTypeId = 'block_content';
$this->bundle = 'basic';
$this->name = drupal_strtolower($this->randomName());
$this->testLanguageSelector = FALSE;
parent::setUp();
}
......@@ -87,7 +81,7 @@ protected function createBlockContent($title = FALSE, $bundle = FALSE) {
* Overrides \Drupal\content_translation\Tests\ContentTranslationUITest::getNewEntityValues().
*/
protected function getNewEntityValues($langcode) {
return array('info' => $this->name) + parent::getNewEntityValues($langcode);
return array('info' => Unicode::strtolower($this->randomName())) + parent::getNewEntityValues($langcode);
}
/**
......@@ -104,6 +98,32 @@ protected function getEditValues($values, $langcode, $new = FALSE) {
return $edit;
}
/**
* {@inheritdoc}
*/
protected function doTestBasicTranslation() {
parent::doTestBasicTranslation();
// Ensure that a block translation can be created using the same description
// as in the original language.
$default_langcode = $this->langcodes[0];
$values = $this->getNewEntityValues($default_langcode);
$storage = \Drupal::entityManager()->getStorage($this->entityTypeId);
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
$entity = $storage->create(array('type' => 'basic') + $values);
$entity->save();
$entity->addTranslation('it', $values);
try {
$message = 'Blocks can have translations with the same "info" value.';
$entity->save();
$this->pass($message);
}
catch (\Exception $e) {
$this->fail($message);
}
}
/**
* Test that no metadata is stored for a disabled bundle.
*/
......
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