function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
$services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'RoomController', // EntityAPIControllerExportableの使用 // 'controller class' => 'EntityAPIController',// <-前回使用したもの: デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 ・・・・・・・ ); // Entityの「controller class」のコールバッククラス class RoomController extends EntityAPIControllerExportable { public function __construct($entityType) { parent::__construct($entityType); } /** * Create a room - we first set up the values that are specific * to our room schema but then also go through the EntityAPIController * function. */ public function create(array $values = array()) { // Add values that are specific to our Room $values += array( 'rid' => '', 'is_new' => TRUE, 'type' => '', 'room_number' => '', 'sys_name' => '', ); $room = parent::create($values); return $room; } }
/** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 ・・・・・ 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', 'admin ui' => array( 'path' => 'admin/structure/rooms', 'file' => 'rooms.admin.inc', 'controller class' => 'RoomsUIController', ), ); return $services; }
/** * UI controller. */ class RoomsUIController extends EntityDefaultUIController { /** * Overrides hook_menu() defaults. */ public function hook_menu() { $items = parent::hook_menu(); $items[$this->path]['description'] = t('Manage rooms. including adding and removing fields and the display of fields.'); return $items; } } /** * Generates the room editing form. */ function room_form($form, &$form_state, $room, $op = 'edit') { if ($op == 'clone') { $room->sys_name .= ' (cloned)'; $room->room_number = ''; } $form['room_number'] = array( '#title' => t('Room Number'), '#type' => 'textfield', '#default_value' => isset($room->room_number)?$room->room_number : "", '#description' => t('Entry the Room number.'), '#required' => TRUE, '#size' => 30, ); // Machine-readable type name. $form['sys_name'] = array( '#type' => 'machine_name', '#default_value' => isset($room->sys_name) ? $room->sys_name : '', '#maxlength' => 32, '#machine_name' => array( 'exists' => 'room_get_sys_names', 'source' => array('label'), ), '#description' => t('A unique machine-readable name for this model type. It must only contain lowercase letters, numbers, and underscores.'), ); $form['type'] = array( '#title' => t('Room Type'), '#type' => 'textfield', '#default_value' => isset($room->type) ? $room->type : "", '#description' => t('Entry the Room type.'), '#required' => TRUE, '#size' => 60, ); $form['actions'] = array('#type' => 'actions'); $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save Room'), '#weight' => 40, ); return $form; } /** * Form API submit callback for the type form. */ function room_form_submit(&$form, &$form_state) { $room = entity_ui_form_submit_build_entity($form, $form_state); $room->save(); $form_state['redirect'] = 'admin/structure/rooms'; }
添付 | サイズ |
---|---|
entity_example_2.zip (3.28 KB) | 3.28 KB |
Roomのテストデータ(dsvフォーマット):
ルームナンバー: room_number
rid,room_number,type 1,1001,シングル 2,1002,シングル 3,1003,シングル 4,2001,ダブル 5,3001,ダブル
/** * Implement hook_schema() */ function entity_example_schema(){ $schema['room']=array( 'description' => 'Rooms', 'fields' => array( 'rid' => array( 'type' => 'serial', 'description' => 'Room Id' ), 'type' => array( 'type' => 'varchar', 'length' => '255', 'description' => 'Room Type' ), 'room_number' => array( 'type' => 'varchar', 'length' => '25', 'description' => 'Room Number' ), ), 'primary key' => array('rid'), ); return $schema; }
/** * Implements hook_entity_info(). */ /** * Implements hook_entity_info(). */ function entity_example_entity_info() { $services['room'] = array( 'label' => t('Room'), // エンティティ名 'entity class' => 'Entity', // デフォルトクラスの使用 'controller class' => 'EntityAPIController',// デフォルトクラスの使用 'base table' => 'room', // DB上のテーブル名 'fieldable' => TRUE, // 余分のフィールド追加可能 'entity keys' => array( // ルームIDをエンティティのキー. 'id' => 'rid', 'label' => 'room_number', // コールバック関数が優先 ), 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'bundles' => array(), // bundleはこのエンティティ自身 // 'label callback' => 'room_label', // 指定しないとラベルの取得ができない 'access callback' => 'room_access' , // default:'value user_access'動作しない 'module' => 'entity_example', 'metadata controller class' => 'EntityExampleMetadataController', ); return $services; } /** * Extend the defaults. */ class EntityExampleMetadataController extends EntityDefaultMetadataController { public function entityPropertyInfo() { $info = parent::entityPropertyInfo(); $properties = &$info[$this->type]['properties']; $properties['type'] = array( 'label' => t('Room type'), 'schema field' => 'type', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('Room type of the room'), ); $properties['room_number'] = array( 'label' => t('Room Number'), 'schema field' => 'room_number', 'getter callback' => 'entity_property_getter_method', 'setter callback' => 'entity_property_verbatim_set', 'required' => TRUE, 'description' => t('The number of the room'), ) return $info; } } function room_access($op, $room = NULL, $account = NULL) { // 全員アクセス可能にする return TRUE; }
添付 | サイズ |
---|---|
entity_example.zip (1.87 KB) | 1.87 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'controller class' => 'RoomController', 'base table' => 'room', 'uri callback' => 'room_uri', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', ), 'static cache' => TRUE, 'bundles' => array( // バンドルの指定 'room'=> array( // バンドル名: room 'label' => 'Room', 'admin' => array( 'path' => 'admin/structure/room/manage', // バンドルの管理URI 'access arguments' => array('administer rooms'), ), ), ), 'view modes' => array( 'full' => array( 'label' => t('Full Room'), 'custom settings' => FALSE, ), ) ); return $room_info; }
/** * Implements hook_menu(). */ function entity_room_menu() { // 管理が面にメッセージ表示のみ $items['admin/structure/room/manage'] = array( 'title' => 'Room Admin', 'description' => 'Manage Room structure', 'page callback' => 'room_info', 'access arguments' => array('administer rooms'), ); // 個別Room表示 $items['room/%entity_room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); // 新規Room作成 $items['room/add'] = array( 'title' => t('Add Room'), 'page callback' => 'room_add', 'access arguments' => array('create room'), ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * create "add room" form */ function room_add() { $room = (object) array ( 'rid' => '', 'type' => 'room', 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); field_attach_form('room', $room, $form, $form_state); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate('room', $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit('room', $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { return entity_get_controller('room')->save($room); } function room_info() { return t('Welcome to the administration page for your rooms!'); } function room_page_title($room){ return $room->room_number; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } /** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); $return['room']['room'] = array( 'form' => array( 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 1, ), ), ); return $return; } function room_uri( $room ){ return array( 'path' => 'room/' . $room->rid, ); } class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * Implements hook_load(). */ function entity_room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.55 KB) | 2.55 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * 追加されたバンドル(RoomType)に拡張フィールド操作URIの追加 * Implements hook_entity_info_alter(). */ function entity_room_entity_info_alter(&$entity_info) { foreach (room_types() as $type => $info) { $entity_info['room']['bundles'][$type] = array( 'label' => $info->room_type_label, 'admin' => array( 'path' => 'admin/structure/room-types/manage/%room_type', 'real path' => 'admin/structure/room-types/manage/' . $type, 'bundle argument' => 4, ), ); } } // room_type_sys でデータの取得 // room_type入力がなければ、全部のRoomTypeを返す function room_type_load($room_type=null){ return room_types( $room_type ) ; } function room_types( $room_type = null ){ $types = entity_load_multiple_by_name( 'room_type', isset($room_type) ? array($room_type) : false ) ; return isset( $room_type ) ? reset($types) : $types ; }
添付 | サイズ |
---|---|
custom_table_2.zip (1.67 KB) | 1.67 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * 追加されたバンドル(RoomType)に拡張フィールド操作URIの追加 * Implements hook_entity_info_alter(). */ function entity_room_entity_info_alter(&$entity_info) { foreach (room_types() as $type => $info) { $entity_info['room']['bundles'][$type] = array( 'label' => $info->room_type_label, 'admin' => array( 'path' => 'admin/structure/room-types/manage/%room_type', 'real path' => 'admin/structure/room-types/manage/' . $type, 'bundle argument' => 4, ), ); } } // room_type_sys でデータの取得 // room_type入力がなければ、全部のRoomTypeを返す function room_type_load($room_type=null){ return room_types( $room_type ) ; } function room_types( $room_type = null ){ $types = entity_load_multiple_by_name( 'room_type', isset($room_type) ? array($room_type) : false ) ; return isset( $room_type ) ? reset($types) : $types ; }
添付 | サイズ |
---|---|
custom_table_2.zip (1.67 KB) | 1.67 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * 追加されたバンドル(RoomType)に拡張フィールド操作URIの追加 * Implements hook_entity_info_alter(). */ function entity_room_entity_info_alter(&$entity_info) { foreach (room_types() as $type => $info) { $entity_info['room']['bundles'][$type] = array( 'label' => $info->room_type_label, 'admin' => array( 'path' => 'admin/structure/room-types/manage/%room_type', 'real path' => 'admin/structure/room-types/manage/' . $type, 'bundle argument' => 4, ), ); } } // room_type_sys でデータの取得 // room_type入力がなければ、全部のRoomTypeを返す function room_type_load($room_type=null){ return room_types( $room_type ) ; } function room_types( $room_type = null ){ $types = entity_load_multiple_by_name( 'room_type', isset($room_type) ? array($room_type) : false ) ; return isset( $room_type ) ? reset($types) : $types ; }
添付 | サイズ |
---|---|
custom_table_2.zip (1.67 KB) | 1.67 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * 追加されたバンドル(RoomType)に拡張フィールド操作URIの追加 * Implements hook_entity_info_alter(). */ function entity_room_entity_info_alter(&$entity_info) { foreach (room_types() as $type => $info) { $entity_info['room']['bundles'][$type] = array( 'label' => $info->room_type_label, 'admin' => array( 'path' => 'admin/structure/room-types/manage/%room_type', 'real path' => 'admin/structure/room-types/manage/' . $type, 'bundle argument' => 4, ), ); } } // room_type_sys でデータの取得 // room_type入力がなければ、全部のRoomTypeを返す function room_type_load($room_type=null){ return room_types( $room_type ) ; } function room_types( $room_type = null ){ $types = entity_load_multiple_by_name( 'room_type', isset($room_type) ? array($room_type) : false ) ; return isset( $room_type ) ? reset($types) : $types ; }
添付 | サイズ |
---|---|
custom_table_2.zip (1.67 KB) | 1.67 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * 追加されたバンドル(RoomType)に拡張フィールド操作URIの追加 * Implements hook_entity_info_alter(). */ function entity_room_entity_info_alter(&$entity_info) { foreach (room_types() as $type => $info) { $entity_info['room']['bundles'][$type] = array( 'label' => $info->room_type_label, 'admin' => array( 'path' => 'admin/structure/room-types/manage/%room_type', 'real path' => 'admin/structure/room-types/manage/' . $type, 'bundle argument' => 4, ), ); } } // room_type_sys でデータの取得 // room_type入力がなければ、全部のRoomTypeを返す function room_type_load($room_type=null){ return room_types( $room_type ) ; } function room_types( $room_type = null ){ $types = entity_load_multiple_by_name( 'room_type', isset($room_type) ? array($room_type) : false ) ; return isset( $room_type ) ? reset($types) : $types ; }
添付 | サイズ |
---|---|
custom_table_2.zip (1.67 KB) | 1.67 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * 追加されたバンドル(RoomType)に拡張フィールド操作URIの追加 * Implements hook_entity_info_alter(). */ function entity_room_entity_info_alter(&$entity_info) { foreach (room_types() as $type => $info) { $entity_info['room']['bundles'][$type] = array( 'label' => $info->room_type_label, 'admin' => array( 'path' => 'admin/structure/room-types/manage/%room_type', 'real path' => 'admin/structure/room-types/manage/' . $type, 'bundle argument' => 4, ), ); } } // room_type_sys でデータの取得 // room_type入力がなければ、全部のRoomTypeを返す function room_type_load($room_type=null){ return room_types( $room_type ) ; } function room_types( $room_type = null ){ $types = entity_load_multiple_by_name( 'room_type', isset($room_type) ? array($room_type) : false ) ; return isset( $room_type ) ? reset($types) : $types ; }
添付 | サイズ |
---|---|
custom_table_2.zip (1.67 KB) | 1.67 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * 追加されたバンドル(RoomType)に拡張フィールド操作URIの追加 * Implements hook_entity_info_alter(). */ function entity_room_entity_info_alter(&$entity_info) { foreach (room_types() as $type => $info) { $entity_info['room']['bundles'][$type] = array( 'label' => $info->room_type_label, 'admin' => array( 'path' => 'admin/structure/room-types/manage/%room_type', 'real path' => 'admin/structure/room-types/manage/' . $type, 'bundle argument' => 4, ), ); } } // room_type_sys でデータの取得 // room_type入力がなければ、全部のRoomTypeを返す function room_type_load($room_type=null){ return room_types( $room_type ) ; } function room_types( $room_type = null ){ $types = entity_load_multiple_by_name( 'room_type', isset($room_type) ? array($room_type) : false ) ; return isset( $room_type ) ? reset($types) : $types ; }
添付 | サイズ |
---|---|
custom_table_2.zip (1.67 KB) | 1.67 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * 追加されたバンドル(RoomType)に拡張フィールド操作URIの追加 * Implements hook_entity_info_alter(). */ function entity_room_entity_info_alter(&$entity_info) { foreach (room_types() as $type => $info) { $entity_info['room']['bundles'][$type] = array( 'label' => $info->room_type_label, 'admin' => array( 'path' => 'admin/structure/room-types/manage/%room_type', 'real path' => 'admin/structure/room-types/manage/' . $type, 'bundle argument' => 4, ), ); } } // room_type_sys でデータの取得 // room_type入力がなければ、全部のRoomTypeを返す function room_type_load($room_type=null){ return room_types( $room_type ) ; } function room_types( $room_type = null ){ $types = entity_load_multiple_by_name( 'room_type', isset($room_type) ? array($room_type) : false ) ; return isset( $room_type ) ? reset($types) : $types ; }
添付 | サイズ |
---|---|
custom_table_2.zip (1.67 KB) | 1.67 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * 追加されたバンドル(RoomType)に拡張フィールド操作URIの追加 * Implements hook_entity_info_alter(). */ function entity_room_entity_info_alter(&$entity_info) { foreach (room_types() as $type => $info) { $entity_info['room']['bundles'][$type] = array( 'label' => $info->room_type_label, 'admin' => array( 'path' => 'admin/structure/room-types/manage/%room_type', 'real path' => 'admin/structure/room-types/manage/' . $type, 'bundle argument' => 4, ), ); } } // room_type_sys でデータの取得 // room_type入力がなければ、全部のRoomTypeを返す function room_type_load($room_type=null){ return room_types( $room_type ) ; } function room_types( $room_type = null ){ $types = entity_load_multiple_by_name( 'room_type', isset($room_type) ? array($room_type) : false ) ; return isset( $room_type ) ? reset($types) : $types ; }
添付 | サイズ |
---|---|
custom_table_2.zip (1.67 KB) | 1.67 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * 追加されたバンドル(RoomType)に拡張フィールド操作URIの追加 * Implements hook_entity_info_alter(). */ function entity_room_entity_info_alter(&$entity_info) { foreach (room_types() as $type => $info) { $entity_info['room']['bundles'][$type] = array( 'label' => $info->room_type_label, 'admin' => array( 'path' => 'admin/structure/room-types/manage/%room_type', 'real path' => 'admin/structure/room-types/manage/' . $type, 'bundle argument' => 4, ), ); } } // room_type_sys でデータの取得 // room_type入力がなければ、全部のRoomTypeを返す function room_type_load($room_type=null){ return room_types( $room_type ) ; } function room_types( $room_type = null ){ $types = entity_load_multiple_by_name( 'room_type', isset($room_type) ? array($room_type) : false ) ; return isset( $room_type ) ? reset($types) : $types ; }
添付 | サイズ |
---|---|
custom_table_2.zip (1.67 KB) | 1.67 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }
添付 | サイズ |
---|---|
entity_room.zip (5.08 KB) | 5.08 KB |
/** * Implements hook_entity_info(). */ function entity_room_entity_info() { $room_info['room'] = array( 'label' => t('Room'), 'entity class' => 'Entity', 'controller class' => 'DrupalDefaultEntityController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'label' => 'room_number', 'bundle' => 'room_type', // DB上にroom_typeフィールドでバンドルを区別 ), 'bundles' => array( 'single_room'=> array( 'label' => 'Single Room', 'admin' => array( 'path' => 'admin/structure/single-room/manage', ), ), 'twin_room'=> array( 'label' => 'Twin Room', 'admin' => array( 'path' => 'admin/structure/twin-room/manage', ), ), ), 'access callback' => 'access_room', 'module' => 'entity_room', ); return $room_info; }
/** * Implements hook_field_extra_fields(). * Field管理にRoomのプロパティ項目を追加(編集不可) */ function entity_room_field_extra_fields() { $return = array(); // For Single Room Bundle $return['room']['single_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); // For Twin Room Bundle $return['room']['twin_room'] = array( 'form' => array( 'room_type' => array( 'label' => t('Room Type'), 'description' => t('The type of the room'), 'weight' => 1, ), 'room_number' => array( 'label' => t('Room Number'), 'description' => t('The number of the room'), 'weight' => 2, ), ), ); return $return; }
/** * Implements hook_menu(). */ function entity_room_menu() { // シングル ルーム 管理画面 $items['admin/structure/single-room/manage'] = array( 'title' => t('Single Room Admin'), 'description' => 'Manage Single Room structure', 'page callback' => 'room_info', 'page arguments' => array('Single Room') , 'access arguments' => array('administer rooms'), ); $items['single-room/add'] = array( 'title' => t('Add Single Room'), 'page callback' => 'room_add', 'page arguments' => array('single_room'), 'access arguments' => array('create room'), ); // ツインルーム管理が画面 $items['admin/structure/twin-room/manage'] = array( 'title' => 'Twin Room Admin', 'description' => 'Manage Twin Room structure', 'page callback' => 'room_info' , 'page arguments' => array('Twin Room'), 'access arguments' => array('administer rooms'), ); $items['twin-room/add'] = array( 'title' => t('Add Twin Room'), 'page callback' => 'room_add', 'page arguments' => array('twin_room'), 'access arguments' => array('create room'), ); // Roomの表示 $items['room/%room'] = array( 'title callback' => 'room_page_title', 'title arguments' => array(1), 'page callback' => 'room_page_view', 'page arguments' => array(1), 'access arguments' => array('view rooms'), 'type' => MENU_CALLBACK, ); return $items; } /** * Implements hook_permission(). */ function entity_room_permission() { return array( 'administer rooms' => array( 'title' => t('Administer rooms'), 'restrict access' => TRUE, ), 'create room' => array( 'title' => t('Create room'), ), 'view rooms' => array( 'title' => t('View Rooms'), ) ); } /** * 全員にRoomエンティティの無条件アクセス権限 */ function access_room(){ return true ; } /** * create "add room" form */ function room_add($type) { $room = (object) array ( 'rid' => '', 'room_type' => $type, 'room_number' => '', ); return drupal_get_form('room_add_form', $room); } function room_add_form($form, &$form_state, $room) { $form['room_type'] = array( '#type' => 'textfield', '#title' => t('Room Type'), '#default_value' => $room->room_type, '#disabled' => true, '#required' => TRUE, ); $form['room_number'] = array( '#type' => 'textfield', '#title' => t('Room Number'), '#required' => TRUE, ); field_attach_form('room', $room, $form, $form_state); $form['submit'] = array( '#type' => 'submit', '#value' => t('Save'), '#weight' => 10, ); return $form; } function room_add_form_validate($form, &$form_state) { $room_submisttion = (object) $form_state['values']; field_attach_form_validate($form_state['values']['room_type'], $room_submisttion, $form, $form_state); } function room_add_form_submit($form, &$form_state) { $room_submission = (object) $form_state['values']; field_attach_submit($form_state['values']['room_type'], $room_submission, $form, $form_state); $room = room_save($room_submission); $form_state['redirect'] = "room/$room->rid"; } function room_save(&$room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } function room_info($type) { return t('Welcome to the administration page for your '.$type.'!'); } function room_page_title($room){ return $room->room_number."(".$room->room_type.")"; } function room_page_view($room, $view_mode = 'full'){ $room->content = array(); // Build fields content. field_attach_prepare_view('room', array($room->rid => $room), $view_mode); entity_prepare_view('room', array($room->rid => $room)); $room->content += field_attach_view('room', $room, $view_mode); return $room->content; } function room_uri( $room ){ return array( 'path' => 'room/'.$room->rid , ); } class RoomController extends EntityAPIController{ //class RoomController extends DrupalDefaultEntityController{ public function __construct($entityType) { parent::__construct($entityType); } // Entityオブジェクト保存 public function save($room) { drupal_write_record('room', $room); field_attach_insert('room', $room); return $room; } } /** * hook_menuのURL引数のオブジェクト取得 */ function room_load($rid = NULL, $reset = FALSE){ $rids = (isset ($rid) ? array($rid) : array()); $room = room_load_multiple($rids, $reset); return $room ? reset ($room) : FALSE; } function room_load_multiple($rids = array(), $conditions = array(), $reset = FALSE){ return entity_load('room', $rids, $conditions, $reset); }
添付 | サイズ |
---|---|
entity_room.zip (2.84 KB) | 2.84 KB |
/** * 追加されたバンドル(RoomType)に拡張フィールド操作URIの追加 * Implements hook_entity_info_alter(). */ function entity_room_entity_info_alter(&$entity_info) { foreach (room_types() as $type => $info) { $entity_info['room']['bundles'][$type] = array( 'label' => $info->room_type_label, 'admin' => array( 'path' => 'admin/structure/room-types/manage/%room_type', 'real path' => 'admin/structure/room-types/manage/' . $type, 'bundle argument' => 4, ), ); } } // room_type_sys でデータの取得 // room_type入力がなければ、全部のRoomTypeを返す function room_type_load($room_type=null){ return room_types( $room_type ) ; } function room_types( $room_type = null ){ $types = entity_load_multiple_by_name( 'room_type', isset($room_type) ? array($room_type) : false ) ; return isset( $room_type ) ? reset($types) : $types ; }
添付 | サイズ |
---|---|
custom_table_2.zip (1.67 KB) | 1.67 KB |
function entity_room_entity_info() { $return = array( 'room' => array( 'label' => t('Room'), 'entity class' => 'RoomEntity', 'label callback' => 'entity_class_label', // see RoomEntity class 'uri callback' => 'entity_class_uri', // see RoomEntity class 'controller class' => 'RoomController', 'base table' => 'room', 'fieldable' => TRUE, 'entity keys' => array( 'id' => 'rid', 'bundle' => 'room_type_sys', ), 'bundle keys' => array( 'bundle' => 'room_type_sys', ), 'bundles' => array(), 'load hook' => 'room_load', 'view modes' => array( 'full' => array( 'label' => t('Default'), 'custom settings' => FALSE, ), ), 'module' => 'entity_room', 'access callback' => 'entity_room_access', ), ); $return['room_type'] = array( 'label' => t('Room Type'), 'entity class' => 'RoomTypeEntity', 'controller class' => 'RoomTypeController', 'base table' => 'room_type', 'fieldable' => FALSE, 'bundle of' => 'room', 'exportable' => TRUE, 'entity keys' => array( 'id' => 'rtid', 'name' => 'room_type_sys', 'label' => 'room_type_label', ), 'module' => 'entity_room', 'admin ui' => array( // Enable the entity API's admin UI. 'path' => 'admin/structure/room-types', 'file' => 'room_type.admin.inc', 'controller class' => 'RoomTypeUIController', ), 'access callback' => 'entity_room_access', ); return $return; }