4

In my custom entity I want to set a base scheme field, which is an integer. The field has a minimum of 0, a maximum of 360 and a default of 180.

I tried this so:

    $fields['pan'] = BaseFieldDefinition::create('integer')
  ->setLabel(t('PAN'))
  ->setDescription(t('Horizontal Positioning'))
  ->setSettings(array(
    'default_value' => 180,
    'min_value' => 0,
    'max_value' => 360
  ))
  ->setDisplayOptions('view', array(
    'label' => 'inline',
    'type' => 'default',
    'weight' => -3,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'number',
    'weight' => -3,
    'settings' => array( 
      'default_value' => 180,
      'min' => 0,
      'max' => 360
    ),
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);

Neither the form is limited nor the input is limited when I post.

How can I get the correct type for my form and the correct settings? How can I look for them? Where?

Edit: solution is the following:

    $fields['pan'] = BaseFieldDefinition::create('integer')
  ->setLabel(t('PAN'))
  ->setDescription(t('Horizontal Positioning'))
  ->setDefaultValue(180)
  ->setSettings(array(
    'min' => 0,
    'max' => 360
  ))
  ->setDisplayOptions('view', array(
    'label' => 'inline',
    'type' => 'number_integer',
    'weight' => -3,
  ))
  ->setDisplayOptions('form', array(
    'type' => 'number',
    'weight' => -3,
  ))
  ->setDisplayConfigurable('form', TRUE)
  ->setDisplayConfigurable('view', TRUE);
  • You should take the answer out of the question and add it as an answer. This is a good answer. – oknate May 2 '18 at 21:12
2

You can always create a field with the desired settings in the field, widget and formatter settings in the UI. Then export the configuratio and use the settings from there. The field settings are in two files:

  • field.storage.entity_type.yourname.yml
  • field.field.entity_type.bundle.yourname.yml

For base fields, you can merge the settings together into a single array.

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.