Silently create a user account when another form is submitted - drupal

I have a form created from another module. I want to add 2 fields to that form: 1. email, 2. password. I think I can do this with hook_form_alter. Then I would like to create a user account and log the user in when the submit button is clicked, then go ahead and execute the action defined by the form.
The original form doesn't have a #submit property...it just has a #action property.
I add the #submit property like this: $form['#submit'] = array('accesscustom_submit');
but accesscustom_submit doesn't seem to be getting called. I think the form is just getting redirected to the #action url that's already defined. Any ideas?

Are you trying to edit the comment form? $form['#action'] is a pretty rare property ... the comment form is the only one I can think of that uses it.
In any case, you can create an account pretty easily by settings up a user array ($account = array('name' => $username, 'pass' => $pass)) and sending it to user_save. See http://api.drupal.org/api/function/user_save/6 for more details.
Once the account is created, you can call user_external_login to log them in (http://api.drupal.org/api/function/user_external_login/6)

Related

Can a Google Form spawn another Google Form

Is it possible, (via Appscript), for one Google Form "Form A" to create and open another Google Form "Form B" during the onSubmit trigger of Form A?
I want to ask a user how many entries they want to make on Form A. Then I want Form A to effectively "call" Form B for each entry desired.
I can create the form dynamically or FormApp.openByUrl() but they both fail with an error. I'm beginning to wonder if a Form cannot be subordinate to a Form?
Any experience with this?
You can create a copy of your form with the onSubmit trigger of your source Google form.
function createGoogleForm() {
const sourceFormId = "xxyy";
const file = DriveApp.getFileById(sourceFormId);
return file.getUrl();
}

custom variable issue in google analytics

Hi am using the following code to track the clicks of the users using custom variable. But in my custom variables report the count is getting increased for user log in but not for button click event. Am storing logged in user id in a variable and passing in the custom variable method as the value.
jQuery(document).ready(function(){
jQuery('#buybutton').on('click', function() {
_gaq.push( ['_setCustomVar', 2, 'gaid', <?php echo(json_encode($userName));?>, 1 ] );
_gaq.push(['_trackEvent','button-webtrader','Click','webtrader-buttonclickevent']);
});
});
I have find a solution for this. Instead of custom variables, i have used event tracking to achieve this. I have used the following function in the click event of the button.
_gaq.push(['_trackEvent','button-category','Click','Clicked by the user:<?php echo(json_encode($userName));?> ']);
"$userName" above contains the logged in user id from my site. Now, i can see the clicks of a particular user using his id under Behavior->Events->Pages->Your Page->Event Category->Event Action->Event Label. As username is personally identifiable info according to google, i didn't use username.

Registering new users via OAuth2 : what to set as user identifier for future log ins?

I have managed to successfully configure this. The problem is, when I change the lines below :
//I have set all requested data with the user's username
//modify here with relevant data
$user->setUsername($username);
$user->setEmail($username);
$user->setPassword($username);
into the information I want to retrive, such as real name, email, my generated password etc, when I click the Login button for Facebook per say, I am asked again if I want to connect with my local testing site.
From what I understand, in the documentation I linked above, this :
$user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
is the line that checks if the user exists or not, and the initial code by itself, sets either facebook_id or twitter_id (this is how I save them) as a new User *username*. If I change the line
$user->setUsername($username); //same as facebook/twitter _id
into
$user->setUsername(setProperUsername()); //sets a proper unique username
Then everytime I try to login I get the "Register" message. So, I have a general idea of how it works but I am having a hard time understanding some things:
1. When I have registered with Facebook and I login with twitter, I register again, no knew row is created, but missing twitter_id fields are updated/populated, username stays intact. How come HWI/FOSUB knows I am the same person when my previous data were from Facebook not Twitter?
2. If there is a global way of knowing I am the same person, what data from the $response object should I use as a key to identify already registered users?
After testing a lot with this, I have the answer if anyone runs into this type of situation
Check your default_target path, make it so it is /profile, /account etc, don't default to login again. Also, if a user is already logged in, do not make him access your login page. This was why my data was being updated. I was basically logged in with my Facebook account and registering with my Twitter account too.
No, there is no global way of knowing I am the same person. The $response object sent me a unique ID for that specific user according to the provider policy. You might use this to identify already registered users and log them in.

Getting user entered password in hook function in Drupal 6

I want to get user details like username and password which is entered by user in My account page and admin user edit page also. In my custom module, I am using mymodule_user($op, &$edit, &$account, $category = NULL) hook function.
How to get user entered username and password details when user form submits?? If this function is not correct then from which hook function can I get user details?
$account->name has the account name reliably.
The password is encrypted in the user table, and so the only place you'd be able to see it is when the user creates their account or changes their password. In that case, you'll need to pull it from $edit.
Indeed your value lies on the $edit for the $op login and edit .
You could also hook_form_()alter these forms and on your submit handler, the value you are looking for is in the $form_state array

Modifying Wordpress post status on Publish

I am attempting to validate fields on a custom post type in the admin panel Edit Post page.
When the user clicks "Publish" I want to validate fields in the POST data, and change the post_status to "pending" if the data does not pass the tests. When this occurs, I'd also like to add errors to the page in the admin notices area.
I've been trying this with an added hook to the "wp_insert_post" action which also saves our own data. I'm not certain of the order of operations, but I'm assuming that the wp_insert_post events happen first, and then my function gets called via the hook.
The problem is that it's the Wordpress function which is doing the post publish actions, so by the time I get to validate data, Wordpress has already saved the post with a status of "publish". What I need to do is either prevent that update, or change the status back to "pending", but I'm having little success in finding a way to do this within the API.
So, here's an order of operations I'd like to effect:
1. admin user edits post data and clicks "Publish"
2. via wp_insert_post, my data validation and post meta save routine is called
3. If data passes validation, post status is "published"
4. Otherwise, post status set to "pending" & message shown in admin notice area
Surely someone has done this, but extensive Googling just leads me to the same seemingly irrelevant pages. Can someone point me in the right direction here? Thanks in advance-
UPDATE:
So, RichardML was indeed correct, hooking to the wp_insert_post_data filter gave me the right place to validate admin post edit page fields. I'm updating this however to note what the rest of the solution is, specifically getting the reason reported in the admin notice area.
First off, you can't just output data or set a field because the admin page is the result of a redirect, and by the time you get to rendering the admin post page again, the admin_notices action is already gone. The trick was something I picked up from another forum, and it's hackish, but it works.
What you'll need to do is in your validation filter function, if you determine that you will need to display errors, is use set_option() to add a blog option with a unique name (I used 'publish_errors'). This should be HTML code in a div with a class of "error".
You will also need to add an action hook for 'admin_notices', pointing at a function which checks for the existence of the 'publish_errors' option, and if it finds it, prints it to the page and deletes it with delete_option().
You can use the wp_insert_post_data filter to inspect and modify post data before it's inserted into the database.
In response to your update I don't think it's necessary to temporarily add an option to the database. It should be possible to simply add a query string variable to the Wordpress redirect, something like this:
add_filter('wp_insert_post_data', 'my_post_data_validator', '99');
function my_post_data_validator($data) {
if ($data['post_type'] == 'post') {
// If post data is invalid then
$data['post_status'] = 'pending';
add_filter('redirect_post_location', 'my_post_redirect_filter', '99');
}
return $data;
}
function my_post_redirect_filter($location) {
remove_filter('redirect_post_location', __FILTER__, '99');
return add_query_arg('my_message', 1, $location);
}
add_action('admin_notices', 'my_post_admin_notices');
function my_post_admin_notices() {
if (!isset($_GET['my_message'])) return;
switch (absint($_GET['my_message'])) {
case 1:
$message = 'Invalid post data';
break;
default:
$message = 'Unexpected error';
}
echo '<div id="notice" class="error"><p>' . $message . '</p></div>';
}

Resources