Incompatibility with Data field field type

Created on 17 April 2025, 4 months ago

Problem/Motivation

The module is incompatible with Data field .
When both modules are enabled, and upon visiting the settings form for Field Encryption, an error occurs due to the way the "Default properties" element is constructed.

\Drupal\field_encrypt\Form\SettingsForm::buildForm() calls BaseFieldDefinition::create($type) which in turn calls \Drupal\datafield\Plugin\Field\FieldType\DataFieldItem::propertyDefinitions().
DataFieldItem::propertyDefinitions() relies upon the "field_name" property being set in the definition array.

Steps to reproduce

  1. Install and enable both the Data field and Field Encryption modules
  2. Visit /admin/config/system/field-encrypt
  3. This results in a "Undefined array key "field_name""

Proposed resolution

I am not sure what the best way to deal with the problem would be. I don't know how much work would be needed to make Field Encryption work with the "data_field" field type, or if it would even be possible due to the dynamic nature of the properties on that field type.

Maybe add some "defensive code" to at least avoid this kind of error, and document this incompatibility somewhere ?

🐛 Bug report
Status

Active

Version

4.0

Component

Code

Created by

🇫🇷France arousseau

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @arousseau
  • 🇮🇳India Ishani Patel

    Hello,
    I've followed the steps and am facing the same issue.

  • 🇯🇵Japan ptmkenny

    Thanks for reporting this.

    The BaseFieldDefinition::create($type) is this one in SettingsForm.php?

          foreach ($field_types as $name => $field_type) {
            // Special handling for preconfigured definitions.
            // @see \Drupal\Core\Field\FieldTypePluginManager::getUiDefinitions()
            $type = str_starts_with($name, 'field_ui:') ? $field_type['id'] : $name;
            $field_definition = BaseFieldDefinition::create($type);

    We could probably follow the comment here and do something closer to what getUiDefinitions does in FieldTypePluginManager:

      /**
       * {@inheritdoc}
       */
      public function getUiDefinitions() {
        $definitions = $this->getDefinitions();
    
        // Filter out definitions that can not be configured in Field UI.
        $definitions = array_filter($definitions, function ($definition) {
          return empty($definition['no_ui']);
        });
    
        // Add preconfigured definitions.
        foreach ($definitions as $id => $definition) {
          if (is_subclass_of($definition['class'], '\Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface')) {
            foreach ($this->getPreconfiguredOptions($definition['id']) as $key => $option) {
              $definitions["field_ui:$id:$key"] = array_intersect_key(
                $option,
                ['label' => 0, 'category' => 1, 'weight' => 1, 'description' => 0]
              ) + $definition;
            }
          }
        }
    
        return $definitions;
      }
    

    Also, I have documented that this module is not compatible with Data Field on the project page.

  • 🇯🇵Japan ptmkenny

    Ok, I looked into this more, and I think this may be a problem with the Data Field module, not Field Encrypt.

          foreach ($field_types as $name => $field_type) {
            // Special handling for preconfigured definitions.
            // @see \Drupal\Core\Field\FieldTypePluginManager::getUiDefinitions()
            $type = str_starts_with($name, 'field_ui:') ? $field_type['id'] : $name;
            $field_definition = BaseFieldDefinition::create($type);
            $definitions = $field_definition->getPropertyDefinitions();
    

    In this code, we are pulling in all the field types, including those with PreconfiguredFieldUiOptionsInterface. These preconfigured definitions do NOT include a name property, so the "special handling" gets the actual field type ID so that it can call BaseFieldDefinition::create(), and then we access getPropertyDefinitions().

    It seems that DataFieldType relies on field_name being set, but this is not true for PreconfiguredFieldUiOptionsInterface field types.

  • 🇫🇷France lazzyvn paris

    I knew it. it write down in \datafield\src\Plugin\Field\FieldType\DataFieldItem.php

        // Reset the field storage config property - it will be recalculated
        // when accessed via the property definition getter.
        // @see Drupal\field\Entity\FieldStorageConfig::getPropertyDefinitions()
        // If we don't do this, an exception is thrown during the table update that
        // is very difficult to recover from since the original field tables have
        // already been removed at that point.
        $field_storage_config = $form_state->getBuildInfo()['callback_object']->getEntity();
        $field_storage_config->set('propertyDefinitions', NULL);
    

    You will see the same error on the module having a dynamic subfield like double field, triple field...

Production build 0.71.5 2024