You are here

function answers_node_view in Answers 7.4

Same name and namespace in other branches
  1. 7 answers.module \answers_node_view()
  2. 7.2 answers.module \answers_node_view()
  3. 7.3 answers.module \answers_node_view()

Implements hook_node_view().

Add the view for answers to a question. Add the new answer form. Add the operations links to an answer node.

File

./answers.module, line 325
The Answers module.

Code

function answers_node_view($node, $view_mode = 'full') {
  module_load_include('inc', 'answers', 'includes/answers.lock');
  if ($node->type == 'answers_question') {
    if ($view_mode == 'full') {
      if (answers_question_locked_p($node)) {
        $node->content['lock_message'] = array(
          '#markup' => variable_get('answers_question_lock_message', ''),
          '#prefix' => '<div class="answers-question-locked-message">',
          '#suffix' => '</div>',
          '#weight' => -100,
        );
      }
      if (answers_create_answer_permission_p($node)) {
        global $user;

        // Create an empty placeholder for an answer node.
        $node_answer = array(
          'uid' => $user->uid,
          'name' => isset($user->name) ? $user->name : '',
          'type' => 'answers_answer',
          'language' => LANGUAGE_NONE,
        );

        // Set the node reference field of the answer to point to its question.
        $node_answer['answers_related_question'][LANGUAGE_NONE][0]['target_id'] = $node->nid;
        $form_state['build_info']['args'][] = (object) $node_answer;
        form_load_include($form_state, 'inc', 'node', 'node.pages');

        // Create a form to edit/save the placeholder answer.
        $answer_form = drupal_build_form('answers_answer_node_form', $form_state);

        // Add the form to the question page.
        $node->content['new_answer_form'] = $answer_form;
        $node->content['new_answer_form']['#weight'] = 150;
        $node->content['new_answer_form']['new_answer_form_title'] = array(
          '#theme' => 'html_tag',
          '#tag' => 'h2',
          '#attributes' => array(
            'class' => 'new-answer-form-title',
          ),
          '#value' => t('Your !Answer', answers_translation()),
          '#weight' => -100,
        );
      }
    }

    // Include the answers in the content when view_mode is
    // either 'search_index' or 'full'.
    if ($view_mode == 'search_index' || $view_mode == 'full') {
      $node->content['answers_list'] = array(
        '#type' => 'markup',
        '#markup' => views_embed_view('question_answers', 'default', $node->nid),
        '#weight' => 49,
      );
    }
  }
  if ($view_mode == 'full' && $node->type == 'answers_answer') {
    if (node_access('update', $node)) {
      $node->content['links']['#links']['answers-answer-edit'] = array(
        'title' => t('Edit'),
        'href' => 'node/' . $node->nid . '/edit',
      );
    }
    if (node_access('delete', $node)) {
      $node->content['links']['#links']['answers-answer-delete'] = array(
        'title' => t('Delete'),
        'href' => 'node/' . $node->nid . '/delete',
      );
    }
  }
}