Skip to content
Snippets Groups Projects
Commit 156aea95 authored by Ahmet Burkan's avatar Ahmet Burkan Committed by Pravin Gaikwad
Browse files

Issue #3414301 by Rajeshreeputra, ankitv18, deepakkm: Fatal error when adding...

Issue #3414301 by Rajeshreeputra, ankitv18, deepakkm: Fatal error when adding a field to any entity type (e.g., basic page node) with GDPR module enabled on Drupal 10.2.1
parent 2a9d7859
No related branches found
No related tags found
3 merge requests!47Issue #3462672 "Impossible to dump",!44merge 3.0.x into 3.x,!32Fixed fatal error and made the code defensive.
Pipeline #226447 passed
......@@ -31,7 +31,12 @@ function gdpr_fields_form_field_config_edit_form_alter(&$form, FormStateInterfac
'#open' => TRUE,
];
GdprFieldSettingsForm::buildFormFields($form['field']['gdpr_fields'], $field->getTargetEntityTypeId(), $field->getTargetBundle(), $field->getName());
if (isset($form_state->getStorage()['field_config'])) {
GdprFieldSettingsForm::buildFormFields($form['field']['gdpr_fields'], $field->getTargetEntityTypeId(), $field->getTargetBundle(), $field->getName(), $form_state->getStorage()['field_config']);
}
else {
GdprFieldSettingsForm::buildFormFields($form['field']['gdpr_fields'], $field->getTargetEntityTypeId(), $field->getTargetBundle(), $field->getName());
}
$form['actions']['submit']['#submit'][] = 'gdpr_fields_form_field_config_edit_form_submit';
}
......
......@@ -42,7 +42,11 @@ class GdprFieldSettingsForm extends FormBase {
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(EntityFieldManagerInterface $entity_field_manager, EntityTypeManagerInterface $entityTypeManager, MessengerInterface $messenger) {
public function __construct(
EntityFieldManagerInterface $entity_field_manager,
EntityTypeManagerInterface $entityTypeManager,
MessengerInterface $messenger,
) {
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entityTypeManager;
$this->messenger = $messenger;
......@@ -235,12 +239,14 @@ class GdprFieldSettingsForm extends FormBase {
* Bundle.
* @param string $field_name
* Field.
* @param \Drupal\field\Entity\FieldConfig $field_config
* The field config object.
*
* @see gdpr_fields_form_field_config_edit_form_submit
*
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public static function buildFormFields(array &$form, $entity_type = NULL, $bundle_name = NULL, $field_name = NULL) {
public static function buildFormFields(array &$form, $entity_type = NULL, $bundle_name = NULL, $field_name = NULL, $field_config = NULL) {
$entityTypeManager = \Drupal::entityTypeManager();
$entityDefinition = $entityTypeManager->getDefinition($entity_type);
......@@ -262,6 +268,18 @@ class GdprFieldSettingsForm extends FormBase {
$anonymizerDefinitions = $anonymizerFactory->getDefinitions();
$fieldDefinition = $fieldManager->getFieldDefinitions($entity_type, $bundle_name)[$field_name];
// From D10.2 onwards field definition is not yet available,
// when we add the form elements in the field settings form.
// Set field definition from form_state if getFieldDefinitions() fails.
if (!$fieldDefinition && $field_config) {
$fieldDefinition = $field_config;
}
// Set warning that the GDPR fields couldn't be loaded.
if (!$fieldDefinition) {
\Drupal::messenger()->addWarning(t('Failed to add GDPR fields to the form.'));
}
$form['gdpr_enabled'] = [
'#type' => 'checkbox',
'#title' => t('This is a GDPR field'),
......@@ -278,7 +296,7 @@ class GdprFieldSettingsForm extends FormBase {
'#value' => $config->sarsFilename,
];
if ($fieldDefinition->getType() === 'entity_reference') {
if ($fieldDefinition && $fieldDefinition->getType() === 'entity_reference') {
$innerEntityType = $fieldDefinition->getSetting('target_type');
$innerEntityDefinition = $entityTypeManager->getDefinition($innerEntityType);
......@@ -375,7 +393,7 @@ class GdprFieldSettingsForm extends FormBase {
];
}
// Otherwise check if this can be removed.
elseif (!$config->propertyCanBeRemoved($fieldDefinition, $errorMessage)) {
elseif ($fieldDefinition && !$config->propertyCanBeRemoved($fieldDefinition, $errorMessage)) {
unset($form['gdpr_rtf']['#options']['remove']);
$form['gdpr_rtf_disabled'] = [
'#type' => 'item',
......@@ -385,7 +403,7 @@ class GdprFieldSettingsForm extends FormBase {
}
// Force removal to 'no' for computed properties.
if ($fieldDefinition->isComputed()) {
if ($fieldDefinition && $fieldDefinition->isComputed()) {
$form['gdpr_rtf']['#default_value'] = 'no';
$form['gdpr_rtf']['#disabled'] = TRUE;
$form['gdpr_rtf']['#description'] = t('*This is a computed field and cannot be removed.');
......
<?php
namespace Drupal\Tests\gdpr_fields\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
/**
* Tests GDPR Fields and settings.
*
* @group gdpr
*/
class GdprFieldsTest extends BrowserTestBase {
use NodeCreationTrait;
use ContentTypeCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'gdpr',
'gdpr_fields',
'gdpr_tasks',
'gdpr_consent',
'anonymizer',
'node',
'field_ui',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
// Create the users used for the tests.
$this->drupalLogin($this->rootUser);
}
/**
* {@inheritdoc}
*/
public function testGdprFieldsSettings() {
// Create a content type.
$this->createContentType(['type' => 'page']);
// Add field to content type and enable GDPR settings.
FieldStorageConfig::create([
'field_name' => 'field_gdpr',
'entity_type' => 'node',
'type' => 'string',
'cardinality' => 1,
'locked' => FALSE,
'indexes' => [],
'settings' => [
'max_length' => 255,
'case_sensitive' => FALSE,
'is_ascii' => FALSE,
],
])->save();
FieldConfig::create([
'field_name' => 'field_gdpr',
'entity_type' => 'node',
'label' => 'GDPR Settings',
'bundle' => 'page',
'description' => '',
'required' => FALSE,
'settings' => [
'gdpr_enabled' => TRUE,
],
])->save();
// Enable GDPR settings.
$this->drupalGet('/admin/structure/types/manage/page/fields/node.page.field_gdpr');
/** @var \Drupal\FunctionalJavascriptTests\JSWebAssert $assertSession */
$assertSession = $this->assertSession();
$assertSession->statusCodeEquals(200);
$assertSession->fieldExists('This is a GDPR field')->check();
$this->getSession()->getPage()->pressButton('Save');
// Verify field is GDPR enabled.
$this->assertTrue($this->config('gdpr_fields.gdpr_fields_config.node')->get('bundles.page.field_gdpr.enabled'), 'The GDPR field has GDPR enabled.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment