You are here

function answers_preprocess_node in Answers 8

Same name and namespace in other branches
  1. 7.4 answers.module \answers_preprocess_node()

Implements hook_preprocess_HOOK().

File

./answers.module, line 13
Contains answers.module.

Code

function answers_preprocess_node(&$variables) {
  if ($variables['node']
    ->bundle() == 'answers_question') {
    $values = [
      'title' => format_string('Answer to Question :nid', [
        ':nid' => $variables['node']
          ->id(),
      ]),
      'type' => 'answers_answer',
      'answers_related_question' => [
        $variables['node']
          ->id(),
      ],
    ];
    $node = \Drupal::entityTypeManager()
      ->getStorage('node')
      ->create($values);
    $form = \Drupal::entityTypeManager()
      ->getFormObject('node', 'default')
      ->setEntity($node);
    $form_array = \Drupal::formBuilder()
      ->getForm($form);
    $form_array['title']['widget'][0]['value']['#type'] = 'hidden';
    $variables['answer_form'] = drupal_render($form_array);
    $answers = views_embed_view('answers', 'block_1', $variables['node']
      ->id());
    $variables['answer_list'] = drupal_render($answers);
    $date_formatter = \Drupal::service('date.formatter');
    $variables['ago'] = $date_formatter
      ->formatTimeDiffSince($variables['node']
      ->getCreatedTime(), [
      'granularity' => 1,
    ]);
  }
  if ($variables['node']
    ->bundle() == 'answers_answer') {
    $date_formatter = \Drupal::service('date.formatter');
    $variables['ago'] = $date_formatter
      ->formatTimeDiffSince($variables['node']
      ->getCreatedTime(), [
      'granularity' => 1,
    ]);
  }
}