In this example will be creating a field that has both an entity reference field and an integer field.
FieldType
The FieldType is the class that will handle the storage of the field. It will be responsible for storing the data in the database and retrieving it. It will also be responsible for validating the data.
<?phpnamespaceDrupal\MODULE_NAME\Plugin\Field\FieldType;useDrupal\Core\Field\FieldItemBase;useDrupal\Core\Field\FieldStorageDefinitionInterface;useDrupal\Core\TypedData\DataDefinition;/** * @FieldType( * id = "YOUR_FIELD_TYPE_ID", * label = @Translation("THE LABEL THAT WILL BE DISPLAYED"), * description = @Translation("A SIMPLE DESCRIPTION OF THE FIELD"), * category = @Translation("THE CATEGORY WHERE IT WILL BE DISPLAYED"), * default_widget = "YOUR_DEFAULT_WIDGET", * default_formatter = "YOUR_DEFAULT_FORMATTER", * ) */// Simple example of a custom field type of two text fieldsclassFieldTypeNameextendsFieldItemBase {publicstaticfunctionpropertyDefinitions(FieldStorageDefinitionInterface $field_definition) { $properties['FIELD_ONE'] =DataDefinition::create('string')->setLabel(t('NAME OF THE FIELD')); $properties['FIELD_TWO'] =DataDefinition::create('string')->setLabel(t('NAME OF THE FIELD'));return $properties; }publicstaticfunctionschema(FieldStorageDefinitionInterface $field_definition) {return ['columns'=> [// Schema for a normal text field'FIELD_ONE'=> ['type'=>'text','size'=>'tiny','not null'=>FALSE, ],// Schema for a normal text field'FIELD_TWO'=> ['type'=>'text','size'=>'tiny','not null'=>FALSE, ], ], ]; }publicfunctionisEmpty() {if (!empty($this->FIELD_ONE)||!empty($this->FIELD_TWO)) {returnFALSE; }returnTRUE; }}
FieldWidget
The FieldWidget is the class that will handle the display of the field in the form. It will be responsible for displaying the field in the form and handling the data that is sent from the form.
The FieldFormatter is the class that will handle the display of the field in the view. It will be responsible for displaying the field in the view.
<?phpnamespaceDrupal\MODULE_NAME\Plugin\Field\FieldFormatter;useDrupal\Core\Field\FieldItemListInterface;useDrupal\Core\Field\FormatterBase;/** * @FieldWidget( * id = "YOUR_DEFAULT_WIDGET", * label = @Translation("THE LABEL THAT WILL BE DISPLAYED"), * description = @Translation("A SIMPLE DESCRIPTION OF THE FIELD"), * field_types = { * "YOUR_FIELD_TYPE_ID" * } * ) */classFieldFormatterNameextendsFormatterBase {/** * {@inheritdoc} */publicfunctionviewElements(FieldItemListInterface $items, $langcode) { $elements = [];foreach ($items as $delta => $item) {// The text value has no text format assigned to it, so the user input// should equal the output, including newlines. $elements[$delta] = ['#type'=>'inline_template','#template'=>'{{ value|nl2br }}','#context'=> ['value'=> $item->value], ]; }return $elements; }}