登录  /  注册
首页 > php教程 > PHP源码 > 正文

drupal7 节点操作module

PHP中文网
发布: 2016-05-25 17:11:29
原创
643人浏览过

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;
	}
}
登录后复制

                   

                   

来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学