Alter default value for field settings (allowed_formats) Please how can I alter the default field settings whenever a new field is added?  

NB I am not asking how to set the default whenever a new entity is added.  This is about changing the pre-populated values on the page "Administration->Structure->Content types->XXX->Manage fields->Add field when a field is added"

NB This question is for D8.  In D7 the answer is to use hook_field_info_alter.

Specifically I am using the module allowed_formats, and I would like every new text_with_summary or text_long field to have defaults of:


Allowed formats = "Basic HTML" only
Form display settings "Hide the help link About text formats." and "Hide text format guidelines." enabled.


The defaults need to work even if the new field is created in code.  In particular it's important to work for the Body field that is created automatically on a new content type.

I'm struggling because looking at the code, the defaults come from the public static function TextWithSummaryItem::defaultFieldSettings and there doesn't seem to be any hook.
Thanks to @Berdir and @4k4 who helped me out several times - I just followed their ideas, tested and wrote up the answers.

This first hook alters the field settings.  I've included a second example for image settings.

/**
 * Implements hook_ENTITY_TYPE_create() for 'field_config'.
 */
function MYMODULE_field_config_create(FieldConfigInterface $field) {
  if ($field-&gt;isSyncing()) return;

  switch ($field-&gt;getType()) {
    case 'image':
      $field-&gt;setSettings(['file_directory' =&gt; 'images', 'max_resolution' =&gt; '1080x1080']);
      break;

    case 'text_with_summary':
    case 'text_long':
      $field-&gt;setThirdPartySetting('allowed_formats', 'basic_html', 'basic_html');
      break;
  }
}


This second hook alters the form display settings.  The array_diff_key command is used to determine which fields are new.

/**
 * Implements hook_ENTITY_TYPE_presave().
 *
 * Override field form display defaults used when a field is added to a content type.
 */
function MYMODULE_entity_form_display_presave(EntityFormDisplay $display) {
  $fields = $display-&gt;getComponents();
  $origFields = (isset($display-&gt;original)) ? $display-&gt;original-&gt;getComponents() : [];
  foreach (array_diff_key($fields, $origFields) as $newField =&gt; $settings) {
    switch ($settings['type']) {
      case 'text_textarea_with_summary':
      case 'text_textarea':
        $settings['third_party_settings']['allowed_formats'] = ['hide_help' =&gt; '1', 'hide_guidelines' =&gt; '1'];
        $display-&gt;setComponent($newField, $settings);
        break;
    }
  }
}