Skip to content

Instantly share code, notes, and snippets.

@m7v
Last active August 29, 2015 13:57
Show Gist options
  • Save m7v/9808641 to your computer and use it in GitHub Desktop.
Save m7v/9808641 to your computer and use it in GitHub Desktop.
Create your Entity
/**
* Implements hook_schema().
*/
function spaint_promocode_schema() {
$schema['spaint_promocode'] = array(
// example (partial) specification for table "node"
'description' => t('The base table for nodes.'),
'fields' => array(
'id' => array(
'description' => t('The primary identifier for a node.'),
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'type' => array(
'description' => t('The type of this entity.'),
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
),
'target' => array(
'description' => t('The brushset that is activated.'),
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
),
'code' => array(
'description' => t('The value of this entity.'),
'type' => 'varchar',
'length' => 10,
'not null' => TRUE,
),
'uid' => array(
'description' => t('User id who used this code.'),
'type' => 'int',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0,
),
'activated' => array(
'description' => t('Value show used or not used this code.'),
'type' => 'int',
'unsigned' => FALSE,
'not null' => TRUE,
'default' => 0,
),
'expiration' => array(
'description' => t('Value show used or not used this code.'),
'type' => 'int',
'unsigned' => FALSE,
'not null' => FALSE,
'default' => NULL,
),
),
'primary key' => array('id'),
'unique keys' => array(
'code' => array('code'),
),
);
return $schema;
}
/**
* Implements hook_entity_info().
*/
function spaint_promocode_entity_info() {
return array(
'spaint_promocode' => array(
'label' => t('Promocode'),
'entity class' => 'Entity',
'controller class' => 'EntityAPIController',
'base table' => 'spaint_promocode',
'entity keys' => array(
'id' => 'id',
'target' => 'target',
'code' => 'code',
),
'admin ui' => array(
'path' => 'admin/structure/spaint_promocodes'
),
'access callback' => 'spaint_promocode_access',
'module' => 'spaint_promocode',
),
);
}
/**
* Check permisson.
*/
function spaint_promocode_access($op, $entity, $account = NULL, $entity_type = 'spaint_promocode') {
return user_access('access administration menu');
}
/**
* Return promocode by id.
*/
function spaint_promocode_load($id) {
$result = entity_load('spaint_promocode', array($id));
return $result ? reset($result) : FALSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment