Home  >  Article  >  php教程  >  drupal7 节点操作module

drupal7 节点操作module

PHP中文网
PHP中文网Original
2016-05-25 17:11:29903browse

1. [PHP]代码    

<?php

function examplenode_menu(){
  $items[&#39;example/node&#39;] = array(
    &#39;title&#39; => &#39;Example node&#39;,
    &#39;description&#39; => &#39;Example of Drupal node&#39;,
    &#39;page callback&#39; => &#39;examplenode_overview&#39;,
    &#39;access arguments&#39; => array(&#39;access content&#39;),
  );


  return $items;
}

function examplenode_overview() {

//	global $user;
//	$newNode = new stdclass();
//	$newNode->type = &#39;page&#39;;
//	$newNode->uid = $user->uid;
//	$newNode->title = &#39;New Node&#39;;
//	node_save($newNode);

  return &#39;Example node &#39;;
}

/**
* Implements hook_node_info() to provide our job_post type.
*/
function examplenode_node_info() {
  return array(
    &#39;job_post&#39; => array(
      &#39;name&#39; => t(&#39;Job Post&#39;),
      &#39;base&#39; => &#39;examplenode&#39;,
      &#39;description&#39; => t(&#39;Use this content type to post a job.&#39;),
      &#39;has_title&#39; => TRUE,
      &#39;title_label&#39; => t(&#39;Job Title&#39;),
      &#39;help&#39; => t(&#39;Enter the job title, job description, and the name of the company that posted the job&#39;),
    ),
  );
}

/**
* Implements hook_menu_alter().
*/
function examplenode_menu_alter(&$callbacks) {
//  if (!user_access(&#39;administer nodes&#39;)) {
//    $callbacks[&#39;node/add/job_post&#39;][&#39;access callback&#39;] = FALSE;
//    // Must unset access arguments or Drupal will use user_access()
//    // as a default access callback.
//    unset($callbacks[&#39;node/add/job_post&#39;][&#39;access arguments&#39;]);
//  }
}

/**
* Implements hook_permission().
*/
function examplenode_permission() {
  return array(
    &#39;create job post&#39; => array(
      &#39;title&#39; => t(&#39;Create a job post&#39;),
      &#39;description&#39; => t(&#39;Create a job post&#39;),
    ),
    &#39;edit own job post&#39; => array(
      &#39;title&#39; => t(&#39;Edit own job post&#39;),
      &#39;description&#39; => t(&#39;Edit your own job posting&#39;),
    ),
    &#39;edit any job post&#39; => array(
      &#39;title&#39; => t(&#39;Edit any job post&#39;),
      &#39;description&#39; => t(&#39;Edit any job posting&#39;),
    ),
    &#39;delete own job post&#39; => array(
      &#39;title&#39; => t(&#39;Delete own job post&#39;),
      &#39;description&#39; => t(&#39;Delete own job posting&#39;),
    ),
    &#39;delete any job post&#39; => array(
      &#39;title&#39; => t(&#39;Delete any job post&#39;),
      &#39;description&#39; => t(&#39;Delete any job posting&#39;),
    ),
  );
}

/**
* Implements hook_node_access().
*/
function examplenode_access($op, $node, $account) {
  $is_author = $account->uid == $node->uid;
  switch ($op) {
    case &#39;create&#39;:
      // Allow if user&#39;s role has &#39;create joke&#39; permission.
      if (user_access(&#39;create job post&#39;, $account)) {
        return NODE_ACCESS_ALLOW;
      }
    case &#39;update&#39;:
      // Allow if user&#39;s role has &#39;edit own joke&#39; permission and user is
      // the author; or if the user&#39;s role has &#39;edit any joke&#39; permission.
      if (user_access(&#39;edit own job post&#39;, $account) && $is_author || user_access(&#39;edit any job post&#39;, $account)) {
        return NODE_ACCESS_ALLOW;
      }
    case &#39;delete&#39;:
      // Allow if user&#39;s role has &#39;delete own joke&#39; permission and user is
      // the author; or if the user&#39;s role has &#39;delete any joke&#39; permission.
      if (user_access(&#39;delete own job post&#39;, $account) && $is_author || user_access(&#39;delete any job post&#39;, $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[&#39;und&#39;][0][&#39;value&#39;]) < 2) {
    form_set_error(&#39;job_post_company&#39;, t(&#39;The company name is too short. It must be atleast 2characters.&#39;),$limit_validation_errors = NULL);
  }
}

/**
* Implements hook_insert().
*/
function examplenode_insert($node) {
  // log details of the job posting to watchdog
  watchdog(&#39;job post&#39;, &#39;A new job post titled: &#39;.$node->title.&#39; for company: &#39;.$node->job_post_company[&#39;und&#39;][0][&#39;value&#39;].&#39; was added by UID: &#39;.$node->uid, $variables = array(),WATCHDOG_NOTICE, $link = &#39;node/&#39;.$node->nid);
}

/**
* Implements hook_update().
*/
function examplenode_update($node) {
  // log details of the job posting to watchdog
  watchdog(&#39;job post&#39;, &#39;A job post titled: &#39;.$node->title.&#39; for company: &#39;.$node->job_post_company[&#39;und&#39;][0][&#39;value&#39;].&#39; was updated by UID: &#39;.$node->uid, $variables = array(),WATCHDOG_NOTICE, $link = &#39;node/&#39;.$node->nid);
}

/**
* Implements hook_delete().
*/
function examplenode_delete($node) {
  // log details of the job posting to watchdog
  watchdog(&#39;job post&#39;, &#39;A job post titled: &#39;.$node->title.&#39; for company: &#39;.$node->job_post_company[&#39;und&#39;][0][&#39;value&#39;].&#39; was deleted by UID: &#39;.$node->uid, $variables = array(),WATCHDOG_NOTICE, $link = &#39;node/&#39;.$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 == &#39;full&#39;) {
    $node->content[&#39;sponsor&#39;] = array(
      &#39;#markup&#39; => theme(&#39;sponsor&#39;, array(&#39;sponsor&#39; => $node->sponsor, &#39;sponsor_id&#39; => $node->nid)),
      &#39;#weight&#39; => 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(
    &#39;sponsor&#39; => array(
      &#39;variables&#39; => array(&#39;sponsor&#39; => NULL, &#39;sponsor_id&#39; => NULL),
      &#39;template&#39; => &#39;sponsor&#39;,
    ),
  );
}


function examplenode_node_access_records($node) {
	// role id = 5, allow access job post
	// must include strict limit
  if ($node->type == &#39;job_post&#39;) {
    $grants = array();
    $grants[] = array(
      &#39;realm&#39; => &#39;example&#39;,
      &#39;gid&#39; => 5,
      &#39;grant_view&#39; => 1,
      &#39;grant_update&#39; => 0,
      &#39;grant_delete&#39; => 0,
      &#39;priority&#39; => 0,
    );

    return $grants;
  }
}

function examplenode_node_grants($account, $op) {
	if($op == &#39;view&#39;) {
		$roles = $account->roles;
		foreach($roles AS $key => $value ){
			$rid[] = $key;
		}
	  $grants[&#39;example&#39;] = $rid;
	  return $grants;
	}
}

                   

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php采集一网站的精美图片Next article:php检查日期