목차
1. [PHP]代码    
php教程 PHP源码 drupal7 节点操作module

drupal7 节点操作module

May 25, 2016 pm 05:11 PM

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 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 채팅 명령 및 사용 방법
4 몇 주 전 By 尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)