Restricting Text Formats - NCIOCPL/cgov-digital-platform GitHub Wiki
The cgov_util module provides a service which sets the #allowed_formats
and #after_build
properties on text widget items, allowing the restriction of text formats and removal of the the format dropdown and help text.
Each module that defines the field.storage item for a WYSIWYG field should implement and update the following in their .module file:
function <module_name>_field_widget_form_alter(&$element, $form_state, $context) {
// Maps field names to an array containing a single format.
$map = [
'<field_XXX>' => ['<text_format_machine_name>'],
'<field_YYY>' => ['<text_format_machine_name>'],
];
$formHelper = \Drupal::service('cgov_util.form_tools');
$formHelper->allowTextFormats($map, $element, $context);
}
Replace the <module_name>, <field_XXX>, and <text_format_machine_name> placeholders as necessary.