drupal 站点开发
function examplenode_install() { //updates the database cache of node types node_types_rebuild(); $types = node_type_get_types(); // add the body field to the node type node_add_body_field($types['job_post']); // load the instance definition for our content type's body $body_instance = field_info_instance('node', 'body', 'job_post'); // configure the body field $body_instance['type'] = 'text_summary_or_trimmed'; // save our changes to the body field instance. field_update_instance($body_instance); // create all the fields we are adding to our content type. foreach (_job_post_installed_fields() as $field) { field_create_field($field); } // create all the instances for our fields. foreach (_job_post_installed_instances() as $instance) { $instance['entity_type'] = 'node'; $instance['bundle'] = 'job_post'; field_create_instance($instance); }}/*** return a structured array defining the fields created by this content type.* for the job post module there is only one additional field – the company name* other fields could be added by defining them in this function as additional elements* in the array below*/function _job_post_installed_fields() { $t = get_t(); return array( 'job_post_company' => array( 'field_name' => 'job_post_company', 'label' => $t('company posting the job listing'), 'type' => 'text', ), );}/*** return a structured array defining the field instances associated with this content type.*/function _job_post_installed_instances() { $t = get_t(); return array( 'job_post_company' => array( 'field_name' => 'job_post_company', 'type' => 'text', 'label' => $t('company posting the job listing'), 'widget' => array( 'type' => 'text_textfield', ), 'display' => array( 'example_node_list' => array( 'label' => $t('company posting the job listing'), 'type' => 'text', ), ), ), );}/*** implements hook_uninstall().*/function examplenode_uninstall() { // gather all the example content that might have been created while this // module was enabled. $sql = 'select nid from {node} n where n.type = :type'; $result = db_query($sql, array(':type' => 'job_post')); $nids = array(); foreach ($result as $row) { $nids[] = $row->nid; } // delete all the nodes at once node_delete_multiple($nids); // loop over each of the fields defined by this module and delete // all instances of the field, their data, and the field itself. foreach (array_keys(_job_post_installed_fields()) as $field) { field_delete_field($field); } // loop over any remaining field instances attached to the job_post // content type (such as the body field) and delete them individually. $instances = field_info_instances('node', 'job_post'); foreach ($instances as $instance_name => $instance) { field_delete_instance($instance); } // delete our content type node_type_delete('job_post'); // purge all field infromation field_purge_batch(1000);}
复制代码