Drupal Entity reference

Entity Type
========
Node, User, File, Taxonomy

Bundles
=====
Article
Page

1. You need to create a Entity Controller to do CRUD operation with database table
2. To create a simple custom entity you need following functions

  • hook_entity_info()
  • hook_entity_property_info()
  • An interface which extends DrupalEntityControllerInterface 
  • A Controller class which extends DrupalDefaultEntityController and  implements above Interface
3. Entity API provide some easy functions to interact with entity in code like entity_save, entity_create, entity_delete, etc.

4. Look at the following function in Entity.module. We can save an entity by writing a 'save callback' or writing Controller. Not sure what method_exists($entity, 'save') expects

function entity_save($entity_type, $entity) {
  $info = entity_get_info($entity_type);
  if (method_exists($entity, 'save')) {
    return $entity->save();
  }
  elseif (isset($info['save callback'])) {
    $info['save callback']($entity);
  }
  elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
    return entity_get_controller($entity_type)->save($entity);
  }
  else {
    return FALSE;
  }
}

Comments

Popular posts from this blog

Programatically create layout builder section in Drupal

Code quality analysis of Drupal. Can I use Sonar?

Set up Drupal7 to Drupal8 migration in simple steps (using Drush)