1

I'm creating a base field (text_long) on a content entity and I want to limit it the Plain text text format for all users at all times.

I know I can use the Allowed Formats module to limit the options, but is there a way to do this when using BaseFieldDefinition::create()?

Here's the definition:

$fields['message'] = BaseFieldDefinition::create('text_long')
  ->setLabel(t('Message'))
  ->setRequired(TRUE)
  ->setDisplayOptions('form', [
    'type' => 'text_textarea',
    'weight' => 10,
  ])
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayOptions('view', [
    'type' => 'text_default',
    'label' => 'above',
    'weight' => 10,
  ])
  ->setDisplayConfigurable('view', TRUE);

PHPDoc says:

Refer to \Drupal\Core\Field\FieldDefinitionInterface::getDisplayOptions() for a list of supported keys. The options should include at least a 'weight', or specify 'region' = 'hidden'. The 'default_widget' / 'default_formatter' for the field type will be used if no 'type' is specified.

However, the documentation for FieldDefinitionInterface::getDisplayOptions() isn't enough information for me to figure out how to restrict this to Plain text.

2 Answers 2

1

There is no documentation for the content of the display options because every plugin you add to the configuration can add their own structure. Also modules not providing field plugins can still add arbitrary data to the third party settings. If the module providing this data doesn't have documentation about the internal config settings, which they normally don't do, you could consider their UI options as documentation. Configure a temporary field in UI and export the configuration. Check the YAML files modified by your config actions for the display options you can put in the base field definition.

0

Based on 4uk4's answer, these are the concrete steps to answer my question.

Note that you need to use the Allowed Formats module for now until this core issue is committed (relatively soon?).

This is a YML export of Allowed Formats restricting a field to plain_text only:

settings:
  allowed_formats:
    - plain_text

This is a setting, not a display option, so you need to use setSetting:

  ->setSetting('allowed_formats', [0 => 'plain_text'])

This removes CKEditor from the field and restricts input to plain text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.