0

I am creating a custom multi-value fieldtype for some functionality that I need.

I have been using the drupal core 'telephone' module and the contrib 'address' module as reference, but am running into some issues.

In the src/Plugins/Field/FieldType.php definition, there is a propertyDefinitions() method used to define the properties of a field.

The default value provided in the scaffolding provided by Drupal Console provides it as

$properties['value'] = DataDefinition::create('string')->setLabel(t('Default value));

However, 'value' isn't the correct property.

The field is setup to be a select list of available options, and a textfield that takes a value.

In the schema() method, I have this:

public static function schema(FieldStorageDefinitionInterface $field_definition) {
  $schema = [
    'columns' => [
      'format' => [
        'type' => 'varchar',
        'length' => 3,
      ],
      'aph_number' => [
        'type' => 'varchar',
        'length' => 255,
      ],
    ],
  ];

  return $schema;

Where as 'format' is a property of one of the fields (stored as 'field_name__format' in the database), and 'aph_number' (stored as field_name__aph_number' in the database).

My property definitions I am using are these:

 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
   $properties = [];

   $properties['format'] = DataDefinition::create('string')
     ->setLabel(t('Format'));

   $properties['aph_number'] = DataDefinition::create('string')
     ->setLabel(t('APH Number'));

   return $properties;
 }

Which, whe looking at the 'address' field is how you define properties for a field.

When I enable the module and add the newly created field to a piece of content, I get a Drupal error, and in watchdog, it gives me this:

InvalidArgumentException: Property value is unknown. in Drupal\Core\TypedData\TypedDataManager->getPropertyInstance()

If I change 'format' and 'aph_number' to 'value' in the $properties array, the error goes away, but I don't want the 'value' property to be available on this field, I want the ones I provided.

I have been unable to find any information online regarding this, and in looking at the two modules I am using as reference, this is correct.

Could somebody possibly help me with this?

4
  • Maybe you need to override mainPropertyName() and return NULL (or one of your properties if that makes more sense)? – Clive Jun 3 at 14:20
  • That's a good call. The 'address' module does have that method in it's FieldType.php. However, after adding that, the problem still persists and I get the same error. – Ex0r Jun 3 at 14:37
  • It might help to enable error messages with backtrace information or use Xdebug to find out which code is calling getPropertyInstance() and from where this code is getting the non-existent property name. – 4k4 Jun 3 at 19:28
  • Its being called by Drupal core and its pulling it from the FieldType.php file I am using to create a new field. As soon as I change the array key to ['value'] the error goes away and the field is added, nut nothing can be stored tovit because value isnt a column in the database. – Ex0r Jun 5 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.