A demonstration table-less entity

Events happening in the community are now at Drupal community events on www.drupal.org.
NealB-1's picture

I created an entity that doesn't live in the database. The entity doesn't do anything useful. It lives in a serialized array in a variable called 'variable_entity'.

The module automatically creates 3 'variable_entity' entities. Each one contains the name of a different variable on the system and fetches the contents. The entity is fieldable.

I thought of some more interesting kinds of non-database entities, like entities that live in an XML file, but that would have been too time-consuming for this little demo.

Here is the hook_entity_info(). The 'entity keys' actually refer to properties on the entity object, not strictly database fields, so the 'id' key needs to be filled in.

Setting the 'uri callback' and 'entity keys'=>'label' enables us to use the entity_uri() and entity_label() API functions.

<?php
/**
* Implements hook_entity_info().
*/
function variable_entity_entity_info() {
  return array(
   
'variable_entity' => array(
     
'label' => t('Variable entity'),
     
'uri callback' => 'variable_entity_uri',
     
'controller class' => 'VariableEntityController',
     
'entity keys' => array(
       
'id' => 'id',
       
'label' => 'label',
      ),
     
'fieldable' => TRUE,
     
'bundles' => array(
       
'variable_entity' => array(
         
'label' => t('Variable entity'),
         
'admin' => array(
           
'path' => 'variable-entity',
           
'access arguments' => array('administer content types'),
          ),
        ),
      ),
    ),
  );
}
?>

Here is the controller. It would have been nice to use the variable name directly as the id, but, as it turns out, the field subsystem expects it to be an integer.

<?php
/**
* The controller class. Inherits from DrupalDefaultEntityController, but overrides the load() method,
* so the database never gets used.
*/
class VariableEntityController extends DrupalDefaultEntityController {
 
// ids have to be numeric, so we need an array mapping ids to variable names that we know about. store it in a variable.
 
public $variables = array();

  public function
__construct($type = 'variable_entity') {
   
parent::__construct($type);
   
$this->variables = variable_get('variable_entity', array('site_name', 'site_mail', 'theme_default'));
  }

  public function
resetCache(array $ids = NULL) { }

  public function
load($ids = array(), $conditions = array()) {
   
$entities = array();
    foreach(
$ids as $id) {
      if(isset(
$this->variables[$id])) {
       
$ent = new stdClass();
       
$ent->variable_name = $this->variables[$id];
       
$ent->variable = variable_get($this->variables[$id], new stdClass());
       
$ent->label = t('Entity @id contains @var variable', array('@id' => $id, '@var' => $this->variables[$id]));
       
$ent->id = $id;
       
$entities[$id] = $ent;
      }
    }

   
// add fields
   
if(!empty($entities)) {
     
$this->attachLoad($entities, array());
    }

    return
$entities;
  }
}
?>

The source for the module is attached. I had to add .txt extensions to get gdo to let me upload them, so remove the .txt extensions and underscores where appropriate.

AttachmentSize
variable_entity.install.txt302 bytes
variable_entity.module.txt2.96 KB
variable_entity_pages.inc_.txt3.14 KB
variable_entity.info_.txt102 bytes

San Diego DUG

Group categories

Event Classifications

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week