Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @file
- * Shows you the option to publish a node as anonymous depending on permissions.
- */
- /**
- * Implements hook_form_BASE_FORM_ID_alter().
- */
- function hidden_author_form_node_form_alter(&$form, $form_state) {
- // Get weight.
- $weight = variable_get('hidden_author_node_weight', -4);
- // Get content types enabled that were saved in hidden_author settings.
- $content_types = variable_get('hidden_author_content_types', array('article' => 'article'));
- if (isset($content_types[$form['#node']->type])) {
- if (user_access('post anonymous nodes')) {
- $form['anonymous'] = array(
- '#type' => 'checkbox',
- '#title' => t('Post anonymously'),
- '#weight' => $weight,
- '#default_value' => hidden_author_get_author_id('node', $form['#node']) ? TRUE : FALSE,
- );
- }
- }
- }
- /**
- * Implements hook_node_insert().
- */
- function hidden_author_node_insert($node) {
- // Checks if anonymous is enabled.
- if ($node->anonymous) {
- db_insert('hidden_author')
- ->fields(array(
- 'entity_type' => 'node',
- 'bundle' => $node->type,
- 'entity_id' => $node->nid,
- 'revision_id' => $node->vid,
- 'uid' => $node->hidden_author,
- ))
- ->execute();
- }
- }
- /**
- * Implements hook_node_update().
- */
- function hidden_author_node_update($node) {
- // Checks if anonymous is enabled.
- if (!$node->anonymous) {
- hidden_author_node_delete($node);
- }
- elseif (!hidden_author_get_author_id('node', $node)) {
- hidden_author_node_insert($node);
- }
- }
- /**
- * Implements hook_node_presave().
- */
- function hidden_author_node_presave($node) {
- // Checks if anonymous is enabled.
- $is_hidden = hidden_author_get_author_id('node', $node);
- if (!$is_hidden && $node->anonymous) {
- $node->hidden_author = $node->uid;
- $node->uid = 0;
- $node->name = t('Anonymous');
- }
- elseif ($is_hidden) {
- if ($node->anonymous) {
- $node->hidden_author = $is_hidden['uid'];
- }
- else {
- $author = user_load($is_hidden['uid']);
- $node->uid = $author->uid;
- $node->name = $author->name;
- }
- }
- }
- /**
- * Implements hook_node_delete().
- */
- function hidden_author_node_delete($node) {
- // Deletes a single row in hidden_author table.
- db_delete('hidden_author')
- ->condition('entity_type', 'node', '=')
- ->condition('bundle', $node->type, '=')
- ->condition('entity_id', $node->nid, '=')
- ->condition('revision_id', $node->vid, '=')
- ->execute();
- }
- /**
- * Get the author uid and entity id for given entity.
- */
- function hidden_author_get_author_id($entity_type, $object) {
- if ($entity_type == 'node' && empty($object->nid)) {
- return;
- }
- // Build query.
- $res = db_select('hidden_author', 'e')
- ->fields('e', array('entity_id', 'uid'))
- ->condition('entity_type', $entity_type, '=')
- ;
- if ($entity_type == 'node') {
- $res
- ->condition('bundle', $object->type, '=')
- ->condition('entity_id', $object->nid, '=')
- ->condition('revision_id', $object->vid, '=');
- }
- $db_result = $res
- ->execute()
- ->fetchAssoc();
- return $db_result;
- }
- /**
- * Implements hook_menu().
- */
- function hidden_author_menu() {
- $items['admin/config/content/hidden_author-rules'] = array(
- 'title' => 'hidden author settings',
- 'description' => 'This page provides hidden author settings.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('hidden_author_list_content_type'),
- 'access callback' => 'user_access',
- 'access arguments' => array('administer hidden_author'),
- 'file' => 'hidden_author.admin.inc',
- 'type' => MENU_NORMAL_ITEM,
- );
- return $items;
- }
- /**
- * Implements hook_help().
- */
- function hidden_author_help($path, $arg) {
- switch ($path) {
- case 'admin/config/content/hidden_author-rules':
- return t('this is some help text for Hidden Author module');
- case 'admin/help#hidden_author';
- return _hidden_author_help();
- }
- }
- /**
- * Callback function for hidden_author_help().
- */
- function _hidden_author_help() {
- $file = drupal_get_path('module', 'hidden_author') . '/README.txt';
- if (file_exists($file)) {
- return _filter_autop(check_plain(file_get_contents($file)));
- }
- }
- /**
- * Implements hook_permission().
- */
- function hidden_author_permission() {
- return array(
- 'administer hidden author' => array(
- 'title' => t('Administer Hidden Author'),
- 'description' => t('Allows you to configure Hidden Author settings.'),
- ),
- 'post anonymous nodes' => array(
- 'title' => t('Post anonymous nodes'),
- 'description' => t('Allows you to post anonymous nodes.'),
- ),
- 'view hidden author' => array(
- 'title' => t('View Hidden Author'),
- 'description' => t('Allows you to view Hidden Author.'),
- ),
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement