Commit f2fdaeba authored by alexpott's avatar alexpott

Issue #2450153 by josephleon, damiankloip, joshi.rohit100, anksy: Add a...

Issue #2450153 by josephleon, damiankloip, joshi.rohit100, anksy: Add a default_formatter to UUIDs fields
parent db8fa578
......@@ -18,7 +18,8 @@
* id = "uuid",
* label = @Translation("UUID"),
* description = @Translation("An entity field containing a UUID."),
* no_ui = TRUE
* no_ui = TRUE,
* default_formatter = "string"
* )
*/
class UuidItem extends StringItem {
......
......@@ -66,7 +66,7 @@ public function testCommentFields() {
// $this->assertFieldAccess('comment', 'cid', $comment->id());
// $this->assertFieldAccess('comment', 'cid', $comment_anonymous->id());
// $this->assertFieldAccess('comment', 'uuid', $comment->uuid());
$this->assertFieldAccess('comment', 'uuid', $comment->uuid());
// $this->assertFieldAccess('comment', 'subject', 'My comment title');
// $this->assertFieldAccess('comment', 'subject', 'Anonymous comment title');
$this->assertFieldAccess('comment', 'name', 'anonymous');
......
<?php
/**
* @file
* Contains \Drupal\field\Tests\String\UuidFormatterTest.
*/
namespace Drupal\field\Tests\String;
use Drupal\simpletest\KernelTestBase;
use Drupal\entity_test\Entity\EntityTest;
/**
* Tests the output of a UUID field.
*
* @group field
*/
class UuidFormatterTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['field', 'entity_test', 'system', 'user'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(['system', 'field']);
$this->installSchema('system', 'router');
\Drupal::service('router.builder')->rebuild();
$this->installEntitySchema('entity_test');
}
/**
* Tests string formatter output.
*/
public function testUuidStringFormatter() {
$entity = EntityTest::create([]);
$entity->save();
$uuid_field = $entity->get('uuid');
$render_array = $uuid_field->view([]);
$this->assertIdentical($render_array[0]['#markup'], $entity->uuid(), 'The rendered UUID matches the entity UUID.');
$render_array = $uuid_field->view(['settings' => ['link_to_entity' => TRUE]]);
$this->assertIdentical($render_array[0]['#type'], 'link');
$this->assertIdentical($render_array[0]['#title'], $entity->uuid());
$this->assertIdentical($render_array[0]['#url']->toString(), $entity->url());
}
}
......@@ -60,7 +60,7 @@ public function testFileFields() {
// @todo Expand the test coverage in https://www.drupal.org/node/2464635
$this->assertFieldAccess('file', 'fid', $file->id());
// $this->assertFieldAccess('file', 'uuid', $file->uuid());
$this->assertFieldAccess('file', 'uuid', $file->uuid());
$this->assertFieldAccess('file', 'langcode', $file->language()->getName());
$this->assertFieldAccess('file', 'uid', 'test user');
$this->assertFieldAccess('file', 'filename', $file->getFilename());
......
......@@ -59,7 +59,7 @@ public function testNodeFields() {
// @todo Expand the test coverage in https://www.drupal.org/node/2464635
$this->assertFieldAccess('node', 'nid', $node->id());
// $this->assertFieldAccess('node', 'uuid', $node->uuid());
$this->assertFieldAccess('node', 'uuid', $node->uuid());
$this->assertFieldAccess('node', 'vid', $node->id());
$this->assertFieldAccess('node', 'type', $node->type->entity->label());
$this->assertFieldAccess('node', 'langcode', $node->language()->getName());
......
......@@ -57,7 +57,7 @@ public function testUserFields() {
// @todo Expand the test coverage in https://www.drupal.org/node/2464635
// $this->assertFieldAccess('user', 'uid', $user->id());
// $this->assertFieldAccess('user', 'uuid', $user->uuid());
$this->assertFieldAccess('user', 'uuid', $user->uuid());
$this->assertFieldAccess('user', 'langcode', $user->language()->getName());
$this->assertFieldAccess('user', 'preferred_langcode', 'Spanish');
$this->assertFieldAccess('user', 'preferred_admin_langcode', 'French');
......
......@@ -90,7 +90,10 @@ protected function assertFieldAccess($entity_type_id, $field_name, $field_conten
$entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
$view_id = $this->randomMachineName();
$base_table = $entity_type->getDataTable() ?: $entity_type->getBaseTable();
$data_table = $entity_type->getDataTable();
// Use the data table as long as the field is not 'uuid'. This is the only
// column that can only be obtained from the base table.
$base_table = ($data_table && ($field_name !== 'uuid')) ? $data_table : $entity_type->getBaseTable();
$entity = View::create([
'id' => $view_id,
'base_table' => $base_table,
......
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