0

I'm attempting to add a checkbox to a field settings form in D9. I have the following code:

function my_module_field_formatter_third_party_settings_form(\Drupal\Core\Field\FormatterInterface $plugin, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, $view_mode, $form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $element = array();
  print '<h1>HERE</h1>';

  if ($plugin
    ->getPluginId() == 'text_textarea_with_summary') {
    $element['my_setting'] = array(
      '#type' => 'checkbox',
      '#title' => t('test'),
      '#default_value' => $plugin
        ->getThirdPartySetting('my_module', 'my_setting'),
    );
  }
  return $element;
}

I then edit an instance of a body field (admin/structure/types/manage/article/fields/node.article.body), and the hook doesn't fire...no debug text nor stopping at the breakpoint on that same line.

2
  • Hopefully a silly question, but did you clear the cache after writing the hook function?
    – Clive
    Oct 21, 2020 at 18:41
  • Yup. And I also have hook_field_widget_form_alter and it breaks at a breakpoint, so it’s not the module file or the xdebug setup.
    – JAyenGreen
    Oct 21, 2020 at 18:44

1 Answer 1

1

There are two problems.

/admin/structure/types/manage/article/fields/node.article.body is the path of a field setting. To trigger the hook you need to visit /admin/structure/types/manage/article/display.

The plugin ID is wrong, too. text_textarea_with_summary is the plugin ID of a widget. What you are looking for is probably the formatter text_summary_or_trimmed.

If this is neither formatter nor widget, but the content type specific field definition, then use a normal form alter hook, because there is no specific hook to add third party settings:

use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_FORM_ID_form_alter().
 */
function my_module_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state) {
  /** @var \Drupal\Core\Field\FieldConfigInterface $field */
  $field = $form_state->getFormObject()->getEntity();
  if ($field->getType() == 'text_with_summary') {
    $form['third_party_settings']['my_module']['my_setting'] = [
      '#type' => 'checkbox',
      '#title' => t('test'),
      '#default_value' => $field->getThirdPartySetting('my_module', 'my_setting'),
    ];
  }
}

Edit: The form keys $form['third_party_settings']['my_module']['my_setting'] are important. They are used to submit the third party settings.

5
  • So with adding to the form settings form being the goal, I’m barking up the wrong tree?
    – JAyenGreen
    Oct 22, 2020 at 12:37
  • This depends on whether the field settings should be stored for a field instance in general, specific for a content type or more specific for a view/form mode. Your question somehow makes all of this possible.
    – 4uk4
    Oct 22, 2020 at 12:58
  • It would be for a field instance, to give the user adding the field instance an option in addition to Required, etc. I was under the impression that doing so from a custom module was a third-party setting.
    – JAyenGreen
    Oct 22, 2020 at 13:17
  • OK, then use a form alter hook. See the edited answer.
    – 4uk4
    Oct 22, 2020 at 13:48
  • Thank you, once again!
    – JAyenGreen
    Oct 22, 2020 at 14:17

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.