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'), ), ), ), ), ); } /** * Implements hook_menu(). */ function variable_entity_menu() { return array( 'variable-entity' => array( 'title' => 'Variable Entities', 'page callback' => 'variable_entity_admin_page', 'file' => 'variable_entity_pages.inc', 'access callback' => TRUE, ), 'variable-entity/manage' => array( 'title' => 'Manage', 'type' => MENU_DEFAULT_LOCAL_TASK, ), 'variable-entity/view/%variable_entity' => array( 'page callback' => 'variable_entity_view', 'page arguments' => array(2), 'title callback' => 'entity_label', 'title arguments' => array('variable_entity', 2), 'file' => 'variable_entity_pages.inc', 'access callback' => TRUE, ), ); } /** * Menu argument loader callback. */ function variable_entity_load($id) { $return = entity_load('variable_entity', array($id)); return isset($return[$id]) ? $return[$id] : FALSE; } /** * Entity URI callback. */ function variable_entity_uri($variable_entity) { return array('path' => 'variable-entity/view/' . $variable_entity->id); } /** * 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; } }