/**
* Implements hook_form_alter().
*/
function MODULE_NAME_form_alter(
&$form,
\Drupal\Core\Form\FormStateInterface $form_state,
$form_id
) {
if ($form_id == 'node_article_form') {
$form["body"]["widget"][0]['#allowed_formats'] = ['full_html']; // Your default format
$form["body"]["widget"][0]['#after_build'][] = 'MODULE_NAME_hide_format';
}
}
function MODULE_NAME_hide_format(
&$element,
FormStateInterface $form_state
) {
if (isset($element["format"]["format"])) {
$element["format"]["format"]["#access"] = FALSE;
}
return $element;
}
I am trying to set the default input format for a node body field and avoid users can select a different input format by hiding the form element to change it.
I am using the following code, but it doesn't work.
/**
* Implements hook_form_alter().
*/
function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'node_article_form') {
$form["body"]["widget"][0]['#allowed_formats'] = ['full_html'];
$form["body"]["widget"][0]['#after_build'][] = 'mymodule_hide_format';
}
}
function mymodule_hide_format(&$element, FormStateInterface $form_state) {
if (isset($element["format"]["format"])) {
$element["format"]["format"]["#access"] = FALSE;
}
return $element;
}