0

I want to be able to add custom settings / attirbutes to field instances.

I can create a custom field with custom settings no probs, but is it possible to get rid of the custom field and just add the settings so that the settings are stored per instance?

For example:

I create a content type, let's call it vehicle. I add a field called wheels which is a basic textfield where you can specify the number of wheels. Now I would like to add a few settings to the wheels field such as brand, model, etc. These settings would be specific to the content type but if you re-used the field wheels on another content type the settings could have different values... now this is a bit contrived but you get the idea!

Any ideas on how this could be achieved? My current solution is to create a separate field called something like Wheels meta information with the settings but I would rather a cleaner solution.

  • What exactly do you mean by "... are stored per instance?". Consider adding a sample to better explain this. – Pierre.Vriens Jun 29 '15 at 6:12
1

Ok, this is done with third party settings in hook form alter:

function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  $entity = $form_state->getFormObject()->getEntity();

  $form['third_party_settings']['my_module']['my_field_setting'] = array(
    '#type' => 'textfield',
    '#title' => t('My custom field setting'),
    '#default_value' => $entity->getThirdPartySetting('my_module', 'my_field_setting'),
  );
}

...and that's it. So awesome.

| improve this answer | |
  • Thanks, after trying to figure out why the setting wasn't saving or loading good to emphasise, you must use the module name in the third party setting subelement, i.e $form['third_party_settings']['my_module']['my_field_setting'] where my_module is your module name. – David Thomas Dec 5 '17 at 22:57
0

You can use field collection module and create group of fields to add attributes like as you mentioned in your example and reuse it for other content types as well with different values.

Hope this will help you.

| improve this answer | |
  • field collection field values would change over every content item - I need it to be field settings. ie. as returned by this function – tzm Jul 1 '15 at 0:15
  • also field collection in Drupal 8 is not really functional – tzm Jul 1 '15 at 7:51

Your Answer

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

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