Custom Content Entity type - allow fields to be managed I've created a custom content entity type in Drupal 8. I've tied it in with the Field UI so it can be fielded, and I added some fields in the entity definition using BaseFieldDefinition.

One of the fields I added was this:

$fields['message'] = BaseFieldDefinition::create('text_long')
  -&gt;setLabel(t('Message'))
  -&gt;setDescription(t('The message to be shown'))
  -&gt;setTranslatable(FALSE)
  -&gt;setRevisionable(FALSE)
  -&gt;setDisplayOptions('view', array(
    'label' =&gt; 'hidden',
    'type' =&gt; 'text_default',
    'weight' =&gt; 0,
  ))
  -&gt;setDisplayConfigurable('view', TRUE)
  -&gt;setDisplayOptions('form', array(
    'type' =&gt; 'text_textfield',
    'weight' =&gt; 0,
  ))
  -&gt;setDisplayConfigurable('form', TRUE);


I want to set a default Text Format for this field, however I'm not sure how to do so. I don't know if I can do it in the code above, but I'm thinking that if I could edit the field settings on the 'Manage Fields' tab, I could do it there. However, I can't figure out how to get my field to show up on that page. It shows up on the 'Manage Form' and 'Manage Display' tab, but not the 'Manage Fields' tab.

Does anyone know how to either get it to show up on that tab, or how to set the default text format on this field?
Alright, after lots of research I've come up with three different solutions to the issue.

Solution 1 (not so great):

On the text formats and editors configuration page (/admin/config/content/formats), the top format in the list that the user has permission to will be set as the text format automatically when saving the field (entity). If you only have a text format or two, this will work, but it is easily breakable, so it's not a great solution.

Pros - easy.

Cons - easy to break if/when someone adds a text format or re-orders them. Hard to configure. Not stable at all.

Solution 2:

In your form defintion for the entity (or a _form_alter hook for the form), you can set the format as follows. Note that in this case, my field is keyed as 'message:

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);

  foreach(\Drupal\Core\Render\Element::children($form['message']['widget']) as $element_index) {
    $form['message']['widget'][$element_index]['#format'] = TEXT_FORMAT;
  }

  return $form;
}


Note: Replace TEXT_FORMAT with the key of the text format you want to use.

Pros - can be overridden as part of the form by other developers

Cons - can be overridden as part of the form by other developers (if you want to ensure that the text format is always the same and cannot be changed)

Solution 3:

In your entity definition, first add this function (again, my field is keyed as 'message'):

public function setMessageFormat($format) {
  $this-&gt;get('message')-&gt;format = $format;

  return $this;
}


Then you can add a preSave() method to the entity definition to set the format:

public function preSave(EntityStorageInterface $storage_controller) {
  parent::preSave($storage_controller);

  $this-&gt;setMessageFormat(TEXT_FORMAT);
}


Note: Replace TEXT_FORMAT with the key of the text format you want to use.

Pros - Ensures your text format is always what you want it to be for that field on the entity

Cons - cannot be changed by anyone else if you want this to be configurable.