0

I'm trying to figure out how to add an extra field to content type field settings page.

Basically I want to alter field instance settings form on admin/structure/types/manage//fields/node../storage .

I'm going with hook_form_FORM_ID_alter for FORM_ID field_config_edit_form. The new form element appears on the form but after submit I'm not able to see the value.

Am I missing something? Is an extra form submit function required other than the default one to submit my new value?

Here is my snippet -

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymod_form_field_storage_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  $form['enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable'),
      '#description' => t('If enabled then this module will alter the field settings.'),
      '#default_value' => $form['enabled'],
  );
}
  • Extra fields in config entities are stored in the ThirdPartySettings, see webomelette.com/… – 4k4 Feb 13 at 23:30
  • Ok, I can see the use of $form['#entity_builders'][] and ThirdPartySettingsInterface . Will try it out and update here. – wilNev Feb 14 at 7:18
0

Here is an example -

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymod_form_field_storage_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  $form['enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable'),
      '#description' => t('If enabled then this module will alter the field settings.'),
      '#default_value' => $form['enabled'],
  );

  $form['#entity_builders'][] = '_my_additional_form_field_storage_submit';
}

/**
 * Entity builder.
 */
function _my_additional_form_field_storage_submit($entity_type, Drupal\field\Entity\FieldStorageConfig $fieldStorageConfig, &$form, \Drupal\Core\Form\FormStateInterface $form_state) {

  if ($form_state->getValue('enabled')) {
    $fieldStorageConfig->setThirdPartySetting('my_additional_settings', 'enabled', $form_state->getValue('enabled'));
  }
  return true;
}

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.