模块文件结构:
- annotate.info
- annotate.module
- annotate.admin.inc
- annotate.install
1. 在drupal目录 sites/all/modules/custom 下新建文件夹annotate.(如果custom不存在,请自行建立)
2.新建文件annotate.info(模块描述文件):
name = Annotate description = "Allows users to annotate nodes." package = Pro Drupal Development core = 7.x files[] = annotate.module files[] = annotate.install files[] = annotate.admin.inc configure=admin/config/content/annotate/settings
3.新建文件annotate.module。(模块主文件):
1 <?php 2 /** 3 * @file 4 * Lets users add private annotations to nodes. 5 * 6 * Adds a text field when a node is displayed 7 * so that authenticated users may make notes. 8 */ 9 10 /** 11 * Implementation of hook_menu(). 12 */ 13 function annotate_menu() 14 { 15 $items[\'admin/config/annotate\'] = array( 16 \'title\' => \'Node annotation\', 17 \'description\' => \'Adjust node annotation options.\', 18 \'position\' => \'right\', 19 \'weight\' => -5, 20 \'page callback\' => \'system_admin_menu_block_page\', 21 \'access arguments\' => array( 22 \'administer site configuration\' 23 ), 24 \'file\' => \'system.admin.inc\', 25 \'file path\' => drupal_get_path(\'module\', \'system\') 26 ); 27 $items[\'admin/config/annotate/settings\'] = array( 28 \'title\' => \'Annotation settings\', 29 \'description\' => \'Change how annotations behave.\', 30 \'page callback\' => \'drupal_get_form\', 31 \'page arguments\' => array( 32 \'annotate_admin_settings\' 33 ), 34 \'access arguments\' => array( 35 \'administer site configuration\' 36 ), 37 \'type\' => MENU_NORMAL_ITEM, 38 \'file\' => \'annotate.admin.inc\' 39 ); 40 return $items; 41 } 42 43 /** 44 * Implements hook_node_load() 45 */ 46 function annotate_node_load($nodes, $types) 47 { 48 global $user; 49 // Check to see if the person viewing the node is the author. If not then 50 // hide the annotation. 51 foreach ($nodes as $node) { 52 if ($user->uid != $node->uid) { 53 unset($node->annotation); 54 } 55 } 56 }
4.新建文件annotate.admin.inc.(annotate.module中定义的file的文件):
1 <?php 2 /** 3 * @file 4 * Administration page callbacks for the annotate module. 5 */ 6 /** 7 * Form builder. Configure annotations. 8 * 9 * @ingroup forms 10 * @see system_settings_form(). 11 */ 12 function annotate_admin_settings() 13 { 14 // Get an array of node types with internal names as keys and 15 // "friendly names" as values. E.g., 16 // array(\'page\' => ’Basic Page, \'article\' => \'Articles\') 17 $types = node_type_get_types(); 18 foreach ($types as $node_type) { 19 $options[$node_type->type] = $node_type->name; 20 } 21 $form[\'annotate_node_types\'] = array( 22 \'#type\' => \'checkboxes\', 23 \'#title\' => t(\'Users may annotate these content types\'), 24 \'#options\' => $options, 25 \'#default_value\' => variable_get(\'annotate_node_types\', array( 26 \'page\' 27 )), 28 \'#description\' => t(\'A text field will be available on these content types to 29 make user-specific notes.\') 30 ); 31 $form[\'#submit\'][] = \'annotate_admin_settings_submit\'; 32 return system_settings_form($form); 33 } 34 35 /** 36 * Process annotation settings submission. 37 */ 38 function annotate_admin_settings_submit($form, $form_state) 39 { 40 dd("wangkangluo1"); 41 // Loop through each of the content type checkboxes shown on the form. 42 foreach ($form_state[\'values\'][\'annotate_node_types\'] as $key => $value) { 43 // If the check box for a content type is unchecked, look to see whether 44 // this content type has the annotation field attached to it using the 45 // field_info_instance function. If it does then we need to remove the 46 // annotation field as the administrator has unchecked the box. 47 if (!$value) { 48 $instance = field_info_instance(\'node\', \'annotation\', $key); 49 if (!empty($instance)) { 50 field_delete_instance($instance); 51 watchdog("Annotation", \'Deleted annotation field from content type: 52 %key\', array( 53 \'%key\' => $key 54 )); 55 } 56 } else { 57 // If the check box for a content type is checked, look to see whether 58 // the field is associated with that content type. If not then add the 59 // annotation field to the content type. 60 $instance = field_info_instance(\'node\', \'annotation\', $key); 61 if (empty($instance)) { 62 $instance = array( 63 \'field_name\' => \'annotation\', 64 \'entity_type\' => \'node\', 65 \'bundle\' => $key, 66 \'label\' => t(\'Annotation\'), 67 \'widget_type\' => \'text_textarea_with_summary\', 68 \'settings\' => array( 69 \'display_summary\' => TRUE 70 ), 71 \'display\' => array( 72 \'default\' => array( 73 \'type\' => \'text_default\' 74 ), 75 \'teaser\' => array( 76 \'type\' => \'text_summary_or_trimmed\' 77 ) 78 ) 79 ); 80 $instance = field_create_instance($instance); 81 watchdog(\'Annotation\', \'Added annotation field to content type: %key\', array( 82 \'%key\' => $key 83 )); 84 } 85 } 86 } // End foreach loop. 87 }
5.新建文件annotate.install(数据相关文件)
1 <?php 2 /** 3 * Implements hook_install() 4 */ 5 function annotate_install() 6 { 7 // Check to see if annotation field exists. 8 $field = field_info_field(\'annotation\'); 9 // if the annotation field does not exist then create it 10 if (empty($field)) { 11 $field = array( 12 \'field_name\' => \'annotation\', 13 \'type\' => \'text_with_summary\', 14 \'entity_types\' => array( 15 \'node\' 16 ), 17 \'translatable\' => TRUE 18 ); 19 $field = field_create_field($field); 20 } 21 } 22 23 /** 24 * Implements hook_uninstall() 25 */ 26 function annotate_uninstall() 27 { 28 watchdog("Annotate Module", "Uninstalling module and deleting fields"); 29 $types = node_type_get_types(); 30 foreach ($types as $type) { 31 annotate_delete_annotation($type); 32 } 33 $field = field_info_field(\'annotation\'); 34 if ($field) { 35 field_delete_field(\'annotation\'); 36 } 37 } 38 function annotate_delete_annotation($type) 39 { 40 $instance = field_info_instance(\'node\', \'annotation\', $type->type); 41 if ($instance) { 42 field_delete_instance($instance); 43 } 44 } 45
完