Score:0

How do I make a custom multi-checkboxes field?

ke flag

By following the tutorials and documentation I am able to create a custom field that supports a single string value. However what I'd like to accomplish is a custom list (preferably the checkboxes type) where a user can select different options. I tried looking in core/modules/options schema.yml and copying the storage settings still gives me the same results. I get the error This value should be of the correct primitive type.

These are what I believe are the relevant chunks of code I currently have:

in fieldtype/

  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    return [
      // columns contains the values that the field will store
      'columns' => [
        'value' => [
          'type' => 'text',
          'size' => 'normal',
          'not null' => FALSE,
          'serialize' => TRUE
        ],
      ],
      'indexes' => [
        'value' => ['value'],
      ],
    ];
  }

In FieldWidget/

  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $sglists = ['1' => 'a', '2' => 'b'];
    $element['value'] = $element + [
        '#type' => 'checkboxes',
        '#options' => $sglists,
        '#empty_value' => '',
        '#default_value' => (isset($items[$delta]->value) && isset($sglists[$items[$delta]->value])) ? $items[$delta]->value : NULL,
        '#description' => t('Select a Sendgrid List'),
      ];

    return $element;
  }

in schema.yml

field.storage_settings.sendgridlists:
  label: 'Sendgrid Lists'
  type: mapping
  mapping:
    selectable_lists:
      type: sequence
      label: 'Selectable Lists'
      sequence:
        type: string

I am pretty sure my issue comes from the schema.yml but I'm not really understanding how to turn this from a single value, into a multi value list. (similar to core/modules/options)

Any help is greatly appreciated, included any links to existing contrib modules that successfully create a custom checkboxes field type that I can reference.

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.