When a user has only a single text format option, just Filtered HTML for example, the selector is still shown even though there are no other options.
How can we remove this when there is only one option?
When a user has only a single text format option, just Filtered HTML for example, the selector is still shown even though there are no other options.
How can we remove this when there is only one option?
The form element type you're probably working with is a 'text_format' type. This is a composite element/widget. You can modify this form structure using hook_form_alter()
. It's a bit hidden, but you can change 2 properties on the widget to control that dropdown.
Make sure you are editing the form structure of the widget, not any containers. See the below code example for an idea on which element to update:
$form['description']['widget'][0]['#format'] = 'full_html';
$form['description']['widget'][0]['#allowed_formats'] = ['full_html'];
You can change 'full_html' to 'restricted_html' and see the editor change. Whatever mode you wish to enforce, you can set the machine name there and it'll set that as the default + hide the dropdown.
See \Drupal\filter\Element\TextFormat.php
for some more info (contained in the annotation at the top of the file):
/**
* Provides a text format render element.
*
* Properties:
* - #base_type: The form element #type to use for the 'value' element.
* 'textarea' by default.
* - #format: (optional) The text format ID to preselect. If omitted, the
* default format for the current user will be used.
* - #allowed_formats: (optional) An array of text format IDs that are available
* for this element. If omitted, all text formats that the current user has
* access to will be allowed.
*
* Usage Example:
* @code
* $form['body'] = array(
* '#type' => 'text_format',
* '#title' => 'Body',
* '#format' => 'full_html',
* '#default_value' => '<p>The quick brown fox jumped over the lazy dog.</p>',
* );
* @endcode
*
* @see \Drupal\Core\Render\Element\Textarea
*
* @RenderElement("text_format")
*/
If you're unsure what the machine name for your text format is, you can find the list under Configuration > Content authoring > Text formats and editors. The machine name of that format will be found in the URL as a named parameter. The three default ones you get with Drupal are full_html
, restricted_html
, and basic_html
. Other modules may add text formats so you can find the full list there.