diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index b09bfa8..71bb768 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -262,6 +262,15 @@ 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(); + $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]; + if ($entity_type_id_definition->getType() != 'integer') { + 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()); } @@ -810,6 +819,15 @@ 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']; + $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]; + if ($entity_type_id_definition->getType() != 'integer') { + // You cannot use comment fields on entity types with non-integer IDs. + unset($form['fields']['_add_new_field']['type']['#options']['comment']); + } } /** 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/src/Tests/Entity/EntityApiTest.php b/core/modules/system/src/Tests/Entity/EntityApiTest.php index caa7bb8..711a7a0 100644 --- a/core/modules/system/src/Tests/Entity/EntityApiTest.php +++ b/core/modules/system/src/Tests/Entity/EntityApiTest.php @@ -29,6 +29,7 @@ public function setUp() { $this->installEntitySchema('entity_test_rev'); $this->installEntitySchema('entity_test_mul'); $this->installEntitySchema('entity_test_mulrev'); + $this->installEntitySchema('entity_test_string_id'); } /** diff --git a/core/modules/system/src/Tests/Entity/EntityFieldTest.php b/core/modules/system/src/Tests/Entity/EntityFieldTest.php index 2091f0c..1cebff4 100644 --- a/core/modules/system/src/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/src/Tests/Entity/EntityFieldTest.php @@ -7,6 +7,8 @@ namespace Drupal\system\Tests\Entity; +use Drupal\entity_test\Entity\EntityTestStringId; + use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Field\FieldDefinition; use Drupal\Core\Field\FieldDefinitionInterface; @@ -48,6 +50,7 @@ public function setUp() { $this->installEntitySchema('entity_test_rev'); $this->installEntitySchema('entity_test_mul'); $this->installEntitySchema('entity_test_mulrev'); + $this->installEntitySchema('entity_test_string_id'); // Create the test field. entity_test_install(); @@ -322,7 +325,12 @@ protected function assertSave($entity_type) { $this->assertTrue((bool) $entity->id(), format_string('%entity_type: Entity loaded.', array('%entity_type' => $entity_type))); // Access the name field. - $this->assertEqual(1, $entity->id->value, format_string('%entity_type: ID value can be read.', array('%entity_type' => $entity_type))); + if ($entity_type == 'entity_test_string_id') { + $this->assertTrue(is_string($entity->id->value), format_string('%entity_type: ID value can be read.', array('%entity_type' => $entity_type))); + } + else { + $this->assertEqual(1, $entity->id->value, format_string('%entity_type: ID value can be read.', array('%entity_type' => $entity_type))); + } $this->assertTrue(is_string($entity->uuid->value), format_string('%entity_type: UUID value can be read.', array('%entity_type' => $entity_type))); $this->assertEqual(Language::LANGCODE_NOT_SPECIFIED, $entity->langcode->value, format_string('%entity_type: Language code can be read.', array('%entity_type' => $entity_type))); $this->assertEqual(\Drupal::languageManager()->getLanguage(Language::LANGCODE_NOT_SPECIFIED), $entity->langcode->language, format_string('%entity_type: Language object can be read.', array('%entity_type' => $entity_type))); @@ -507,6 +515,24 @@ protected function assertDataStructureInterfaces($entity_type) { // Field format. NULL, ); + + // The entity id and the revision id of entity_test_string_id will have to + // be added in here as well. + if ($entity_type == 'entity_test_string_id') { + $target_strings = array( + NULL, + $entity->uuid->value, + Language::LANGCODE_NOT_SPECIFIED, + $this->entity_name, + // Bundle name. + $entity->bundle(), + NULL, + $this->entity_field_text, + // Field format. + NULL, + ); + } + $this->assertEqual($strings, $target_strings, format_string('%entity_type: All contained strings found.', array('%entity_type' => $entity_type))); } diff --git a/core/modules/system/src/Tests/Entity/EntityLanguageTestBase.php b/core/modules/system/src/Tests/Entity/EntityLanguageTestBase.php index c76fc97..9197154 100644 --- a/core/modules/system/src/Tests/Entity/EntityLanguageTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityLanguageTestBase.php @@ -53,6 +53,7 @@ function setUp() { $this->installEntitySchema('entity_test_rev'); $this->installEntitySchema('entity_test_mul'); $this->installEntitySchema('entity_test_mulrev'); + $this->installEntitySchema('entity_test_string_id'); $this->installConfig(array('language')); diff --git a/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php b/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php index b64ec89..15b10c3 100644 --- a/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php +++ b/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php @@ -85,7 +85,7 @@ protected function assertRevisions($entity_type) { $revision_ids[] = $entity->revision_id->value; // Check that the fields and properties contain new content. - $this->assertTrue($entity->revision_id->value > $legacy_revision_id, format_string('%entity_type: Revision ID changed.', array('%entity_type' => $entity_type))); + $this->assertNotEqual($entity->revision_id->value, $legacy_revision_id, format_string('%entity_type: Revision ID changed.', array('%entity_type' => $entity_type))); $this->assertNotEqual($entity->name->value, $legacy_name, format_string('%entity_type: Name changed.', array('%entity_type' => $entity_type))); $this->assertNotEqual($entity->field_test_text->value, $legacy_text, format_string('%entity_type: Text changed.', array('%entity_type' => $entity_type))); } diff --git a/core/modules/system/src/Tests/Entity/EntityUUIDTest.php b/core/modules/system/src/Tests/Entity/EntityUUIDTest.php index a0ec726..39541d6 100644 --- a/core/modules/system/src/Tests/Entity/EntityUUIDTest.php +++ b/core/modules/system/src/Tests/Entity/EntityUUIDTest.php @@ -26,6 +26,7 @@ public function setUp() { $this->installEntitySchema('entity_test_rev'); $this->installEntitySchema('entity_test_mul'); $this->installEntitySchema('entity_test_mulrev'); + $this->installEntitySchema('entity_test_string_id'); } /** diff --git a/core/modules/system/src/Tests/Entity/EntityValidationTest.php b/core/modules/system/src/Tests/Entity/EntityValidationTest.php index dceab23..fa21882 100644 --- a/core/modules/system/src/Tests/Entity/EntityValidationTest.php +++ b/core/modules/system/src/Tests/Entity/EntityValidationTest.php @@ -36,6 +36,7 @@ public function setUp() { $this->installEntitySchema('entity_test_rev'); $this->installEntitySchema('entity_test_mul'); $this->installEntitySchema('entity_test_mulrev'); + $this->installEntitySchema('entity_test_string_id'); // Create the test field. entity_test_install(); diff --git a/core/modules/system/tests/modules/entity_test/entity_test.install b/core/modules/system/tests/modules/entity_test/entity_test.install index 015b067..fc656a9 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.install +++ b/core/modules/system/tests/modules/entity_test/entity_test.install @@ -14,6 +14,7 @@ function entity_test_install() { 'entity_test_rev', 'entity_test_mul', 'entity_test_mulrev', + 'entity_test_string_id', ); foreach ($entity_types as $entity_type) { // Auto-create fields for testing. diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index 6c3a078..f91279c 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -55,6 +55,7 @@ function entity_test_entity_types($filter = NULL) { $types[] = 'entity_test_rev'; } $types[] = 'entity_test_mulrev'; + $types[] = 'entity_test_string_id'; return array_combine($types, $types); } diff --git a/core/modules/system/tests/modules/entity_test/entity_test.routing.yml b/core/modules/system/tests/modules/entity_test/entity_test.routing.yml index 38b0c44..8e64681 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.routing.yml +++ b/core/modules/system/tests/modules/entity_test/entity_test.routing.yml @@ -34,6 +34,13 @@ entity_test.delete_entity_test_rev: requirements: _access: 'TRUE' +entity_test.delete_entity_test_string_id: + path: '/entity_test/delete/entity_test_string_id/{entity_test_string_id}' + defaults: + _entity_form: entity_test_string_id.delete + requirements: + _access: 'TRUE' + entity_test.render_options: path: '/entity_test_converter/{foo}' options: 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..6a9585d --- /dev/null +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestStringId.php @@ -0,0 +1,84 @@ +isNew()) { + $this->set('id', substr(md5(rand()), 0, 10)); + } + if ($this->isNewRevision()) { + $this->set('revision_id', substr(md5(rand()), 0, 10)); + } + } + + /** + * {@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); + + $fields['revision_id'] = FieldDefinition::create('string') + ->setLabel(t('Revision ID')) + ->setDescription(t('The version id of the test entity.')); + + $fields['langcode']->setRevisionable(TRUE); + $fields['name']->setRevisionable(TRUE); + $fields['user_id']->setRevisionable(TRUE); + + return $fields; + } + +}