Adding Contenttype via hook_install

Public

Welcome everybody and thank you for viewing my snipp.

Below you will find a example how to add a contenttype via code.

</> CopyGet raw version
php
  1. function hook_install() {
  2. /**
  3.   * Adding Contenttype
  4.   */
  5. $name = 'lessons';
  6.  
  7. $lessons = array(
  8. 'type' => $name,
  9. 'name' => st('Lessons'),
  10. 'base' => 'node_content',
  11. 'description' => st('Create a new lesson for a learn session.'),
  12. 'custom' => TRUE,
  13. 'modified' => TRUE,
  14. 'locked' => TRUE,
  15. );
  16.  
  17. $contenttype = node_type_set_defaults($lessons);
  18.  
  19. node_add_body_field($contenttype, st('Description'));
  20.  
  21. node_type_save($contenttype);
  22.  
  23.  
  24. /**
  25.   * Add peristant variables that control settings
  26.   */
  27. variable_set('additional_settings__active_tab_' . $name, 'edit-menu');
  28. variable_set('node_preview_' . $name, 1);
  29. variable_set('node_options_' . $name, array(0 => 'status', 1 => 'promote'));
  30. variable_set('node_submitted_' . $name, 0);
  31. variable_set('menu_options_' . $name, array());
  32. variable_set('menu_parent_' . $name, 'main-menu:0');
  33.  
  34.  
  35. /**
  36.   * Adding fields to the Contenttype
  37.   */
  38. foreach (_learn_installed_fields() as $field) {
  39. if (!field_info_field($field['field_name'])) {
  40. field_create_field($field);
  41. }
  42. }
  43.  
  44. /**
  45.   * Create all the instances for our fields
  46.   */
  47. foreach (_learn_installed_fields() as $instance) {
  48. $instance['entity_type'] = 'node';
  49. $instance['bundle'] = $lessons['type'];
  50. field_create_instance($instance);
  51. }
  52.  
  53. drupal_set_message('The Contenttype "Lessons" has been added to your system.');
  54. }

This is a example form one of my modules were I used this so please no 'bla bla why do you use this field there' thx :D

</> CopyGet raw version
php
  1. /**
  2.  * This is the setup of fields for a Taxonomy vocabulary
  3.  *
  4.  * # learn_video (HTML 5 Video upload)
  5.  * This field allows the user to upload a video. Amazone S3 link is possible
  6.  *
  7.  * # learn_class (Taxonomy)
  8.  * This Field will use the Vocabulary that was defined before in the install.
  9.  *
  10.  * @return array This array contains informations about fields in a Taxonomy vocabulary
  11.  */
  12. function _learn_installed_fields() {
  13.  
  14. $vocab = taxonomy_vocabulary_machine_name_load('tutorial_class');
  15.  
  16. return array(
  17. 'learn_video' => array(
  18. 'field_name' => 'learn_video',
  19. 'label' => 'Tutorial Video',
  20. 'type' => 'video',
  21. 'module' => 'video',
  22. 'active' => '1',
  23. 'locked' => '0',
  24. 'cardinality' => '1',
  25. 'deleted' => '0',
  26. 'columns' => array(
  27. 'value' => array(
  28. 'type' => 'int',
  29. 'not null' => FALSE,
  30. ),
  31. ),
  32. 'bundles' => array(
  33. 'node' => array(
  34. 'lessons',
  35. ),
  36. ),
  37. ),
  38. 'learn_class' => array(
  39. 'field_name' => 'learn_class',
  40. 'type' => 'taxonomy_term_reference',
  41. 'label' => 'Learn Class',
  42. 'required' => true,
  43. 'widget' => array(
  44. 'type' => 'options_select'
  45. ),
  46. 'display' => array(),
  47. 'settings' => array(
  48. 'allowed_values' => array(
  49. array(
  50. 'vocabulary' => !empty($vocab->machine_name) ? $vocab->machine_name : 0,
  51. 'parent' => 0
  52. ),
  53. ),
  54. ),
  55. 'bundles' => array(
  56. 'node' => array(
  57. 'lessons',
  58. ),
  59. ),
  60. ),
  61. );
  62. }