php - drupal entity implementation, there is a mistake somewhere -
i'm starting out drupal , poking around entity api. i've tried write implementation not meant production, test case.
but can't work. when try save new candidate error message: creating default object empty value in entity_form_submit_build_entity() (line 8213 of /wwwroot/drupal/public_html/includes/common.inc).
the entity not being added database. it's stupid error can't track down. if more experience me out grateful.
the audition.module file:
<?php ###database schemas function audition_schema(){ $schema['audition_candidate'] = array( 'description'=>'stores candidates audition', 'fields'=>array( 'id'=>array( 'description'=>'identifier candidate', 'type'=>'serial', 'not null'=>true, 'unsigned'=>true, ), 'lastname'=>array( 'description'=>'candidate last name', 'type'=>'varchar', 'length'=>'50', 'not null'=>true, ), 'firstname'=>array( 'description'=>'candidate first name', 'type'=>'varchar', 'length'=>'50', 'not null'=>true, ), 'mailadress'=>array( 'description'=>'candidate mail adress', 'type'=>'varchar', 'length'=>'255', 'not null'=>true, ), 'vocalgroup'=>array( 'description'=>'candidate vocal group type', 'type'=>'int', 'length'=>'small', 'not null'=>true, ), ), 'primary key' =>array('id'), 'label' => array('lastname','firstname'), ); $schema['vocal_group'] = array( 'description'=>'stores different vocal groups', 'fields'=>array( 'id'=>array( 'description'=>'identifier vocal group', 'type'=>'serial', 'not null'=>true, 'unsigned'=>true, ), 'group'=>array( 'description'=>'vocal group name', 'type'=>'varchar', 'length'=>'50', 'not null'=>true, ), ), 'primary key' =>array('id'), 'uri callback' => 'entity_class_uri', ); return $schema; } ###entities function audition_entity_info(){ $entity['candidate']= array( 'label'=>t('audition candidate'), 'plural label'=>t('audition candidates'), 'base table' => 'audition_candidate', 'entity class' => 'candidateentity', 'entity keys' => array( 'id'=>'id', 'label' => 'lastname'), 'admin ui' => array( 'path' => 'admin/candidate', 'controllerclass' => 'entitydefaultuicontroller', 'menu wildcard' => '%candidate', 'file' => 'candidate.admin.inc', ), 'module' => 'audition', 'access callback' => 'candidate_access_callback', ); $entity['vocalgroup'] = array( 'label' => t('audition candidate vocal group'), 'plural label' => t('vocal groups'), 'base table' => 'vocal_group', 'entity keys' => array( 'id'=>'id', ), ); /*dsm($entity);*/ return $entity; } class candidateentity extends entity { protected function defaulturi() { return array('path' => 'candidate/' . $this->identifier()); } } function candidate_access_callback($op, $candidate = null, $account= null){ if($op == 'view' && user_access('view_candidates',$account)) { return true; } else if(user_access('administer_candidates',$account)) { return true; } return false; } ###install script ###permissions function audition_permission(){ $permission['administer_candidates'] = array( 'title' => t('edit candidates'), 'description' => t('can make changes candidate date'), ); $permission['view_candidates'] = array( 'title'=> t('view candidates'), 'description' => t('can view candidates') ); return $permission; } ###menu hooks function audition_menu(){ $menuitems['audition'] = array( 'page callback'=> 'audition_page', 'access callback' => true, ); $menuitems['candidate/%candidate'] = array( 'title' => 'audition candidate', 'page callback' => 'candidate_entity_view', 'page arguments' => array(1), 'access callback' => true, ); return $menuitems; } ###page callback functions function audition_page() { $candidate= entity_load('candidate', array(1)); /*dsm($candidate);*/ return 'test'; } function candidate_load($candidateid) { $candidate = entity_load('candidate',array($candidateid)); dsm($candidate); return array_pop($candidate); } function candidate_entity_view($candidate) { return 'test'; }
and candidate.admin.inc:
<?php /* * change license header, choose license headers in project properties. * change template file, choose tools | templates * , open template in editor. */ function candidate_form($form, &$form_state, $candidate){ $form['lastname'] = array( '#title' => t('last name'), '#type' => 'textfield', '#default_value' => isset($candidate->lastname) ? $candidate->lastname : '', '#description' => 'fill out last name', '#required' => true, ); $form['firstname'] = array( '#title' => t('first name'), '#type' => 'textfield', '#default_value' => isset($candidate->firstname) ? $candidate->firstname : '', '#description' => 'fill out first name', '#required' => true, ); $form['mailadress'] = array( '#title' => t('mailadress'), '#type' => 'textfield', '#default_value' => isset($candidate->mailadress) ? $candidate->mailadress : '', '#description' => 'fill out valid mailadress', '#required' => true, ); $form['vocalgroup'] = array( '#title' => t('vocal group'), '#type' => 'textfield', '#default_value' => isset($candidate->vocalgroup) ? $candidate->vocalgroup : '', '#description' => 'to wich vocal group belong?', '#required' => true, ); $form['actions'] = array( '#type' => 'actions', 'submit' => array( '#type' => 'submit', '#value' => t('submit'), ), ); return $form; } function candidate_form_submit($form, &$form_state){ $candidate = entity_ui_form_submit_build_entity($form, $form_state); dsm($candidate); $candidate->save(); drupal_set_message(t('you created candidate')); $form_state['redirect'] = 'admin/candidate'; }
Comments
Post a Comment