아래에 제공된 customcompanymodule 코드를 사용하여 다양한 필드로 프로그래밍 방식으로 컨텐츠 유형을 작성할 수 있습니다.
이 코드를 사용자 정의 모듈의 .install 파일에 추가 할 수 있습니다. 프로그래밍 방식으로 "회사"라는 컨텐츠 유형과 다양한 유형의 필드 (텍스트, 숫자, 날짜 (참고 : 날짜 필드는 기본적으로 제공되지 않으므로 날짜 모듈을 설치해야 함), 이미지, 목록)를 추가합니다.
또한 'customcompanymodule'모듈을 제거 할 때 컨텐츠 유형 "company"를 해당 필드 및 데이터와 함께 제거하는 제거 코드를 추가했습니다.
필요에 따라이 필드를 수정 / 제거 할 수 있습니다.
function customcompanymodule_install() {
$t = get_t();
node_types_rebuild();
$company = array(
'type' => 'company',
'name' => $t('Company'),
'base' => 'node_content',
'module' => 'node',
'description' => $t('Content type to handle companys.'),
'body_label' => $t('Company Description'),
'title_label' => $t('Company Title'),
'promote' => 0,
'status' => 1,
'comment' => 0,
);
$content_type = node_type_set_defaults($company);
node_type_save($content_type);
foreach (_company_installed_fields() as $field) {
field_create_field($field);
}
foreach (_company_installed_instances() as $instance) {
$instance['entity_type'] = 'node';
$instance['bundle'] = 'company';
field_create_instance($instance);
}
$weight = db_query("SELECT weight FROM {system} WHERE name = :name", array(':name' => 'categories'))->fetchField();
db_update('system')->fields(array(
'weight' => $weight + 1,
))
->condition('name', 'company')
->execute();
}
function _company_installed_fields() {
$t = get_t();
$fields = array(
'company_startdate' => array(
'field_name' => 'company_startdate',
'label' => $t('Company Start Date'),
'cardinality' => 1,
'type' => 'datetime',
'module' => 'date',
'settings' => array(
'granularity' => array(
'month' => 'month',
'day' => 'day',
'hour' => 'hour',
'minute' => 'minute',
'year' => 'year',
'second' => 0,
),
'tz_handling' => 'site',
'timezone_db' => 'UTC',
'cache_enabled' => 0,
'cache_count' => '4',
'todate' => 'required',
),
),
'company_totalwinners' => array(
'field_name' => 'company_totalwinners',
'label' => $t('Maximum Company Winners'),
'cardinality' => 1,
'type' => 'number_integer',
'module' => 'number',
'settings' => array(
'max_length' => 10000,
),
),
'company_minwinner' => array(
'field_name' => 'company_minwinner',
'label' => $t('Minimum Entries for Company to Activate'),
'cardinality' => 1,
'type' => 'number_integer',
'module' => 'number',
'settings' => array(
'max_length' => 10000,
),
),
'company_totalentries' => array(
'field_name' => 'company_totalentries',
'label' => $t('Company Total Entries'),
'cardinality' => 1,
'type' => 'number_integer',
'module' => 'number',
'settings' => array(
'max_length' => 10000,
),
),
'company_points' => array(
'field_name' => 'company_points',
'label' => $t('Company Points'),
'cardinality' => 1,
'type' => 'number_integer',
'module' => 'number',
'settings' => array(
'max_length' => 10000,
),
),
'company_image' => array(
'field_name' => 'company_image',
'label' => $t('Image'),
'cardinality' => 1,
'type' => 'image',
'settings' => array(
'default_image' => 0,
'uri_scheme' => 'public',
),
),
'company_description' => array(
'field_name' => 'company_description',
'label' => $t('Company Description'),
'cardinality' => 1,
'type' => 'text',
'module' => 'text',
'length' => '255'
),
'company_winner' => array(
'field_name' => 'company_winner',
'label' => $t('Company Description'),
'cardinality' => 1,
'type' => 'text',
'module' => 'text',
'length' => '255'
),
'field_autowinnerselection' => array(
'field_name' => 'field_autowinnerselection',
'label' => $t('Auto Company Winner Selection'),
'type' => 'list_boolean',
'module' => 'list',
'active' => '1',
'locked' => '0',
'cardinality' => '1',
'deleted' => '0'
),
);
return $fields;
}
function _company_installed_instances() {
$t = get_t();
$instances = array(
'company_startdate' => array(
'field_name' => 'company_startdate',
'label' => $t('Company Lifespan'),
'cardinality' => 1,
'widget' => array(
'type' => 'date_popup',
'module' => 'date',
'settings' => array(
'input_format' => 'm/d/Y - H:i:s',
'input_format_custom' => '',
'year_range' => '-3:+3',
'increment' => '15',
'label_position' => 'above',
'text_parts' => array(),
),
),
),
'company_totalwinners' => array(
'field_name' => 'company_totalwinners',
'label' => $t('Maximum Company Winners'),
'cardinality' => 1,
'widget' => array(
'type' => 'number',
'module' => 'number',
'settings' => array('size' => 60),
),
),
'company_minwinner' => array(
'field_name' => 'company_minwinner',
'label' => $t('Minimum Number of Entries for Company to Activate'),
'cardinality' => 1,
'required' => 1,
'widget' => array(
'type' => 'number',
'module' => 'number',
'settings' => array('size' => 60),
),
),
'company_totalentries' => array(
'field_name' => 'company_totalentries',
'label' => $t('Company Total Entries'),
'cardinality' => 1,
'required' => 1,
'widget' => array(
'type' => 'number',
'module' => 'number',
'settings' => array('size' => 60),
),
),
'company_points' => array(
'field_name' => 'company_points',
'label' => $t('Company Points'),
'cardinality' => 1,
'required' => 1,
'widget' => array(
'type' => 'number',
'module' => 'number',
'settings' => array('size' => 60),
),
),
'company_image' => array(
'field_name' => 'company_image',
'label' => $t('Image'),
'cardinality' => 1,
'required' => 1,
'type' => 'company_image',
'settings' => array(
'max_filesize' => '',
'max_resolution' => '213x140',
'min_resolution' => '213x140',
'alt_field' => 1,
'default_image' => 0
),
'widget' => array(
'settings' => array(
'preview_image_style' => 'thumbnail',
'progress_indicator' => 'throbber',
),
),
'display' => array(
'default' => array(
'label' => 'hidden',
'type' => 'image',
'settings' => array('image_style' => 'medium', 'image_link' => ''),
'weight' => -1,
),
'teaser' => array(
'label' => 'hidden',
'type' => 'image',
'settings' => array('image_style' => 'thumbnail', 'image_link' => 'content'),
'weight' => -1,
),
),
),
'company_description' => array(
'field_name' => 'company_description',
'label' => $t('Company Description'),
'cardinality' => 1,
'widget' => array(
'weight' => '-3',
'type' => 'text_textfield',
'module' => 'text',
'active' => 1,
'settings' => array(
'size' => '1000',
),
),
),
'company_winner' => array(
'field_name' => 'company_winner',
'label' => $t('Company Winner'),
'cardinality' => 1,
'widget' => array(
'weight' => '-3',
'type' => 'text_textfield',
'module' => 'text',
'active' => 1,
'settings' => array(
'size' => '60',
),
),
),
'field_autowinnerselection' => array(
'field_name' => 'field_autowinnerselection',
'required' => 1,
'label' => $t('Auto Company Winner Selection'),
'widget' => array(
'weight' => '-3',
'type' => 'options_buttons',
'module' => 'options',
'active' => 1,
'settings' => array(),
),
),
);
return $instances;
}
function customcompanymodule_uninstall() {
$content_types = array(
'name1' => 'company',
);
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type1';
$result = db_query($sql, array(':type1' => $content_types['name1']));
$nids = array();
foreach ($result as $row) {
$nids[] = $row->nid;
}
node_delete_multiple($nids);
node_type_delete($content_types['name1']);
field_purge_batch(1000);
}
node_type_set_defaults()
그리고node_type_save()
물론, 그리고을 살펴 봐야hook_install()
합니다.