diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 1834e55..ec891e3 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -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(t('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']); + } } /** @@ -937,6 +947,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(). */ function comment_node_update_index(EntityInterface $node, $langcode) { diff --git a/core/modules/comment/src/Tests/CommentNonNodeTest.php b/core/modules/comment/src/Tests/CommentNonNodeTest.php index b5bb317..8f53832 100644 --- a/core/modules/comment/src/Tests/CommentNonNodeTest.php +++ b/core/modules/comment/src/Tests/CommentNonNodeTest.php @@ -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'); + } + } diff --git a/core/modules/comment/src/Tests/CommentStringIdEntitiesTest.php b/core/modules/comment/src/Tests/CommentStringIdEntitiesTest.php new file mode 100644 index 0000000..014b3b1 --- /dev/null +++ b/core/modules/comment/src/Tests/CommentStringIdEntitiesTest.php @@ -0,0 +1,57 @@ + '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.'); + } + } + +} diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestStringId.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestStringId.php new file mode 100644 index 0000000..627d3a5 --- /dev/null +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestStringId.php @@ -0,0 +1,54 @@ +setLabel(t('ID')) + ->setDescription(t('The ID of the test entity.')) + ->setReadOnly(TRUE); + return $fields; + } + +} diff --git a/core/modules/system/tests/modules/entity_test/src/Routing/EntityTestRoutes.php b/core/modules/system/tests/modules/entity_test/src/Routing/EntityTestRoutes.php index 7f608e6..ccbc414 100644 --- a/core/modules/system/tests/modules/entity_test/src/Routing/EntityTestRoutes.php +++ b/core/modules/system/tests/modules/entity_test/src/Routing/EntityTestRoutes.php @@ -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) {