Score:0

Error when instantiating custom field in custom module

nl flag

I apologize if the answer to this is obvious, but I am newly-embarked on porting a number of complex custom Drupal 7 modules to Drupal 9. I am using Drupal version 9.3.6, PHP version 7.4.28, mysql, apache2. I have created a custom field as follows, which on install goes without errors and appears in the field list report:

langcode: en
status: true
dependencies:
  module:
    - user
  enforced:
    module:
      - test_module
id: user.field_test
field_name: field_test
entity_type: user
type: string_long
settings:
  case_sensitive: false
module: core
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false

But when I try to instantiate it, either with field.field.user.user.field_test.yml or manually through the UI, I get the following error stack:

 [warning] A non-numeric value encountered TypedConfigManager.php:189
 [error]  Error: Unsupported operand types in Drupal\Core\Config\TypedConfigManager->getDefinitionWithReplacements() (line 189 of /var/www/test_site/web/core/lib/Drupal/Core/Config/TypedConfigManager.php) #0 /var/www/test_site/web/core/lib/Drupal/Core/Config/TypedConfigManager.php(105): Drupal\Core\Config\TypedConfigManager->getDefinitionWithReplacements()

(Sorry, I had to delete the remainder of the stack dump to avoid getting flagged as spam!)

This is the part of TypedConfigManager as follows:

    // Add type and default definition class.
    $definition += [
      'definition_class' => '\Drupal\Core\TypedData\DataDefinition', // Here is line 189
      'type' => $type,
      'unwrap_for_canonical_representation' => TRUE,
    ];
    return $definition;

I am at a loss here. If I create the field storage through the admin interface and instantiate also through the admin interface, everything works fine. I export those definitions through the single export interface, and then I get the failure.

Thank you in advance for any insight!

Update: @Jaypan, here is the code for the field.field:

langcode: en
status: true
dependencies:
  config:
    - field.storage.user.field_test
  module:
    - user
  enforced:
    module:
      - test_module
id: user.user.field_test
field_name: field_test
entity_type: user
bundle: user
label: 'Test field'
description: ''
required: false
translatable: false
default_value: {  }
default_value_callback: ''
settings: {  }
field_type: string_long
Jaypan avatar
de flag
I believe fields have two sets of configuration, one for the field on the entity, and one for the storage. You have only listed one set of configuration - maybe you're missing the other. Something to look into.
Jennifer avatar
nl flag
Thank you, @Jaypan, but I have both. As mentioned, the error happens even when I instantiate manually. But for completeness I included above the code for the field.field.
Score:0
nl flag

I have figured out where the problem is. The fact that instantiating the fields (both manually and programmatically) triggered the error has nothing to do with it, and I still do not understand exactly why, but here is what I learned by deleting files from the module one-by-one: a view that I had created manually and exported for my module apparently caused some issue in the configuration system. When I deleted the yml file for the view from config/schema, everything enabled fine. Go figure.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.