6

I created a custom entity with drupal generate:entity:content.

I am adding fields in the baseFieldDefinitions method of the entity class.

Below is what drupal console generates by default for the name field.

Where I can find a list of all possible type values for ->setDisplayOptions('form'). For example, the field below uses string_textfield.

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    ...
    $fields['name'] = BaseFieldDefinition::create('string')
      ->setLabel(t('Name'))
      ->setDescription(t('The name of the entity.'))
      ->setSettings([
        'max_length' => 50,
        'text_processing' => 0,
      ])
      ->setDefaultValue('')
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'string',
        'weight' => -4,
      ])
      ->setDisplayOptions('form', [
        'type' => 'string_textfield',
        'weight' => -4,
      ])
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);
8

Those are field widget plugin IDs.

You can get all of those by querying the field widget plugin manager (service ID: plugin.manager.field.widget).

E.g. to get all the IDs in your entity class (once the service is injected):

$widget_types = $this->fieldWidgetPluginManager->getDefinitions();
$plugin_ids = array_keys($widget_types);

Same thing in procedural code in case someone wants it:

$widget_types = \Drupal::service('plugin.manager.field.widget')->getDefinitions();
$plugin_ids = array_keys($widget_types);

If your current context allows you to inject the widget plugin manager service rather than get it from \Drupal, all the better.

| improve this answer | |
  • 1
    Good answer, this should be in d.org docs, noobs will be searching for info of what they can add and there is no info on that. So people will be lost. – Marko Blazekovic Sep 13 '18 at 9:07
  • The answer is for setDisplayOptions('form'), but for setDisplayOptions('view'), do you know the possible plugin options for the type parameter? – David Feb 18 '19 at 6:23
  • plugin.manager.field.formatter for view (field formatter) options. – Beau Sep 10 '19 at 1:19

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.