drupal7 节点操作module
程序员文章站
2022-04-15 12:13:09
...
1. [PHP]代码
<?php function examplenode_menu(){ $items['example/node'] = array( 'title' => 'Example node', 'description' => 'Example of Drupal node', 'page callback' => 'examplenode_overview', 'access arguments' => array('access content'), ); return $items; } function examplenode_overview() { // global $user; // $newNode = new stdclass(); // $newNode->type = 'page'; // $newNode->uid = $user->uid; // $newNode->title = 'New Node'; // node_save($newNode); return 'Example node '; } /** * Implements hook_node_info() to provide our job_post type. */ function examplenode_node_info() { return array( 'job_post' => array( 'name' => t('Job Post'), 'base' => 'examplenode', 'description' => t('Use this content type to post a job.'), 'has_title' => TRUE, 'title_label' => t('Job Title'), 'help' => t('Enter the job title, job description, and the name of the company that posted the job'), ), ); } /** * Implements hook_menu_alter(). */ function examplenode_menu_alter(&$callbacks) { // if (!user_access('administer nodes')) { // $callbacks['node/add/job_post']['access callback'] = FALSE; // // Must unset access arguments or Drupal will use user_access() // // as a default access callback. // unset($callbacks['node/add/job_post']['access arguments']); // } } /** * Implements hook_permission(). */ function examplenode_permission() { return array( 'create job post' => array( 'title' => t('Create a job post'), 'description' => t('Create a job post'), ), 'edit own job post' => array( 'title' => t('Edit own job post'), 'description' => t('Edit your own job posting'), ), 'edit any job post' => array( 'title' => t('Edit any job post'), 'description' => t('Edit any job posting'), ), 'delete own job post' => array( 'title' => t('Delete own job post'), 'description' => t('Delete own job posting'), ), 'delete any job post' => array( 'title' => t('Delete any job post'), 'description' => t('Delete any job posting'), ), ); } /** * Implements hook_node_access(). */ function examplenode_access($op, $node, $account) { $is_author = $account->uid == $node->uid; switch ($op) { case 'create': // Allow if user's role has 'create joke' permission. if (user_access('create job post', $account)) { return NODE_ACCESS_ALLOW; } case 'update': // Allow if user's role has 'edit own joke' permission and user is // the author; or if the user's role has 'edit any joke' permission. if (user_access('edit own job post', $account) && $is_author || user_access('edit any job post', $account)) { return NODE_ACCESS_ALLOW; } case 'delete': // Allow if user's role has 'delete own joke' permission and user is // the author; or if the user's role has 'delete any joke' permission. if (user_access('delete own job post', $account) && $is_author || user_access('delete any job post', $account)) { return NODE_ACCESS_ALLOW; } } } /** * Implement hook_form() with the standard default form. */ function examplenode_form($node, $form_state) { return node_content_form($node, $form_state); } /** * Implements hook_validate(). */ function examplenode_validate($node) { // Enforce a minimum character count of 2 on company names. if (isset($node->job_post_company) && strlen($node->job_post_company['und'][0]['value']) < 2) { form_set_error('job_post_company', t('The company name is too short. It must be atleast 2characters.'),$limit_validation_errors = NULL); } } /** * Implements hook_insert(). */ function examplenode_insert($node) { // log details of the job posting to watchdog watchdog('job post', 'A new job post titled: '.$node->title.' for company: '.$node->job_post_company['und'][0]['value'].' was added by UID: '.$node->uid, $variables = array(),WATCHDOG_NOTICE, $link = 'node/'.$node->nid); } /** * Implements hook_update(). */ function examplenode_update($node) { // log details of the job posting to watchdog watchdog('job post', 'A job post titled: '.$node->title.' for company: '.$node->job_post_company['und'][0]['value'].' was updated by UID: '.$node->uid, $variables = array(),WATCHDOG_NOTICE, $link = 'node/'.$node->nid); } /** * Implements hook_delete(). */ function examplenode_delete($node) { // log details of the job posting to watchdog watchdog('job post', 'A job post titled: '.$node->title.' for company: '.$node->job_post_company['und'][0]['value'].' was deleted by UID: '.$node->uid, $variables = array(),WATCHDOG_NOTICE, $link = 'node/'.$node->nid); } /** * Implements hook_load(). */ function examplenode_load($nodes) { // Add a new element to the node at load time for storing the // job posting sponsor information foreach ($nodes as $node) { $node->sponsor = "ACME Career Services, Your Source for Drupal Jobs"; } return $node; } /** * Implement hook_view(). */ function examplenode_view($node, $view_mode) { // Add and theme the sponsor so it appears when the job post is displayed if ($view_mode == 'full') { $node->content['sponsor'] = array( '#markup' => theme('sponsor', array('sponsor' => $node->sponsor, 'sponsor_id' => $node->nid)), '#weight' => 100, ); } return $node; } /** * Implements hook_theme(). */ function examplenode_theme() { // define the variables and template associated with the sponsor field // The sponsor will contain the name of the sponsor and the sponsor_id // will be used to create a unique CSS ID return array( 'sponsor' => array( 'variables' => array('sponsor' => NULL, 'sponsor_id' => NULL), 'template' => 'sponsor', ), ); } function examplenode_node_access_records($node) { // role id = 5, allow access job post // must include strict limit if ($node->type == 'job_post') { $grants = array(); $grants[] = array( 'realm' => 'example', 'gid' => 5, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0, 'priority' => 0, ); return $grants; } } function examplenode_node_grants($account, $op) { if($op == 'view') { $roles = $account->roles; foreach($roles AS $key => $value ){ $rid[] = $key; } $grants['example'] = $rid; return $grants; } }