0

I want to change "Your answer" in the add answer form of the Answers module so that it says "Give [user who asked question] your answer".

Here is a snippet of code from answers.module that defines the value

// 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'),
          '#weight' => -100,
        );

I tried changing it to:

// 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('Give ', $author, 'your answer'),
          '#weight' => -100,
        );

Not surprisingly, this didn't work. I'm not sure which variable to use for $author, and other changes, so that the username of the person who asked the question appears there.

Disclaimer: I apologize if the question is off, I work for a non-profit organization that isn't a tech company and am just trying to build us a new website but have a gluten for punishment and an interest in getting back in to the tech field. Thanks for your patience.

Edit: I created a helper module for customizations to other modules.

  • 2
    You should never change module files directly. Instead, you can create a new module and implement alter hooks to override output from other modules. Second, that is not how the t() function works... I suggest you read its documentation first and update your question so it includes valid code. – Neograph734 Aug 11 '17 at 22:29
  • Okay. Is new_answer_form_title a hook so I could drupal_alter('new_answer_form_title', 'new_answer_form_title_new'); with $new_answer_form_title_new = array (some stuff);? – Christia Aug 12 '17 at 0:13
  • Please double check what you put up here. There is no $new_answer_form_title_new in drupal_alter('new_answer_form_title', 'new_answer_form_title_new'); so that does not make sense. The clearer you are, the better we can help. Do not rush it, better to read it over and make sure is correct :) – Neograph734 Aug 12 '17 at 7:47
  • That question was good, buy you were mixing up variables and strings in your example, which in turn confused me... ;) I hope the below answer gets you somewhere. – Neograph734 Aug 12 '17 at 8:42
2

Since you have the node object, you can find its author via $node->uid. Then load the account and wrap it in format_username():

$uid = $node->uid;
$account = user_load($uid);
$account_name = format_username($account);

The hook you need depends on the function in which your snippet was found. In your case it is in hook_node_view, so you need hook_node_view_alter()

From there it is as simple as redefining the value. Something like this:

MODULENAME_node_view_alter(&$build) {
  $node = &$build['#node'];

  $new_title = t("Give @author your answer", array('@author' => $account_name));

  $node->content['new_answer_form']['new_answer_form_title']['#value'] = $new_title.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.