要利用程式來建立內容類型,首要先建立好新內容類型的資料陣列,再透過node_type_save()這個函式來儲存這個內容類型。作法如下:
//建立新內容類型的資料陣列
$my_content_type = array(
'type' => 'my_content', //machine name
'name' => st('My Content'), //human readable name
'base' => 'node_content', //
'description' => st('This is my content'), //內容類型說明
'custom' => TRUE, //若為true表示是由user建立的,若為false則表示由模組建立
'modified' => TRUE, //true表示已被管理者修改過,目前沒有用處
'locked' => FALSE, //true表示管理者可以變更machine name
'disabled'=>FALSE, //true表示本內容類型已停用
'has_title'=>TRUE, //表示有使用title欄位
'title_label'=>'標題',//設定title的欄位標籤
// 'module'=>'my_module',//設定建立此內容類型的模組名稱
// 'orid_type'=>若locked為True,則本欄位為原本的machine name
);
//為本內容類型提供欄位的預設值
$my_content_type = node_type_set_defaults($my_content_type);
//儲存本內容類型
node_type_save($my_content_type);
//為本內容類型增加一個body欄位
node_add_body_field($my_content_type);