"Tokenized email action" is not picking up my tokens - drupal

I'm having some problems setting up a tokenized email to use the tokens I've created in my module. I am using this in a tokenized email [example-contact]. I've implemented the example_token_values() and example_token_list() in my module and I've also created a trigger in my module. My module has a form, created with the form API, that gets stored in a custom table. My tokens are listed when I view all available token so I know that example_token_list() is working but when example_token_values() is called $type doesn't come up as equal to 'example'.
I need to pull information from the submitted form and display them on the tokenized email that is sent out. Am I missing a function? The Trigger I created is working and fires when the form is submitted and the Action is sending out the email the problem is that the tokens are not being replace with the form's values.
Is there a function that I need to implement that will call example_token_values("example",$form)?

An implementation of hook_token_values() can use any values for $object; if the module needs to use the value of $form passed to one of its functions, it can use it.

Related

Associate WP/WooCommerce user with CF7 form

If using a CF7 form in a "members area" of WP, is there a way to associate who submits the form if the user was originally created through a WooCommmerce purchase?
I know we could just ask for their user name as part of the form, but I'm looking for a better way to solve this where the form automatically pulls in the user ID. We will then push the form submission through a webhook and add automation on the backend based on the form response. It's just this first part I'm looking for help with, and I have not found any good articles on it.
WordPress allows you to query who is currently logged in user for a given requests/form submission with the function get_current_user_id(). However, logged-in user credentials are not preserved during a REST api/AJAX call. CF7 uses the REST api to submit forms and therefore it is not possible to use this function for a CF7 form submission.
You have 2 ways to solve this problem,
.1. Use the Smart Grid-layout extension which fixes many coding issues of the CF7 plugin, one of which is the preservation of user credentials on form submissions, therefore allowing you to use the above mentioned function,
add_action('wpcf7_before_send_mail', 'get_cf7_user');
function get_cf7_user($form){
$user_id = get_current_user_id();
switch($user_id){
case 0: //no user logged in.
break;
default: //registered user ID.
$user = wp_get_current_user(); //this is another fn which will get you the user object.
break;
}
}
.2. alternatively you will need to add the current user's ID into the form as a hidden field to retrieve it once the form is submitted. See this answer for an example.

Drupal Quiz Module - emailing results after taking the quiz

I have a Drupal website that uses the Quiz module to administer tests to visitors. These tests need to be available to anonymous users. My problem is that I need to be able to ask the test taker to enter name and e-mail so the results can be sent to them. I just don't know how to go about doing this. I consider myself a beginner in Drupal and PHP.
Any help would be appreciated.
-First of all, create a custom module.
-Secondly, you need to add the email address and name fields. You can do this by either adding the two fields via hook_form_alter in your custom module or by enabling and using the Short Answer module/field that's included in your Quiz module and then customising the style of the field according to your needs (because it'll look like a question). Personally, I would recommend adding them using hook_form_alter. Plus, I suggest you learn about Hooks in Drupal, it will make your life easier.
-You have to validate and retrieve the values for the two fields. You can also use the same form_alter hook for this. Add a validation and a submission function to the validate and the submit stack of your Quiz form ($form['#validate'][] = 'your_validate_function'; and $form['#submit'][] = 'your_submit_function';). You can validate your email by using Drupal's function valid_email_address and, of course, you can validate other fields and calling Drupal's form_set_error to notify users of any input mistakes.
-To send your email after the Quiz is submitted, call Drupal's drupal_mail, in your submit function, which basically takes all the parameters needed to send an email. You'll have to create a hook_mail in your custom module. Check out an example of how to do this here. You can retrieve your form values (name and email address) from the local array $form_state['values'], pass them as $params to your drupal_mail function and add them to the body of your email in your hook_mail function. And that's it :D
-Alternatively, you can send an email by creating an action and assigning an action to be performed after a user has completed this Quiz. The Quiz module has support for that. Here's an example of how to write an action.
You may also use hook_quiz_finished instead of form submit callback.
Quiz module uses it to perform actions like sending results over email at the end of a quiz.
function mymodule_quiz_finished($quiz, $score, $session_data) {
//Sending e-mail.
}
If you ended here and you are using Drupal 7, go to Rules http://www.yourwebsite.com/admin/config/workflow/rules, and set the rule that the Quiz module has made available already to active under the settings, the rule is called "Send quiz results at the end of a quiz". I couldn't find the ability to do this anywhere within the Quiz config itself. Tested and its working. Be sure that the Rules Module UI is enabled to allow you to make the edits.

How to update the value of a single field invoking appropriate validation

I'm making a module to allow users to update single fields on in this case, their user entity.
The code below is an example of the method I have initially been using to get it working and test other elements of the module
global $user;
$account = user_load($user->uid);
$edit = (array) $account;
$edit['field_lastname']['und'][0]['value'] = 'test';
user_save($account, $edit);
However this bypasses any field validation defined elsewhere in Drupal. I don't want to reproduce any validation written elsewhere - it's not the Drupal way!
My question is: Is there a function in Drupal 7 that can be called to update the value of a single field. I imagine such a function would clear the appropriate caches, invoke the fields validation etc.
I am aware the solution will be totally different to my current user object based one. I just can't for the life of me find the appropriate function in the API. I wander whether the fact I am looking for a save function alone is the problem - and that there are some other necessary steps that come before.
Any help gratefully appreciated.
Check out the drupal_form_submit function. It lets you submit forms from code. In this case, you could use it to the user edit form, which would then fire the appropriate validation.

Drupal: customizing validation error messages

When the user submits a custom CCK form and a field marked as REQUIRED is empty, I get an xyz field is required..
How can I customize this message without modifying core modules?
Depending on the error message and how you need to change it, you may be able to use the String Overrides module to replace the string used to generate the message.
Another option is to create a custom module that overrides the validation function for a particular form or field, replacing any error messages with the messages of your choosing.

How do I display data from an external database in Drupal?

I am building a custom module that will allow my users to do a simple query against an MS SQL database. I've built the form using hook_form() and have gotten validation to work.
I'm planning on retrieving the data from hook_form_submit(), but once I've done that, how do I append it below the form? It does not appear that I have access to $output from hook_form_submit(). I'm at a loss as to what to do next.
Thanks
Dana
When you are rendering the form you should check for $form_state['values'] to see if the user has already submitted a form when you're rendering the form. Then you could paint the form results in the same step as painting the form.
The first time the user loads the form page the $form_state variable won't contain any submitted form info so you can render an empty results table.
There's a good illustration of the Drupal Form API workflow on Drupal.org here: Form API Internal Workflow Illustration
The problem in trying to output data in the hook_form() method is that the method gets invoked twice which clears the post values the second time through. Throw a dpm($form_state) in the hook_form() function and you'll see two sets of post data. One with values and one without.
So after dissecting the built in Search module, which pretty much operates exactly the way I want my form to work, I figured out how this is done. Well, at least one way you can do it.
What Search module does is take the values from $form_state in hook_form_submit() and pastes them into the URL, then it sets the $form_state['redirect'] to that new URL, effectively storing those variables in the URL and changing the POST to a GET.
Now, in the callback, they extract those values from the URL, do the search on them, THEN they call drupal_get_form(), append the results to the end and return it.
There's another solution HERE where they use SESSION to store the values until the second trip through. Weird, but it works.

Resources