Commit e05d622e authored by alexpott's avatar alexpott

Issue #2205215 by larowlan, jessebeach, dixon_: Fixed {comment} and...

Issue #2205215 by larowlan, jessebeach, dixon_: Fixed {comment} and {comment_entity_statistics} only support integer entity ids.
parent e0d17ee6
......@@ -264,8 +264,13 @@ function comment_field_config_delete(FieldConfigInterface $field) {
*/
function comment_field_config_insert(FieldConfigInterface $field) {
if ($field->getType() == 'comment') {
// Check that the target entity type uses an integer ID.
$entity_type_id = $field->getTargetEntityTypeId();
if (!_comment_entity_uses_integer_id($entity_type_id)) {
throw new \UnexpectedValueException('You cannot attach a comment field to an entity with a non-integer ID field');
}
// Delete all fields and displays attached to the comment bundle.
entity_invoke_bundle_hook('insert', 'comment', $field->getTargetEntityTypeId() . '__' . $field->getName());
entity_invoke_bundle_hook('insert', 'comment', $entity_type_id . '__' . $field->getName());
}
}
......@@ -812,6 +817,11 @@ function comment_form_field_ui_field_overview_form_alter(&$form, $form_state) {
if ($form['#entity_type'] == 'comment' && $request->attributes->has('commented_entity_type')) {
$form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($request->attributes->get('commented_entity_type'), $request->attributes->get('field_name'));
}
$entity_type_id = $form['#entity_type'];
if (!_comment_entity_uses_integer_id($entity_type_id)) {
// You cannot use comment fields on entity types with non-integer IDs.
unset($form['fields']['_add_new_field']['type']['#options']['comment']);
}
}
/**
......@@ -936,6 +946,24 @@ function comment_entity_predelete(EntityInterface $entity) {
}
}
/**
* Determines if an entity type is using an integer-based ID definition.
*
* @param string $entity_type_id
* The ID the represents the entity type.
*
* @return bool
* Returns TRUE if the entity type has an integer-based ID definition and
* FALSE otherwise.
*/
function _comment_entity_uses_integer_id($entity_type_id) {
$entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
$entity_type_id_key = $entity_type->getKey('id');
$field_definitions = \Drupal::entityManager()->getBaseFieldDefinitions($entity_type->id());
$entity_type_id_definition = $field_definitions[$entity_type_id_key];
return $entity_type_id_definition->getType() === 'integer';
}
/**
* Implements hook_node_update_index().
*/
......
......@@ -382,4 +382,20 @@ function testCommentFunctionality() {
$this->assertNoFieldByName('comment_body[0][value]', '', 'Comment field found.');
}
/**
* Tests comment fields are not available for entity types with string ids.
*/
public function testsStringIdEntities() {
// Create a bundle for entity_test.
entity_test_create_bundle('entity_test', 'Entity Test', 'entity_test_string_id');
$limited_user = $this->drupalCreateUser(array(
'administer entity_test_string_id fields',
));
$this->drupalLogin($limited_user);
// Visit the Field UI overview.
$this->drupalGet('entity_test_string_id/structure/entity_test/fields');
// Ensure field isn't shown for string ids.
$this->assertNoOption('edit-fields-add-new-field-type', 'comment');
}
}
<?php
/**
* @file
* Contains \Drupal\comment\Tests\CommentStringIdEntitiesTest.
*/
namespace Drupal\comment\Tests;
use Drupal\simpletest\KernelTestBase;
/**
* Tests that comment fields cannot be added to entities with non-integer IDs.
*/
class CommentStringIdEntitiesTest extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('comment', 'user', 'field', 'field_ui', 'entity_test');
public static function getInfo() {
return array(
'name' => 'Comments on Entity Types with string IDs',
'description' => 'Test that comment fields cannot be added to entities with non-integer IDs',
'group' => 'Comment',
);
}
public function setUp() {
parent::setUp();
$this->installEntitySchema('comment');
$this->installSchema('comment', array('comment_entity_statistics'));
}
/**
* Tests that comment fields cannot be added entities with non-integer IDs.
*/
public function testCommentFieldNonStringId() {
try {
$field = entity_create('field_config', array(
'name' => 'foo',
'entity_type' => 'entity_test_string_id',
'settings' => array(),
'type' => 'comment',
));
$field->save();
$this->fail('Did not throw an exception as expected.');
}
catch (\UnexpectedValueException $e) {
$this->pass('Exception thrown when trying to create comment field on Entity Type with string ID.');
}
}
}
<?php
/**
* @file
* Contains \Drupal\entity_test\Entity\EntityTestStringId.
*/
namespace Drupal\entity_test\Entity;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\Entity\EntityTypeInterface;
/**
* Defines a test entity class with a string ID.
*
* @ContentEntityType(
* id = "entity_test_string_id",
* label = @Translation("Test entity with string_id"),
* controllers = {
* "access" = "Drupal\entity_test\EntityTestAccessController",
* "form" = {
* "default" = "Drupal\entity_test\EntityTestForm"
* },
* "translation" = "Drupal\content_translation\ContentTranslationHandler"
* },
* base_table = "entity_test_string",
* fieldable = TRUE,
* field_cache = TRUE,
* entity_keys = {
* "id" = "id",
* "uuid" = "uuid",
* "bundle" = "type"
* },
* links = {
* "canonical" = "entity_test.render",
* "edit-form" = "entity_test.edit_entity_test_string_id",
* "admin-form" = "entity_test.admin_entity_test_string_id"
* }
* )
*/
class EntityTestStringId extends EntityTest {
/**
* {@inheritdoc}
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields['id'] = FieldDefinition::create('string')
->setLabel(t('ID'))
->setDescription(t('The ID of the test entity.'))
->setReadOnly(TRUE);
return $fields;
}
}
......@@ -22,6 +22,7 @@ class EntityTestRoutes {
*/
public function routes() {
$types = entity_test_entity_types();
$types[] = 'entity_test_string_id';
$routes = array();
foreach ($types as $entity_type) {
......
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