From 8e78de6c726b1399544f139781c76ef6c6a5243a Mon Sep 17 00:00:00 2001
From: chipcleary <chipcleary@hotmail.com>
Date: Sat, 28 Jan 2012 15:20:34 -0600
Subject: [PATCH] Issue #1371588 by hefox, chipcleary: Remove navigation to answer node (and also provide admin option to use theming provided by the answers module)

---
 answers.install              |    9 ++++++++
 answers.module               |   44 ++++++++++++++++++++++++++++++++++++-----
 includes/answers.display.inc |   40 ++++++++++++++++++++++++++++++++++++++
 node--answer.tpl.php         |   41 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 128 insertions(+), 6 deletions(-)
 create mode 100644 includes/answers.display.inc
 create mode 100644 node--answer.tpl.php

diff --git a/answers.install b/answers.install
index 30432fa..edb7e73 100644
--- a/answers.install
+++ b/answers.install
@@ -73,3 +73,12 @@ function _answers_set_default_question_field_values($question) {
     node_save($question);
   }
 }
+
+/**
+ * Reset the theme cache so that answers theming is enabled
+ */
+function answers_update_7300() {
+  watchdog('answers', 'Enable answers theming');
+  drupal_theme_rebuild();
+  return array();
+}
diff --git a/answers.module b/answers.module
index e1e6c8a..88b59b9 100644
--- a/answers.module
+++ b/answers.module
@@ -4,7 +4,8 @@
  * Code for the Answers feature.
  */
 
-include_once('answers.features.inc');
+include_once('answers.features.inc');;
+module_load_include('inc', 'answers', 'includes/answers.display');
 module_load_include('inc', 'answers', 'includes/answers.field_utils');
 module_load_include('inc', 'answers', 'includes/answers.notify');
 module_load_include('inc', 'answers', 'includes/answers.search');
@@ -43,7 +44,7 @@ function answers_menu() {
 
   $items['admin/config/content/answers'] = array(
     'title'              => 'Answers',
-  'description'        => 'Configure how the question/answer service operates',
+    'description'        => 'Configure how the question/answer service operates',
     'page callback'      => 'drupal_get_form',
     'page arguments'     => array('answers_settings'),
     'access arguments'   => array('administer content types'),
@@ -78,7 +79,8 @@ function answers_menu_alter(&$items) {
 function answers_settings() {
   $form = array();
 
-  $form['notify'] = _answers_notify_settings();
+  $form += _answers_notify_settings();
+  $form += _answers_display_settings();
 
   return system_settings_form($form);
 }
@@ -90,9 +92,9 @@ function answers_node_view($node, $view_mode, $langcode) {
 
   if ($node->type == 'question') {
 
-    // Ensure that the 'Post an Answer' link only shows if the question is not locked
-    // The logic is a little complicated below to avoid updating the field when not necessary
-    // The field should have the *opposite* value of the node->locked field
+    // Ensure that the 'Post an Answer' link only shows if the question is not locked.
+    // The logic is a little complicated below to avoid updating the field when not necessary.
+    // The field should have the *opposite* value of the node->locked field.
     $field_instance = field_info_instance('node', 'field_answer_question', 'answer');
     $locked_p = answers_field_get_value($node, 'field_question_locked_p');
     if ($locked_p == $field_instance['widget']['settings']['node_link']['full']) {
@@ -100,6 +102,36 @@ function answers_node_view($node, $view_mode, $langcode) {
       field_update_instance($field_instance);
     }
   }
+  elseif (variable_get('answers_use_answers_theme_templates_p', TRUE) 
+          && $node->type == 'answer' 
+          && $view_mode == 'full' 
+          && node_is_page($node)
+          && $node->status) {
+    // If viewing the node page for a published answer and if configured to use theme templates,
+	// then redirect to question, with answer node id in fragment.
+    $items = field_get_items('node', $node, 'field_answer_question', $node->language);
+    if (!empty($items[0]['nid'])) {
+      drupal_goto('node/' . $items[0]['nid'], array('fragment' => 'node-' . $node->nid));
+    }
+  }
+}
+
+/**
+ * Implements hook_theme().
+ **/
+function answers_theme() {
+  $theme = array();
+
+  // This makes answers default to using the node templates provided by the answers module 
+  if (variable_get('answers_use_answers_theme_templates_p', TRUE)) {
+    $path = drupal_get_path('module', 'answers');
+    $theme['node__answer'] = array(
+      'template' => 'node--answer',
+      'path' => $path,
+      'original hook' => 'node',
+    );
+  }
+  return $theme;
 }
 
 /*
diff --git a/includes/answers.display.inc b/includes/answers.display.inc
new file mode 100644
index 0000000..649ab37
--- /dev/null
+++ b/includes/answers.display.inc
@@ -0,0 +1,40 @@
+<?php
+
+/**
+ * @file
+ * Display utility functions for the 'Answers' module
+ */
+
+/**
+ * Add settings relating to display to the admin form
+ */
+function _answers_display_settings() {
+  $form = array();
+
+  $form['answers_display'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Display Settings'),
+  );
+
+  // This setting tells answers to use the theme templates which answers provides.
+  // If the user employs a special theme or has overwritten node.tpl.php, this may make answers look different
+  $form['answers_display']['answers_use_answers_theme_templates_p'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Use theming provided by the answers module instead of default theme templates'),
+    '#default_value' => variable_get('answers_use_answers_theme_templates_p', TRUE),
+    '#description' => t('This will ignore the default theme template for question and answer nodes and instead use theming provided by the Answers module (Recommended).'),
+  );
+  $form['#submit'][] = '_answers_display_settings_submit';
+
+  return $form;
+}
+
+/**
+ * Form submission that clears the theme registery if navigation setting changed.
+ */
+function _answers_display_settings_submit($form, $form_state) {
+  // Clear the theme registery when the 'answers_use_answers_theme_templates_p' setting is changed.
+  if ($form['answers_display']['answers_use_answers_theme_templates_p']['#default_value'] != $form_state['values']['answers_use_answers_theme_templates_p']) {
+    drupal_theme_rebuild();
+  }
+}
diff --git a/node--answer.tpl.php b/node--answer.tpl.php
new file mode 100644
index 0000000..fb7fbf2
--- /dev/null
+++ b/node--answer.tpl.php
@@ -0,0 +1,41 @@
+<?php
+
+/**
+ * @file
+ * The Answers Module overrides the default node theme implementation.
+ *
+ * Overrides implemented:
+ *   - The title of an answers node is no longer linked to the node page (generally not relevant for answers nodes)
+ */
+?>
+<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
+
+  <?php print $user_picture; ?>
+
+  <?php print render($title_prefix); ?>
+  <?php if (!$page): ?>
+    <h2<?php print $title_attributes; ?>><?php print $title; ?></h2>
+  <?php endif; ?>
+  <?php print render($title_suffix); ?>
+
+  <?php if ($display_submitted): ?>
+    <div class="submitted">
+      <?php print $submitted; ?>
+    </div>
+  <?php endif; ?>
+
+  <div class="content"<?php print $content_attributes; ?>>
+    <?php
+      // We hide the comments and links now so that we can render them later.
+      hide($content['comments']);
+      hide($content['links']);
+      print render($content);
+    ?>
+  </div>
+
+  <?php print render($content['links']); ?>
+
+  <?php print render($content['comments']); ?>
+
+
+</div>
-- 
1.7.4.msysgit.0

