An example for a very basic content entity in Drupal 8
Switch branches/tags
Nothing to show
Clone or download
Latest commit 4a92461 Jan 8, 2018
Failed to load latest commit information.
sk_contactform Remove Debug code and fix route name Jan 8, 2018 Readme improved Jan 8, 2018

Drupal 8 Basic Content Entity Example

This is a basic example for a content entity in Drupal 8

There is this documentation on for a basic content entity:

While this is valid and helpful for basic understanding, it is of limited practical use imho.

Another example for a config is provided in Examples module Example given there has a lot more to it and if working from there it is maybe a bit harder to get your head around all the things used there and to determine what you might need and what you do not need.

So my intention here is to provide a very basic implementation (but with some more features as the very basic example on and explain settings and effects here, basically for my own reference.

At this moment the example provides the following:

  • a content entity with three fields beside the id and uuid fields, these fields are
    • name (string/text filed)
    • mail (email field)
    • message (long text field)
  • a form for adding a content entity

Nothing more at this moment, will update it occasionally

src/Entity/SKContactForm.php defines the Entity by implementing ContentEntityBase class only function baseFieldDefintions is implemented. This is where the additional fields are added. Using the create method a field type plugin is provided, you can find valid field types by searching annotations of @FieldType in code.

There is annotation code in the class file which is important for the Entity.

By enabling the module a table sk_contactform (base table annotation) is created, with fields id, uuid, name, mail and message as defined in baseFieldDefintions function.

The handlers annotation specifies the class where the form for adding entities is provided. This is src/Form/SendContactMessageForm.php. This class is extending ContentEntityForm class and providing the form. To actually have the custom fields displayed in the form it is necessary to set displayOptions() in baseFieldDefinitions() of Entity Class. displayOptions() first sets the context (form or view), here form and then provides the options for the context, type and weight. Type is a Field Widget Plugin, you can find valid options searching annotations of @FieldWidget in code.

Finally there is a routing.yml file which maps the entity add handler to a custom url, in this case /sk/contactform/add