3

I'm trying to add 2 options to the field settings of all fields of a particular field type, in this case file field. To add the fields to the form I'm using MYMODULE_form_field_config_edit_form_alter() instead of extending the base class so it will work with other modules and I won't need to use a custom field type.

Using ThirdPartySettings to store the data doesn't add it to the base config for the field therefore the settings won't be automatically available to other modules.

e.g.

function MYMODULE_form_field_config_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) { $field = $form_state->getFormObject()->getEntity(); if ($field->getType() == 'file') { $settings = $field->getSettings(); $form['my_checkbox'] = [ '#type' => 'checkbox', '#title' => t('My Checkbox'), '#default_value' => isset($settings['my_checkbox']) ? $settings['my_checkbox'] : '', '#description' => t('Check to check my checkbox.'), ]; $form['#entity_builders'][] = 'MYMODULE_form_builder'; } } function MYMODULE_form_builder($entity_type, FieldConfig $field, &$form, FormStateInterface $form_state) { // What goes here? I can't do $field->set() on properties which don't exists. } 

I've tried setting up a schema file as src/config/scheme/mymodule.schema.yml but I'm not sure how to extend the relevant settings in core/modules/file/config/schema/file/schema.yml.

base_file_field_field_settings seems to have the relevant entries I'm wanting to add to. Do I need to add a class or an install yml file?

1 Answer 1

2

This works for me:

$field->setThirdPartySetting('mymodule', 'my_checkbox', $form_state->getValue('my_checkbox')); 

After you save your field settings, the new third party setting should be available in your field config:

third_party_settings: mymodule: my_checkbox: 1 

Here is a good article about this: https://www.webomelette.com/drupal-8-custom-data-configuration-entities-using-thirdpartysettingsinterface

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.