6

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')
  ->setLabel(t('Message'))
  ->setDescription(t('The message to be shown'))
  ->setTranslatable(FALSE)
  ->setRevisionable(FALSE)
  ->setDisplayOptions('view', array(
    'label' => 'hidden',
    'type' => 'text_default',
    'weight' => 0,
  ))
  ->setDisplayConfigurable('view', TRUE)
  ->setDisplayOptions('form', array(
    'type' => 'text_textfield',
    'weight' => 0,
  ))
  ->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?

  • It does not look like a (text_textfield) TextfieldWidget allows for a default text format, but if the field item has the format property set, then it will use it. – mradcliffe Nov 28 '16 at 17:31
3

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->get('message')->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->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.

1

Yes, this is currently not possible, see https://www.drupal.org/node/2004672. A text field is considered empty if there is no text, and if it is empty, then the default value isn't saved.

For configurable fields, there is https://www.drupal.org/project/allowed_formats and other similar modules.

You could try setting a default value in either postCreate() or preCreate() but it is possible that this would be filtered out in the same way at some point. Would have to try to be sure. The only option that's left then is to do it in your form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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