Entityの管理フォーム:一覧表示/追加/削除/更新 操作ができるようにする
- 前回紹介した(DrupalのEntityを定義する簡単な例 )では、データを直接にDBに書き込むか、Feedsでcsvフォーマットデータをインポートする方法となります
- 前回の例に一つEntityプロパティを追加します(sys_name)
- これはコンテンツタイプなどを追加するときによく見られる名前とシステム名のペアです
- 今回の例では:名前は「room_number」、システム名は「sys_name」にします
- DBにもこのフィールドを追加します(カスタムモジュールのinstallファイルにあるスキーマに記入しました)
- モジュール(Entity API)がEnitityに関する管理フォームが提供しています
- 今回のカスタムモジュール(entity_example_2.zip)をダウンロードして、テストサイトにインストールし、動作を確認できます
- 作成されたEntity一覧の表示
- Entityの新規作成/編集/追加操作もあります
Entityの管理フォームを表示させるため「EntityAPIControllerExportable」を使用します
- Entityを定義(hook_entity_info() )する部分に「controller class」を「EntityAPIControllerExportable」を使用します
$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; } }
- Entity管理フォームをコンテンツ管理ページ(例)に挿入するために「admin ui」パラメターを定義します
/** * 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定義は「rooms.admin.inc」ファイルに書きこみます(room.module内で記述することもできます)
/** * 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の管理操作ができるようになります