0

I am creating a Field plugin, where I have requirement as below

Field name : Question

Properties:

Question Text : String

Question Type : integer (0,1,2)

Question Options : Array of Strings

I defined schema like this. I am not sure if I am doing it right for Question Option

public static function schema(FieldStorageDefinitionInterface $field_definition) {
    $schema = [
        'columns' => [
            'question_text' => [
                'type' => 'varchar',
            ],
            'option_type' => [
                'description' => 'Option Type: 0 = radio, 1 = checkbox',
                'type' => 'int',
                'default' => 0,
            ],
            'question_option' => [
                'type' => 'varchar',
                'serialize' => TRUE,
            ],
        ],
    ];

    return $schema;
}

And for property definition I have following

public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {

    $properties['question_text'] = DataDefinition::create('string');
    $properties['option_type'] = DataDefinition::create('integer');

    $properties['question_option'] = DataDefinition::create('string');

    return $properties;
}

Please let me know how can I do it for the question_option.

--

Thanks

1 Answer 1

1

If you want an array of strings maybe your should use a blob in you schema:

 'question_option' => [
          'type' => 'blob',
          'size' => 'big',
          'not null' => TRUE,
          'serialize' => TRUE
        ],

Note that by default 'size' = 'normal' which max storage value is 16KB so if it is enough for you use 'big'; 'big' can handle up to 4GB. And for properties as follow:

$properties['question_options'] = MapDataDefinition::create()->setLabel(t('Options'))
2
  • Yes I used the same. Only one difference. Instead of DataDefinition I used MapDataDefinition. $properties['options'] = MapDataDefinition::create()->setLabel(t('Options')); Commented Feb 23, 2017 at 5:12
  • Nice, so i'll correct my post with your feedback! Commented Feb 23, 2017 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.