diff --git a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php index a60122fb77e5b630d3c420016aa1e55400f44a95..2a8bb1436da910503f2d257cee168c63b496d2e8 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityDatabaseStorage.php @@ -342,6 +342,10 @@ public function getTableMapping() { * {@inheritdoc} */ protected function doLoadMultiple(array $ids = NULL) { + if (!empty($ids)) { + $ids = $this->cleanIds($ids); + } + // Build and execute the query. $records = $this ->buildQuery($ids) @@ -351,6 +355,39 @@ protected function doLoadMultiple(array $ids = NULL) { return $this->mapFromStorageRecords($records); } + /** + * Sanitizes the entity IDs to the correct data type. + * + * The identifier sanitization provided by this method has been introduced + * as Drupal used to rely on the database to facilitate this, which worked + * correctly with MySQL but led to errors with other DBMS such as PostgeSQL. + * + * @param array $ids + * The entity IDs to verify. + * @return array + * The sanitized list of entity IDs. + */ + protected function cleanIds(array $ids) { + $keys = $this->entityType->getKeys(); + $bundle = $keys['bundle'] ? $keys['bundle'] : $this->entityTypeId; + + $definitions = $this->entityManager->getFieldDefinitions($this->entityTypeId, $bundle); + $info = $this->entityManager->getDefinition($this->entityTypeId); + $id_definition = $definitions[$info->getKey('id')]; + + switch ($id_definition->getType()) { + case 'integer': + $ids = array_filter($ids, 'is_numeric'); + $ids = array_map('intval', $ids); + break; + case 'string': + $ids = array_filter($ids, 'is_string'); + $ids = array_map('strval', $ids); + break; + } + return $ids; + } + /** * Maps from storage records to entity objects. * diff --git a/core/modules/system/src/Tests/Entity/EntityViewControllerTest.php b/core/modules/system/src/Tests/Entity/EntityViewControllerTest.php index 4faec44b5673d1b360a545800493dac14008fd31..19515d03c0b37b1823fb9557b0b33cc2e17e4275 100644 --- a/core/modules/system/src/Tests/Entity/EntityViewControllerTest.php +++ b/core/modules/system/src/Tests/Entity/EntityViewControllerTest.php @@ -76,6 +76,11 @@ function testEntityViewController() { $this->assertRaw($entity->label()); $this->assertRaw('full'); } + + // As entity_test IDs must be integers, make sure requests for non-integer + // IDs return a page not found error. + $this->drupalGet('entity_test/invalid'); + $this->assertResponse(404); } /**