Creating custom entities

by Gisle Hannemyr

Table of contents

Introduction

Move this to Create custom content types (case study).

Create the node type

To create the node type, create a file named node.type.contact.yml and place it in your module's directory in a subdirectory called config/install. For example this file could be at /modules/example/config/install/node.type.contact.yml if the module is in the custom module HNM Accept.

Add fields

To create the node type, create a file named field.field.node.contact.body.yml and place it in your module's directory in a subdirectory called config/install. Example:

field.field.node.contact.body.yml
langcode: en
status: true
dependencies:
    config:
        - field.storage.node.body
        - node.type.contact
    module:
        - text
id: node.contact.body
field_name: body
entity_type: node
bundle: contact
label: Body
description: 'More specific information about the contact.'
required: false
translatable: true
default_value: {  }
default_value_callback: ''
settings:
    display_summary: true
field_type: text_with_summary

Define schema for the custom entity

Register it with Drupal

Entity class and controller

Overriding the entity classes

Individual entity pages

Admin user interface

Fields

Exposing the entity type to Views

Final word

This is the end of the introduction to Drupal entities.

This chapter first explained how to write a schema defintion for a custom Drupal entity type, and how to register this as a entity type with Drupal. It then introduced the the contributed Entity API module to access to a set of object oriented tools for working with the entities, including a CRUD (create, read, update, delete) interface.

The four main aspects of working with entities described in this chapter are:

  1. How to show (read) the instances of an entity.
  2. How to create an admin interface for creating, updating and deleting entity instances.
  3. How to make an entity class fieldable through the UI.
  4. How to expose entities to Views to do queries and listings.

While some ground has been covered, there's more you can do to perfect and customize your entity types: view modes, revisions, etc.

DEBUGGING

function entity_get_info


Last update: 2014-11-26 [gh].