1

I have seen hooks to use to alter webform options, but not the questions that appear on the left side of a grid.

I would like those questions to be a node titles actually of a content type that I have already defined.

For example, the content type titles will appear on the left and the options are already defined as first choice, second choice, and third choice radio buttons above in the header for each node title.

Here's the grid I would like to produce:

Node Title -- First Choice -- Second Choice -- Third Choice

Title One -- Radio Button -- Radio Button -- Radio Button

Title Two -- Radio Button -- Radio Button -- Radio Button

Title Three -- Radio Button -- Radio Button -- Radio Button

Title n -- Radio Button -- Radio Button -- Radio Button

Essentially I just need a way to import questions that are node titles of another content type into the webform Questions field that requires them to be inputted as pairs ex.

"safe_key|Some readable option"

"safe_key|Some readable option"

"safe_key|Some readable option"

Thank you very much.

  • Did you find a way to save the dynamic questions and the answer in the database? So you can show it in the results? Regards – Fumble Jan 4 '17 at 10:10
  • I ended up using the Form API. Code can be found here on github. – gabemartinez Jan 4 '17 at 19:17
1

Sure enough there is a way, a very neat one, just found it out myself!

In the following example I use a View's result to get the list of nodes I will use as the questions in the webform grid. This allows even easier management of the questions you want to display in your grid, by setting for example specific filters on your view.

You need to implement the hook_webform_component_render_alter hook. Here is the code you need:

function yourmodule_webform_component_render_alter (&$element, &$component) {
  // Select the right webform component
  if( $element['#type'] == 'webform_grid' && 
      $element['#title'] == 'Title of Webform Component' ) {

    // Override the title of the grid component
    $element['#title'] = "Node Title";
    // and use it as the label of the questions' column
    $element['#title_display'] = "internal";
    $new_grid_questions = array();
    // This is a standard content view that contains at least a nid and a node title field
    foreach( views_get_view_result('my_view', 'my_display') as $res ) {
      $new_grid_questions[$res->nid] = $res->node_title;
    }
    $element['#grid_questions'] = $new_grid_questions;
  }
}

And here is a screenshot of the end result:

enter image description here

Hope this helps!

  • This is fantastic and is helping me get further now that I have my titles/questions in place. I'm going to use this to update the ['extra']['questions'] array, as I'm assuming that is how they will show in the Results. Thank you. – gabemartinez Mar 22 '16 at 5:20
  • Do you have any advice about modifying the webform database to save the dynamic questions/node titles so to see those in results? Thank you. I just need more guidance in modifying the form and the saving of the dynamic data. – gabemartinez Mar 30 '16 at 21:26
  • Hey @gabemartinez, best thing would be to open a new question, add a link to this one and describe your new requirements! See you around! – Stefanos Petrakis Mar 30 '16 at 21:31

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.