3

I have created a custom field type module (field plugin) containing four fields: title, description, address, image. Drupal version: 8.02

This is the schema structure inside FieldType php file:

public static function schema(FieldStorageDefinitionInterface $field) {
    return array(
        'columns' => array(
            'title' => array(
                'type' => 'varchar',
                'length' => 255,
                'not null' => FALSE,
            ),
            'description' => array(
                'type' => 'varchar',
                'length' => 255,
                'not null' => FALSE,
            ),
            'address' => array(
                'type' => 'varchar',
                'length' => 255,
                'not null' => FALSE,
            ),
            'image' => array(
                'type' => 'int',
                'size' => 'small',
                'not null' => FALSE,
                'default' => 0,
            ),
        ),
    );
}

And this is the structure defined in FieldWidget php file:

$element['title'] = array(
    '#title' => t('Title'),
    '#type' => 'textfield',
    '#default_value' => isset($items[$delta]->title) ? $items[$delta]->title : NULL,
    '#attributes' => array(
        'data-title-delta' => $delta,
    ),
);

$element['description'] = array(
    '#title' => t('Description'),
    '#type' => 'textfield',
    '#default_value' => isset($items[$delta]->description) ? $items[$delta]->description : NULL,
    '#attributes' => array(
        'data-description-delta' => $delta,
    ),
);

$element['address'] = array(
    '#title' => t('Address'),
    '#type' => 'textfield',
    '#default_value' => isset($items[$delta]->address) ? $items[$delta]->address : NULL,
    '#attributes' => array(
        'data-address-delta' => $delta,
    ),
);

$element['image'] = array(
    '#title' => t('Image'),
    '#type' => 'managed_file',
    '#upload_validators' => array(
        'file_validate_extensions' => array('jpg jpeg png gif'),
    ),
);

The three textfields were working perfectly until I've added the managed_file field: now, if I fill the fields, choose an image file and save, I get this error message:

This value should be of the correct primitive type.

And ALL the module fields are highlighted in red. Nothing is saved. I'm quite sure that the issue is related to the managed_field (image) field, but I can't understand how to solve this.

1
  • I have made some tests trying to understand this problem. As said, it only occurs when the managed_file field is added to the form. It seems that managed_file field creates problems to other general textfields of the module, because the error I'm getting is related to the FALSE value returned by PrimitiveTypeConstrainValidator class at this control: if ($typed_data instanceof StringInterface && !is_scalar($value) && !($value instanceof MarkupInterface)) { $valid = FALSE; }
    – Gabriele
    Commented Feb 27, 2016 at 11:18

0

Browse other questions tagged or ask your own question.