Creating a content entity type in Drupal 8

This page provides an example of how to create a content entity type, with administration management pages, for Drupal 8.

We will create a 'Contact' entity to add, edit and delete People (Contacts). It is fully fieldable and uses most of the new entity concepts available in Drupal 8.

Note: This code corresponds to the ContentEntityExample of the Drupal Examples Module. You can download it from https://www.drupal.org/project/examples.

Create a custom content and configuration entity

This explains how to create a node-like entity in Drupal 8. That means that we'll create one configuration entity(FooType) which will serve as type or bundle for our second entity(Foo) which is a content entity and will represent the node itself. The content entity will also support revisions and translations(not yet tested).

Programming Drupal 7 Entities

Cover image for this book
Sub-title: 
Expose local or remote data as Drupal 7 entities and build custom solutions.
Authors: 
Publisher: 
Packt Publishing
Publication date: 
2013-06
Page count: 
134
ISBN-13: 
9781782166528

What you will learn from this book

  • Manipulate and utilize comment, file, field collection, node, term, user, and vocabulary entities
  • Attach, manipulate, and utilize date, file, image, link, number, text, and term reference fields
  • Write type-agnostic code dealing with more types of Drupal data than ever before
  • Upgrade a legacy Drupal node module to expose and utilize entities
  • Expose your legacy database tables as fully-fledged Drupal entities
  • Batch import remote data and expose them as entities
  • Glean good coding practices for dealing with entities

In Detail

Writing code for manipulating Drupal data has never been easier! Learn to dice and serve your data as you slowly peel back the layers of the Drupal entity onion. Next, expose your legacy local and remote data to take full advantage of Drupal's vast solution space.

Programming Drupal 7 Entities is a practical, hands-on guide that provides you with a thorough knowledge of Drupal's entity paradigm and a number of clear step-by-step exercises, which will help you take advantage of the real power that is available when developing using entities.

This book looks at the Drupal 7 entity paradigm, and breaks down the mystery and confusion that developers face when building custom solutions using entities. It will take you through a number of clear, practical recipes that will help you to take full advantage of Drupal entities in your web solutions without much coding.

You will also learn how to expose local and remote data to give your legacy data new life as fully-fledged Drupal entities that can be used by other modules such as views, rules, and so on. In addition to this, you'll learn to create, retrieve, update, and delete (CRUD) entities, their properties and fields, along with some programmatic wizardry to manipulate complex entities such as field collections. If you want to develop quickly and easily using Drupal entities, then this is the book for you.

You will learn everything you need to know to develop code and expose data using entities in Programming Drupal 7 Entities.

Approach

The book follows a standard tutorial-based approach to create, retrieve, update, and delete Drupal 7 entities, their properties and fields.

Who this book is for

Programming Drupal 7 Entities is perfect for intermediate or advanced developers new to Drupal entity development who are looking to get a good grounding in how to code using the new paradigm. It’s assumed that you will have some experience in PHP development already, and being vaguely familiar with Drupal, GIT, and Drush will also help.

MigrateDestinationCommerceProduct

The contrib module Commerce Migrate provides a few destination classes that extend the Entity API destination class provided by Migrate Extras.

When declaring the destination map, use "commerce_product" as the entity type (which is defined by the Commerce module suite), and "my_product_type" as the bundle type.

The following class imports commerce products. "sku" and "commerce_price" mappings are relevant to products. In the following example, "my_product_type" is the machine name of the commerce product type (defined via UI config), "import_database" is the import database name (defined in settings.php), and "import_data" is the table in that database where the import data exists. In the source map, "ItemNumber" is the unique identifier column in the table.

<?php
class AseEndoMigration extends Migration {
/**
* Doc comment.
*/
public function __construct() {
parent::__construct();

// The defintion of the columns.
$columns = array(
'Category',
'Name',
'ItemNumber',
'Description',
'USRetailPrice',
);

// Entity type, and bundle.
$this->destination = new MigrateDestinationCommerceProduct('commerce_product', 'my_product_type');

// Select all endodontic products.

Example: Programmatically create and update field collection with Entity API

The Entity API provides classes and methods / functions that make CRUD for entities much, much easier and less prone to errors and bugs.

The first example show the creation of a field collection entity that will be attached to a node that has a field collection field already defined for it through the Manage Fields tab of the node type creation / modification UI. The example shows creating the field collection entity (using the entity_create function of the Entity API, attaching it to a node, and using the Entity API then to set values of fields within the field collection.


<?php
// Code that would be here but is omitted loads a node called $my_node
// Nodes of the type that is $my_node have a field of the field collection
// type called field_text_files, which in turn is defined as having two
// fields called field_source_txt_filename and field_source_txt_content

// Begin by using the entity_create function to create a new entity of
// type field_collection_item. The second parameter to the function
// provides "an array of values to set, keyed by property name".
// In our example, the field in my_node that holds the field collection is

Subscribe with RSS Subscribe to RSS - entity api