Custom Wizard Plugin


(Per Torstensson) #103

Ok. New feature request which possibly is super edge case and not worth mentioning, but I thought I’d just put it here for consideration;

I will use Custom Wizard for users to post their guesses on how the World Cup group play will end. This will be pretty straight forward; 8 steps with 4 dropdown fields each which will result in a published topic, which other users can comment. Pretty neat and will work perfectly out of the box.

However, posting their guesses for the playoffs is another matter. Due to the structure of the World Cup knockout phase (e.g. 2nd in group B meets 1st in group A etc. in the round of 16), I have planned to solve this by creating;

  1. one step with 8 dropdown fields with 8 choices to select the winner of each knockout match in the round of 16
  2. a second step with 4 dropdown fields with 16 choices to select the winner of each knockout match in the quarter finals
  3. a third step with 2 dropdown fields with 32 choices to select the winner of each knockout match in the semi finals
  4. a fourth and final step with 2 dropdown fields with 32 choices to select the winner of the final and play-off for third place

This will work but with the obvious downside that the user can input contradictory results.

I am now asking if it would be possible to add functionality to load stored input values from older steps. E.g. if field_1 was stored in step_1, it would be possible to render the stored value in a later step in any title, description, label, key or value field. The syntax could be like with post builder; w{field_1}, w{field_2} etc.

If this could be implemented, I believe that I, in my very specific use case, could create the same amount of steps and dropdown fields, but with only 2 choices to select the winner of each knockout match throughout the whole playoffs. I haven’t given it much thought, but I believe there should be reasonable and relevant other use cases for this functionality as well.

Thank you for considering.


(Angus McLeod) #104

Interesting :slight_smile:

This is possible at the moment via a secondary plugin.

You can add a step handler using CustomWizard::Builder.add_step_handler that gets run after each step of a certain wizard that gives you access to all the fields of the wizard and the values submitted up to that point.

Here’s an example (this is the whole plugin).

plugin.rb

DiscourseEvent.on(:custom_wizard_ready) do
  CustomWizard::Builder.add_step_handler('wizard_id') do |builder|
    updater = builder.updater

    if builder.updater && builder.updater.step && builder.updater.step.id === 'step_id'
      input = builder.updater.fields.to_h

      if builder.submissions.empty?
        builder.submissions.push({})
      end

      builder.submissions[index_of_step]["field_id_to_fill"] = input['field_id_of_value']
    end
  end
end

(Per Torstensson) #105

Wow! Just wow! I am so hopeful for this, thank you so much for this @angus!

Trying to implement it though, I notice I am not as keen at this as I would like to be… Other than javascript for the front end and other supported plugins, this is my first foray into adding “my own” code to my installation.

I tried to be a good citizen by creating a repo (my first, sort of - yay \o/!) in which I added your code:

I then tried to add the plugin via the officially supported ways to my installation:

However, after installation it doesn’t seem to load earlier step values, and what’s more is that I can’t access the plugin section or upgrade section of the admin part of my discourse installation.

It is reasonably something obvious I am missing in the plugin.rb, but I can’t understand what - Any pointers in the right direction on what I am doing wrong installing this as a plugin is highly appreciated!


(Angus McLeod) #106

So far, you have the code I provided in the plugin.rb :+1: You need to replace the dummy values I used with the actual step and field ids.

That said, I decided I will in fact expand prefilling of fields.

Currently, fields used by actions that update the user profile are prefilled with the relevant profile field data, if it exists.

This will be generalised, so you will be able to prefill the text, textarea, dropdown and composer field types with:

  • Values from other fields; or

  • Translations with params (i.e. you could identify a field to be interpolated into the translation string); or

  • Custom text with interpolated values (i.e. like the post builder); or

  • Values in the user’s profile.

I’ll add this early next week.


#107

Is it possible to use variables in the topic title when the using the Create Topic action?

I’m using the wizard to have people fill out an application for our gaming guild, which then gets turned into a post. I was really hoping to include u{username} in the topic title so that I didn’t end up with 100 topics entitled “Application”.

(Wonderful little wizard, btw. Thank you!)