Introduce to Entity in Drupal

Nguyen Tien Si

GO1

Agenda

  • Drupal 6 Data
  • Introducing entities
  • Introducing entity types, bundles and fields
  • Entity use cases
  • Developing with Entity Metadata Wrappers

Drupal 6 Data

  • Custom database table
    • User
    • Comment
    • File
    • Taxonomy
    • Node
  • Custom content type
    • Page
    • Blog

Introducing entities

Any defined chunk of data in Drupal. This includes things like nodes, users, taxonomy terms, files, etc. Contributed modules can define custom entities. Each entity type can have multiple bundles. 

Source: https://www.drupal.org/glossary#entity

Introducing entity types, bundles and fields

Entity types

In earlier versions of Drupal, the field system was only used on content types. Now, thanks to the Entity API, we can add fields to other things, like comments. Fieldable entities make Drupal eminently flexible. An entity type is a useful abstraction to group together fields.

 

Example: Node, Product, Order,...

Bundles

Bundles are an implementation of an entity type to which fields can be attached. You can consider bundles as subtypes of an entity type. With content nodes (an entity type), for example, you can generate bundles (subtypes) like articles, blog posts, or products.

Fields

A field is a reusable piece of content. In technical terms, each field is a primitive data type, with custom validators and widgets for editing and formatters for display. You can read further for a developer's guide to using the Drupal 7 Fields API.

Fields & Field instances

Fields & Fields instances in Database

Fields & Fields instances in Database

Field API

Putting this in Object-Oriented Design

  • An entity type is a base class
  • A bundle is an extended class
  • A field is a class member, property, variable or field instance (depending on your naming preference)
  • An entity is an object or instance of a base or extended class

Entity use cases

  • Entity Kick Start: model
  • Entity Construction Kit: eck
  • White label entity type: while
  • Entity in Practice: profile2, message, commerce, bean, subs, course
  • Entity Revision in Practice: fieldable_panels_panes

Commerce Entities

Entity w/ no bundles

Entity w/ 2 defined bundles

Entity w/ bundles & fields

Entity w/ n bundles & fields

Developing with Entity Metadata Wrappers

  • Reading https://www.drupal.org/documentation/entity-metadata-wrappers
<?php
 $node->field_number[LANGUAGE_NONE][0]['value'];
?>
<?php
 $node_wrapper->field_number->value(); 
?>

After

Before

References

  • https://www.drupal.org/node/1261744
  • https://drupalize.me/videos/introduction-working-entities-drupal-7-series
  • https://drupalcommerce.org/user-guide/products
  • Book: Programming Drupal Entities
  • http://joshaust.in/wp-content/uploads/2012/06/Entities-and-Bundles-in-Drupal-7.pdf
  • https://www.drupal.org/project/entity

Introduce to Entity in Drupal

By Nguyen Tien Si

Introduce to Entity in Drupal

Introduce to Entity in Drupal

  • 999