diff -u b/allowed_formats.module b/allowed_formats.module --- b/allowed_formats.module +++ b/allowed_formats.module @@ -2,6 +2,8 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\field\Entity\FieldConfig; +use Drupal\Core\Field\WidgetInterface; +use Drupal\Core\Field\FieldDefinitionInterface; /** * Implements hook_form_FORM_ID_form_alter(). @@ -24,11 +26,27 @@ '#default_value' => $field->getThirdPartySettings('allowed_formats'), '#description' => t('Restrict which text formats are allowed, given the user has the required permissions. If no text formats are selected, then all the ones the user has access to will be available.') ]; - $form['third_party_settings']['allowed_formats_hide'] = [ + } +} + +/** + * Implements hook_field_widget_third_party_settings_form(). + */ +function allowed_formats_field_widget_third_party_settings_form(WidgetInterface $plugin, FieldDefinitionInterface $field_definition, $form_mode, $form, FormStateInterface $form_state) { + if (in_array($field_definition->getType(), _allowed_formats_field_types())) { + $element = array(); + $element['hide_help'] = [ + '#type' => 'checkbox', + '#title' => t('Hide the help link About text formats.'), + '#default_value' => $plugin->getThirdPartySetting('allowed_formats', 'hide_help'), + ]; + $element['hide_guidelines'] = [ '#type' => 'checkbox', - '#title' => t('Hide text format guidelines when only a single format is allowed.'), - '#default_value' => $field->getThirdPartySettings('allowed_formats_hide'), + '#title' => t('Hide text format guidelines.'), + '#default_value' => $plugin->getThirdPartySetting('allowed_formats', 'hide_guidelines'), ]; + + return $element; } } @@ -56,9 +74,14 @@ if (!empty($allowed_formats)) { $element['#allowed_formats'] = $allowed_formats; } - if ((count($element['#allowed_formats']) == 1) && ($field_configuration->getThirdPartySettings('allowed_formats'))) { - $element['#after_build'][] = '_allowed_formats_remove_textarea_help'; - } + } + + /** @var WidgetInterface $widget */ + $widget = $context['widget']; + $element['#allowed_format_hide_settings'] = $widget->getThirdPartySettings('allowed_formats'); + + if (count(array_filter($element['#allowed_format_hide_settings']))) { + $element['#after_build'][] = '_allowed_formats_remove_textarea_help'; } } } @@ -68,9 +91,20 @@ */ function _allowed_formats_remove_textarea_help($form_element, FormStateInterface $form_state) { if (isset($form_element['format'])) { - // All this stuff is needed to hide the help text. - foreach (['guidelines', 'help', '#type', '#theme_wrappers'] as $field) { - unset($form_element['format'][$field]); + if ($form_element['#allowed_format_hide_settings']['hide_help']) { + unset($form_element['format']['help']); + } + if ($form_element['#allowed_format_hide_settings']['hide_guidelines']) { + unset($form_element['format']['guidelines']); + } + + // If nothing is left in the wrapper, hide it as well. + if (isset($form_element['#allowed_formats']) + && count($form_element['#allowed_formats']) == 1 + && $form_element['#allowed_format_hide_settings']['hide_help'] + && $form_element['#allowed_format_hide_settings']['hide_guidelines']) { + unset($form_element['format']['#type']); + unset($form_element['format']['#theme_wrappers']); } }