Skip to content
Snippets Groups Projects
Unverified Commit f3227f46 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2608750 by phenaproxima, shriaas2898, KapilV, mohit_aghera, RenatoG,...

Issue #2608750 by phenaproxima, shriaas2898, KapilV, mohit_aghera, RenatoG, akhoury, guilhermevp, praveenmoses61, sulfikar_s, Abhijith S, larowlan, pameeela, Sid_omp: Exception when creating an entity reference field targeting an entity type without an ID

(cherry picked from commit 3eebb1b1)
parent aed9f638
No related branches found
No related tags found
20 merge requests!10011Issue #3200534 by quietone, longwave, Kristen Pol: Use dataprovider for...,!3134Issue #3222236: Lighthouse SEO: Uncrawlable Link a#main-content,!2571Issue #3000717: Missing mapping for "nodereference_url" widget,!2521Issue #3185775: Place Views preview on the side on large monitors,!1803Issue #2329253: Allow the ChangedItem to skip updating when synchronizing (f.e. when migrating),!1603Issue #3231707: mxr576's core patch playground,!1479Issue #3250298: Return empty string "" with JSON Serializer instead of FALSE,!1478Issue #3250298: Return empty string "" with JSON Serializer instead of FALSE,!1203Issue #3236191 Wrong group exposed form widgets and multiple selection error.,!1076Issue #2903336 Added node context for tokens.,!1015Issue #3226944: REST's Request handler doesn't resolve $data argument for put method,!810Issue #3219541: Remove redudante call $this->requestStack->getCurrentRequest() in FormBuilder::buildForm,!803Issue #3219167: webchick test issue of all time turbo edition super star 20000 NG,!740Issue #3216088: Update Symfony 5 components to 5.3,!657Draft: Remove alpha-stability experimental modules and themes from 9.2.x only, prior...,!577Issue #3209779: Create new database storage for the tracker module,!526Update block module to use once library,!516Issue #3207782: Figure out BC for jquery once by @drupal/once,!400Issue #3051766: Deprecate and replace jQuery Joyride (for tours),!35Issue #3164686 WebAssert::addressEquals() and AssertLegacyTrait::assertUrl() fail to check the querystring
......@@ -10,6 +10,7 @@
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\TypedData\EntityDataDefinition;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldException;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface;
......@@ -67,6 +68,12 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
$settings = $field_definition->getSettings();
$target_type_info = \Drupal::entityTypeManager()->getDefinition($settings['target_type']);
// If the target entity type doesn't have an ID key, we cannot determine
// the target_id data type.
if (!$target_type_info->hasKey('id')) {
throw new FieldException('Entity type "' . $target_type_info->id() . '" has no ID key and cannot be targeted by entity reference field "' . $field_definition->getName() . '"');
}
$target_id_data_type = 'string';
if ($target_type_info->entityClassImplements(FieldableEntityInterface::class)) {
$id_definition = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($settings['target_type'])[$target_type_info->getKey('id')];
......@@ -356,13 +363,23 @@ public function storageSettingsForm(array &$form, FormStateInterface $form_state
$element['target_type'] = [
'#type' => 'select',
'#title' => t('Type of item to reference'),
'#options' => \Drupal::service('entity_type.repository')->getEntityTypeLabels(TRUE),
'#default_value' => $this->getSetting('target_type'),
'#required' => TRUE,
'#disabled' => $has_data,
'#size' => 1,
];
// Only allow the field to target entity types that have an ID key. This
// is enforced in ::propertyDefinitions().
$entity_type_manager = \Drupal::entityTypeManager();
$filter = function (string $entity_type_id) use ($entity_type_manager): bool {
return $entity_type_manager->getDefinition($entity_type_id)
->hasKey('id');
};
$options = \Drupal::service('entity_type.repository')->getEntityTypeLabels(TRUE);
foreach ($options as $group_name => $group) {
$element['target_type']['#options'][$group_name] = array_filter($group, $filter, ARRAY_FILTER_USE_KEY);
}
return $element;
}
......@@ -618,8 +635,9 @@ public function getSettableOptions(AccountInterface $account = NULL) {
}
/**
* Render API callback: Processes the field settings form and allows access to
* the form state.
* Render API callback: Processes the field settings form.
*
* Allows access to the form state.
*
* @see static::fieldSettingsForm()
*/
......
<?php
namespace Drupal\Tests\system\Functional\Entity;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
/**
* Tests creating entity reference fields in the UI.
*
* @group entity
*/
class EntityReferenceFieldCreationTest extends BrowserTestBase {
use EntityReferenceTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['entity_test', 'node', 'field_ui'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests that entity reference fields cannot target entity types without IDs.
*/
public function testAddReferenceFieldTargetingEntityTypeWithoutId() {
$this->drupalLogin($this->rootUser);
$node_type = $this->drupalCreateContentType()->id();
// Entity types without an ID key should not be presented as options when
// creating an entity reference field in the UI.
$this->drupalGet("/admin/structure/types/manage/$node_type/fields/add-field");
$edit = [
'new_storage_type' => 'entity_reference',
'label' => 'Test Field',
'field_name' => 'test_reference_field',
];
$this->submitForm($edit, 'Save and continue');
$this->assertSession()->optionNotExists('settings[target_type]', 'entity_test_no_id');
// Trying to do it programmatically should raise an exception.
$this->expectException('\Drupal\Core\Field\FieldException');
$this->expectExceptionMessage('Entity type "entity_test_no_id" has no ID key and cannot be targeted by entity reference field "test_reference_field"');
$this->createEntityReferenceField('node', $node_type, 'test_reference_field', 'Test Field', 'entity_test_no_id');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment