Skip to content

Instantly share code, notes, and snippets.

@jaydonnell
Created October 25, 2011 19:15
Show Gist options
  • Save jaydonnell/1313902 to your computer and use it in GitHub Desktop.
Save jaydonnell/1313902 to your computer and use it in GitHub Desktop.
function character_schema() {
$schema['character'] = array (
'description' => 'The main store for our entity',
'fields' => array(
'pid' => array(
'description' => 'Primary key for our table of characters',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'note' => array(
'description' => 'The actual note',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE
),
),
'primary key' => array('pid'),
);
return $schema;
}
function character_entity_info(){
$character_info['character'] = array(
'label' => t('Character Note'),
'controller class' => 'CharacterController',
'base table' => 'character',
'uri callback' => 'character_uri',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'pid',
),
'static cache' => TRUE,
'bundles' => array(
'character'=> array(
'label' => 'Character',
'admin' => array(
'path' => 'admin/structure/character/manage',
'access arguments' => array('administer characters'),
),
),
),
'view modes' => array(
'full' => array(
'label' => t('Full Character'),
'custom settings' => FALSE,
),
)
);
return $character_info;
}
function character_uri($character){
return array(
'path' => 'character/' . $character->id,
);
}
function character_load($pid = NULL, $reset = FALSE){
$pids = (isset ($pid) ? array($pid) : array());
$character = character_load_multiple($pids, $reset);
return $character ? reset ($character) : FALSE;
}
function character_load_multiple($pids = array(), $conditions = array(), $reset = FALSE){
return entity_load('character', $pids, $conditions, $reset);
}
function character_menu(){
$items['character/add'] = array(
'title' => 'Add Character!',
'page callback' => 'character_add',
'access arguments' => array('create character'),
);
$items['admin/structure/character/manage'] = array(
'title' => 'Character Admin',
'description' => 'Manage Character structure',
'page callback' => 'character_info',
'access arguments' => array('administer characters'),
);
$items['character/%character'] = array(
'title callback' => 'character_page_title',
'title arguments' => array(1),
'page callback' => 'character_page_view',
'page arguments' => array(1),
'access arguments' => array('view characters'),
'type' => MENU_CALLBACK,
);
return $items;
}
function character_permission(){
return array(
'administer characters' => array(
'title' => t('Administer characters'),
'restrict access' => TRUE,
),
'view postsits' => array(
'title' => t('View Characters'),
)
);
}
function character_info() {
return ('Welcome to the administration page for your Characters!');
}
function character_page_title($character){
return $character->pid;
}
function character_page_view($character, $view_mode = 'full'){
$character->content = array();
// Build fields content.
field_attach_prepare_view('character', array($character->pid => $character), $view_mode);
entity_prepare_view('character', array($character->pid => $character));
$character->content += field_attach_view('character', $character, $view_mode);
return $character->content;
}
function character_field_extra_fields() {
$return = array();
$return['character']['character'] = array(
'form' => array(
'note' => array(
'label' => t('Note'),
'description' => t('Character Note'),
),
),
);
return $return;
}
function character_add() {
$character = (object) array (
'pid' => '',
'type' => 'character',
'note' => '',
);
return drupal_get_form('character_add_form', $character);
}
function character_add_form($form, &$form_state, $character) {
$form['note'] = array(
'#type' => 'textfield',
'#title' => t('Note'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
field_attach_form('character', $character, $form, $form_state);
return $form;
}
function character_add_form_validate($form, &$form_state) {
$character_submisttion = (object) $form_state['values'];
field_attach_form_validate('character', $character_submisttion, $form, $form_state);
}
function character_add_form_submit($form, &$form_state) {
$character_submission = (object) $form_state['values'];
field_attach_submit('character', $character_submission, $form, $form_state);
$character = character_save($character_submission);
$form_state['redirect'] = "character/$character->pid";
}
function character_save(&$character) {
return entity_get_controller('character')->save($character);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment