模块文件结构:
- job_post.info
- job_post.install
- job_post.module
- sponsor.tpl.php
1.job_post.info:
1 name = Job Post 2 description = A job posting content type 3 package = Pro Drupal Development 4 core = 7.x 5 files[] = job_post.install 6 files[] = job_post.module
2.job_post.install:
1 <?php 2 /** 3 * @file 4 * Install file for Job Post module. 5 */ 6 /** 7 * Implements hook_install(). 8 * - Add the body field. 9 * - Configure the body field. 10 * - Create the company name field. 11 */ 12 function job_post_install() { 13 node_types_rebuild(); 14 $types = node_type_get_types(); 15 // add the body field to the node type 16 node_add_body_field($types['job_post']); 17 // Load the instance definition for our content type's body 18 $body_instance = field_info_instance('node', 'body', 'job_post'); 19 // Configure the body field 20 $body_instance['type'] = 'text_summary_or_trimmed'; 21 // Save our changes to the body field instance. 22 field_update_instance($body_instance); 23 // Create all the fields we are adding to our content type. 24 foreach (_job_post_installed_fields() as $field) { 25 field_create_field($field); 26 } 27 // Create all the instances for our fields. 28 foreach (_job_post_installed_instances() as $instance) { 29 $instance['entity_type'] = 'node'; 30 $instance['bundle'] = 'job_post'; 31 field_create_instance($instance); 32 } 33 } 34 /** 35 * Return a structured array defining the fields created by this content type. 36 * For the job post module there is only one additional field – the company name 37 * Other fields could be added by defining them in this function as additional elements 38 * in the array below 39 */ 40 function _job_post_installed_fields() { 41 $t = get_t(); 42 return array( 43 'job_post_company' => array( 44 'field_name' => 'job_post_company', 45 'label' => $t('Company posting the job listing'), 46 'type' => 'text', 47 ), 48 ); 49 } 50 /** 51 * Return a structured array defining the field instances associated with this content type. 52 */ 53 function _job_post_installed_instances() { 54 $t = get_t(); 55 return array( 56 'job_post_company' => array( 57 'field_name' => 'job_post_company', 58 'type' => 'text', 59 'label' => $t('Company posting the job listing'), 60 'widget' => array( 61 'type' => 'text_textfield', 62 ), 63 'display' => array( 64 'example_node_list' => array( 65 'label' => $t('Company posting the job listing'), 66 'type' => 'text', 67 ), 68 ), 69 ), 70 ); 71 } 72 73 /** 74 * Implements hook_uninstall(). 75 */ 76 function job_post_uninstall() { 77 // Gather all the example content that might have been created while this 78 // module was enabled. 79 $sql = 'SELECT nid FROM {node} n WHERE n.type = :type'; 80 $result = db_query($sql, array(':type' => 'job_post')); 81 $nids = array(); 82 foreach ($result as $row) { 83 $nids[] = $row->nid; 84 } 85 // Delete all the nodes at once 86 node_delete_multiple($nids); 87 // Loop over each of the fields defined by this module and delete 88 // all instances of the field, their data, and the field itself. 89 foreach (array_keys(_job_post_installed_fields()) as $field) { 90 field_delete_field($field); 91 } 92 // Loop over any remaining field instances attached to the job_post 93 // content type (such as the body field) and delete them individually. 94 $instances = field_info_instances('node', 'job_post'); 95 foreach ($instances as $instance_name => $instance) { 96 field_delete_instance($instance); 97 } 98 // Delete our content type 99 node_type_delete('job_post'); 100 // Purge all field infromation 101 field_purge_batch(1000); 102 }
3.job_post.module:
1 <?php 2 /** 3 * @file 4 * This module provides a node type called job post 5 */ 6 7 /** 8 * Implements hook_node_info() to provide our job_post type. 9 */ 10 function job_post_node_info() { 11 return array( 12 'job_post' => array( 13 'name' => t('Job Post'), 14 'base' => 'job_post', 15 'description' => t('Use this content type to post a job.'), 16 'has_title' => TRUE, 17 'title_label' => t('Job Title'), 18 'help' => t('Enter the job title, 19 job description, and the name of the company that posted the job'), 20 ), 21 ); 22 } 23 24 /** 25 * Implements hook_menu_alter(). 26 */ 27 function job_post_menu_alter(&$callbacks) { 28 // If the user does not have 'administer nodes' permission, 29 // disable the job_post menu item by setting its access callback to FALSE. 30 if (!user_access('administer nodes')) { 31 $callbacks['node/add/job_post']['access callback'] = FALSE; 32 // Must unset access arguments or Drupal will use user_access() 33 // as a default access callback. 34 unset($callbacks['node/add/job_post']['access arguments']); 35 } 36 } 37 38 /** 39 * Implements hook_permission(). 40 */ 41 function job_post_permission() { 42 return array( 43 'create job post' => array( 44 'title' => t('Create a job post'), 45 'description' => t('Create a job post'), 46 ), 47 'edit own job post' => array( 48 'title' => t('Edit own job post'), 49 'description' => t('Edit your own job posting'), 50 ), 51 'edit any job post' => array( 52 'title' => t('Edit any job post'), 53 'description' => t('Edit any job posting'), 54 ), 55 'delete own job post' => array( 56 'title' => t('Delete own job post'), 57 'description' => t('Delete own job posting'), 58 ), 59 'delete any job post' => array( 60 'title' => t('Delete any job post'), 61 'description' => t('Delete any job posting'), 62 ), 63 ); 64 } 65 66 /** 67 * Implements hook_node_access(). 68 */ 69 function job_node_access($op, $node, $account) { 70 $is_author = $account->uid == $node->uid; 71 switch ($op) { 72 case 'create': 73 // Allow if user's role has 'create joke' permission. 74 if (user_access('create job', $account)) { 75 return NODE_ACCESS_ALLOW; 76 } 77 case 'update': 78 // Allow if user's role has 'edit own joke' permission and user is 79 // the author; or if the user's role has 'edit any joke' permission. 80 if (user_access('edit own job', $account) && $is_author || 81 user_access('edit any job', $account)) { 82 return NODE_ACCESS_ALLOW; 83 } 84 case 'delete': 85 // Allow if user's role has 'delete own joke' permission and user is 86 // the author; or if the user's role has 'delete any joke' permission. 87 if (user_access('delete own job', $account) && $is_author || 88 user_access('delete any job', $account)) { 89 return NODE_ACCESS_ALLOW; 90 } 91 } 92 } 93 94 /** 95 * Implement hook_form() with the standard default form. 96 */ 97 function job_post_form($node, $form_state) { 98 return node_content_form($node, $form_state); 99 } 100 101 /** 102 * Implements hook_validate(). 103 */ 104 105 /* 106 function job_post_validate($node) { 107 // Enforce a minimum character count of 2 on company names. 108 if (isset($node->job_post_company) && 109 strlen($node->job_post_company['und'][0]['value']) < 2) { 110 form_set_error('job_post_company', 111 t('The company name is too short. It must be atleast 2 112 characters.'), 113 $limit_validation_errors = NULL); 114 } 115 } 116 */ 117 118 /** 119 * Implements hook_insert(). 120 */ 121 function job_post_insert($node) { 122 // log details of the job posting to watchdog 123 watchdog('job post', 'A new job post titled: '.$node->title.' for company: '. 124 $node->job_post_company['und'][0]['value']. 125 ' was added by UID: '.$node->uid, $variables = array(), 126 WATCHDOG_NOTICE, $link = 'node/'.$node->nid); 127 } 128 129 /** 130 * Implements hook_update(). 131 */ 132 function job_post_update($node) { 133 // log details of the job posting to watchdog 134 watchdog('job post', 'A job post titled: '.$node->title.' for company: '. 135 $node->job_post_company['und'][0]['value']. 136 ' was updated by UID: '.$node->uid, $variables = array(), 137 WATCHDOG_NOTICE, $link = 'node/'.$node->nid); 138 } 139 140 /** 141 * Implements hook_delete(). 142 */ 143 function job_post_delete($node) { 144 // log details of the job posting to watchdog 145 watchdog('job post', 'A job post titled: '.$node->title.' for company: '. 146 $node->job_post_company['und'][0]['value']. 147 ' was deleted by UID: '.$node->uid, $variables = array(), 148 WATCHDOG_NOTICE, $link = 'node/'.$node->nid); 149 } 150 151 /** 152 * Implements hook_load(). 153 */ 154 function job_post_load($nodes) { 155 // Add a new element to the node at load time for storing the 156 // job posting sponsor information 157 foreach ($nodes as $node) { 158 $node->sponsor = "ACME Career Services, Your Source for Drupal Jobs"; 159 } 160 return $node; 161 } 162 163 /** 164 * Implement hook_view(). 165 */ 166 function job_post_view($node, $view_mode) { 167 // Add and theme the sponsor so it appears when the job post is displayed 168 if ($view_mode == 'full') { 169 $node->content['sponsor'] = array( 170 '#markup' => theme('sponsor', array('sponsor' => $node->sponsor, 171 ‘sponsor_id’ => $node_nid)), 172 '#weight' => 100, 173 ); 174 } 175 return $node; 176 } 177 178 /** 179 * Implements hook_theme(). 180 */ 181 function job_post_theme() { 182 // define the variables and template associated with the sponsor field 183 // The sponsor will contain the name of the sponsor and the sponsor_id 184 // will be used to create a unique CSS ID 185 return array( 186 'sponsor' => array( 187 'variables' => array('sponsor' => NULL, 'sponsor_id' => NULL), 188 'template' => 'sponsor', 189 ), 190 ); 191 } 192 193 /** 194 * Implements hook_validate(). 195 */ 196 function job_post_validate($node) { 197 // Enforce a minimum character count of 2 on company names. 198 if (isset($node->job_post_company) && 199 strlen($node->job_post_company['und'][0]['value']) < 2) { 200 form_set_error('job_post_company', 201 t('The company name is too short. It must be atleast 2 202 characters.'), 203 $limit_validation_errors = NULL); 204 } 205 }
4.sponsor.tpl.php:
1 <?php 2 /** 3 * @file 4 * Default theme implementation for rendering job post sponsor information 5 * 6 * Available variables: 7 * - $sponsor_id: the node ID asociated with the job posting 8 * - $sponsor: the name of the job post sponsor 9 */ 10 ?> 11 <div id="sponsor-<?php print $sponsor_id ?>" class="sponsor"> 12 <div class="sponsor-title"> 13 <h2>Sponsored by</h2> 14 </div> 15 <div class="sponsored-by-message"> 16 This job posting was sponsored by: <?php print $sponsor; ?> 17 </div> 18 </div>
完