搜尋
首頁php教程PHP源码drupal7 节点操作module

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;
	}
}

                   

                   

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),