Question

I have a form that calculates two values, form works perfectly, however i'm having some problems working out how I can calculate the two values and return the answer as rendered HTML so I can style it all nice and pretty for the user to see.

All help will much appreciated. Thanks.

Was it helpful?

Solution

In a Drupal form there are three types of callback that are normally necessary to create a form:

  • The form builder is the function that builds the form using an array the form API understands
  • The form validation handler is the function that validates the values submitted from the users
  • The form submission handler is the function that acts on the values submitted from the users

Of those functions, the one that outputs the result of the calculation done on the values submitted from the users is the last one. Normally, the form submission handler saves the data in the database, and show a message to the users through drupal_set_message(), but it could also simply show the result of the operations done on the entered data.

Since Drupal 7, the output of a form submission handler, as well as the output produced from a menu callback, can be a rendering array, and not a string. A rendering array has the pro of being easily altered from third-party modules through hook_page_build(), or hook_page_alter().

node_view() is an example of menu callback that returns a rendering array, and not a string.

OTHER TIPS

If it's just a message you want then you should look into drupal_set_message. It allows you to set a message like this:

drupal_set_message(t('Hello, world.'),'myClass');

Which outputs HTML like this:

<div class="messages myClass">Hello, world</div>

That would be a very simple way to set a message for the user to see once the form is submitted. If you need something a little more advanced, look into my answer here and this Drupal answer here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top