I am using custom external login form for posting the content and logging the user . Content is getting created and saved in user name using


$author = user_load(array('mail' => $form_state['values']['name']));  
$form_state['values']['uid'] = $author->uid;

But I am not able to login into the site.How I can achieve this ?

Comments

maheshg85’s picture

I am not able add image here . But its looks similar that of login form and both username and password text-fields are custom.

jaypan’s picture

You didn't give enough code to be able to help.

Contact me to contract me for D7 -> D10/11 migrations.

maheshg85’s picture

Following is my code..NOde is getting created but user not able to login.

/**
*implementation of hook_form_alter
*/

function property_account_form_alter(&$form, $form_state, $form_id){
  global $user;
// for not logged in user adding custom login form in node form.
   if(!($user->uid)){
   
     switch($form_id){
	   case 'propertysale_node_form':         
	     $form['name'] = array(
            '#type' => 'textfield',
            '#title' => 'E-Mail',
            '#description' =>t(''), 
	     '#validated'=>TRUE,           
            '#weight'=>-10, 
          );
         $form['pass'] = array(
            '#type' => 'password',
            '#title' => 'Password',
            '#description' =>t(''),
             '#validated'=>TRUE,         
             '#weight'=>-9,  
             '#suffix'=>"<div id='forgot-pass'>".l('<b>Forgot Your Password?</b>','user/password',array('html' => TRUE, 'absolute' => TRUE, 'attributes' => array('target' => '_blank')))."</div>",
         );	
      
     $form['my_captcha_element'] = array(
       '#type' => 'captcha',
       '#captcha_type' => 'image_captcha/Image', 
       '#access' => $access,
       '#weight'=>-2, 
       '#required' =>false,
       '#validated'=>TRUE,	   
     );  
     
       array_unshift($form['#submit'], 'property_account_submit');    
       $form['#validate']['']=	'property_account_validate';
	   break;
	 }
   }
 }

function property_account_submit($form, &$form_state){
     $user= user_load(array('mail' => $form_state['values']['name']));  

     $form_state['values']['uid'] = $user->uid;  // this will assign uid to created node
     $form_state['redirect'] = 'user/'. $user->uid;// here I trying to login the user using code form user_login_submit
    return;   
  }  

jaypan’s picture

Change this:
$user= user_load(array('mail' => $form_state['values']['name']));

To this:

     global $user;
     $user= user_load(array('mail' => $form_state['values']['name']));  

As a side note, this:
$form['#validate']['']= 'property_account_validate';

Should be this:
$form['#validate'][]= 'property_account_validate';
The way you wrote it will assign a new validation function with a key of an empty string. The way I wrote it will auto-increment the key from whatever the last key was.

Contact me to contract me for D7 -> D10/11 migrations.

maheshg85’s picture

Hi Jay Matwichuk,
Just changing the one line its allowing the user to login excellently, but now node is getting saved as anonymous . when I remove the line

  $form_state['redirect'] = 'user/'. $user->uid;

node is getting created in the user name . How I can achieve both i.e Saving node in user's uid and Logging him to site. Thanks for your last suggestion.Its excellent.

maheshg85’s picture


/**
  * Implementation of hook_nodeapi()
  */

function test_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
    global $user;
    switch ($op) {
    case 'insert': 
   
    if($node->type=='mycontent' ){
   if($node->uid=='0'){   
    db_query("UPDATE node SET uid='%d' WHERE nid='%d'",$user->uid,$node->nid);  
    
    }   
   }   
   }
  }