Improve DX creating field definitions

Created on 27 November 2013, about 11 years ago
Updated 25 October 2024, about 2 months ago

Follow-up from #2047229: Make use of classes for entity field and data definitions to further improve DX of creating field definitions as requested by Dries in #2047229-189: Make use of classes for entity field and data definitions .

To getting the discussion started, here is what Dries posted:

However, I'd argue that as it stands it doesn't really move the needle in developer experience. I believe there is a lot more we could do.

Example taken from the patch:

+    $fields['weight'] = FieldDefinition::create('integer')
+      ->setLabel(t('Weight'))
+      ->setDescription(t('The weight of this term in relation to other terms.'))
+      ->setFieldSetting('default_value', 0);

Could be simplified to:

+    $fields['weight'] = new IntegerFieldDefinition()
+      ->setLabel(t('Weight'))
+      ->setDescription(t('The weight of this term in relation to other terms.'))
+      ->setDefaultValue(0);

IntegerFieldDefinition would extend BaseFieldDefinition, and setDefaultValue() would be implemented in IntegerFieldDefinition.

In fact, this could be simplified further. 'Label' and 'Description' appear to be required so we could actually write the following.

+    $fields['weight'] = new IntegerFieldDefinition(t('Weight'), t('The weight of this term in relation to other terms.'))
+      ->setDefaultValue(0);

Another example from the patch:

+    $fields['changed'] = FieldDefinition::create('integer')
+      ->setLabel(t('Changed'))
+      ->setDescription(t('The time that the term was last edited.'))
+      ->setPropertyConstraints('value', array('EntityChanged' => array()));

Could become:

+    $fields['changed'] = new EntityChangedFieldDefinition();
+      ->setLabel(t('Changed'))
+      ->setDescription(t('The time that the term was last edited.'));

Note that all the obscure constraint stuff would now abstracted away for people that need to create new entity definitions.

Or even simpler:
+ $fields['changed'] = new EntityChangedFieldDefinition(t('Changed'), t('The time that the term was last edited.'));

Thoughts?

📌 Task
Status

Postponed: needs info

Version

11.0 🔥

Component

field system

Created by

🇦🇹Austria fago Vienna

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇳🇿New Zealand quietone

    There hasn't been any discussion here for 9 years. Is there anything to do here, perhaps documentation?

    I am setting the status to Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024