Created
November 14, 2015 16:04
-
-
Save massiws/7db0801faf2f8cea5d3c to your computer and use it in GitHub Desktop.
Drupal 7: create custom content type in custom module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_install(). | |
*/ | |
function customodule_install() { | |
node_types_rebuild(); | |
$types = node_type_get_types(); | |
node_add_body_field($types['my_content_type']); | |
add_custom_fields(); | |
} | |
/* | |
* Add extra fields. | |
*/ | |
function add_custom_fields() { | |
foreach (_customodule_installed_fields() as $field) { | |
field_create_field($field); | |
} | |
foreach (_customodule_installed_instances() as $fieldinstance) { | |
$fieldinstance['entity_type'] = 'node'; | |
$fieldinstance['bundle'] = 'my_content_type'; | |
field_create_instance($fieldinstance); | |
} | |
} | |
/* | |
* Define the fields. | |
*/ | |
function _customodule_installed_fields() { | |
$t = get_t(); | |
return array( | |
'customodule_textfield1' => array( | |
'field_name' => 'customodule_textfield1', | |
'label' => $t('Custom text field 1'), | |
'type' => 'text', | |
), | |
'customodule_textfield2' => array( | |
'field_name' => 'customodule_textfield2', | |
'label' => $t('Custom text field 2'), | |
'type' => 'text', | |
), | |
); | |
} | |
/* | |
* Define the field instances. | |
*/ | |
function _customodule_installed_instances() { | |
$t = get_t(); | |
return array( | |
'customodule_textfield1' => array( | |
'field_name' => 'customodule_textfield1', | |
'type' => 'text', | |
'label' => $t('Custom text field 1'), | |
'widget' => array( | |
'type' => 'text_textfield', | |
), | |
'display' => array( | |
'example_node_list' => array( | |
'label' => $t('Custom text field 1'), | |
'type' => 'text', | |
), | |
), | |
), | |
'customodule_textfield2' => array( | |
'field_name' => 'customodule_textfield2', | |
'type' => 'text', | |
'label' => $t('Custom text field 2'), | |
'widget' => array( | |
'type' => 'text', | |
), | |
'display' => array( | |
'example_node_list' => array( | |
'label' => $t('Custom text field 2'), | |
'type' => 'text', | |
), | |
), | |
), | |
); | |
} | |
/** | |
* Implements hook_uninstall(). | |
* | |
* If needed, remove all 'my_content_type' node created when | |
* this module is uninstalled. | |
*/ | |
function customodule_uninstall() { | |
$new_ct = 'my_content_type'; | |
$sql = 'SELECT nid FROM {node} n WHERE n.type = :type'; | |
$result = db_query($sql, array(':type' => $new_ct)); | |
$nodeids = array(); | |
foreach ($result as $row) { | |
$nodeids[] = $row->nid; | |
} | |
node_delete_multiple($nodeids); | |
delete_custom_fields(); | |
node_type_delete($new_ct); | |
field_purge_batch(500); | |
} | |
function delete_custom_fields() { | |
foreach (array_keys(_customodule_installed_fields()) as $field) { | |
field_delete_field($field); | |
} | |
$instances = field_info_instances('node', 'my_content_type'); | |
foreach ($instances as $instance_name => $fieldinstance) { | |
field_delete_instance($fieldinstance); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_node_info() | |
*/ | |
function customodule_node_info() { | |
return array( | |
'my_content_type' => array( | |
'name' => t('My content type'), | |
'base' => 'my_content_type', | |
'description' => t('A description for this content type'), | |
'has_title' => TRUE, | |
) | |
); | |
} | |
/** | |
* Implement hook_form() | |
*/ | |
function my_content_type_form($node, $form_state) { | |
return node_content_form($node, $form_state); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment