I have an entityreference field that references a redhen_contact entity. I want to set the node's author to be the user associated with the contact referenced in this field.
This is part of a (I think, rather basic) custom rendition of the node form via hook_menu()
and drupal_get_form
.
I can get the user from the entityreference field and I can print its name
, uid
, email
etc. with drupal_set_message
. And when I print $node->uid
with drupal_set_message
it shows up as if I was successful, but it gets overridden apparently and I have no idea where. The saved nodes always show that I'm the author (as I'm the one submitting the form).
I first tried using hook_form_alter()
, then I tried using hook_node_presave()
, then hook_node_insert()
. I don't know of any hooks that fire later. But maybe one of these hooks would work if I did it right?
Here's most of what I've tried:
<?php
function mymodule_register_existing_adult_submit($form, &$form_state) {
$registrant_id = $form['#node']->field_registrant['und'][0]['target_id'];
// $contact_wrapper = entity_metadata_wrapper('redhen_contact', $registrant_id);
// $registrant_name = $contact_wrapper->label();
// $registrant_email = $contact_wrapper->email->value();
// $registrant_user = $contact_wrapper->user->value();
// $registrant_url = $contact_wrapper->user->value();
// drupal_set_message(t("Hey this works! " . "ID=" . $registrant_id . " " . $registrant_name . " " . $registrant_email . "<br>" . $registrant_user->uid));
// sdpm($form);
//$form['additional_settings']['group']['#groups']['additional_settings'][0]['name']['#value'] = $registrant_user->uid;
//$form['additional_settings']['group']['#groups']['additional_settings'][0]['name']['#default_value'] = $registrant_user->uid;
//$form['author']['name']['#default_value'] = $registrant_user->uid;
//$form['author']['name']['#value'] = $registrant_user->uid;
//$form_state['complete form']['uid']['#value'] = $registrant_user->uid;
//$form_state['complete form']['uid']['#value'] = $registrant_email;
//$form_state['values']['uid'] = $registrant_user->uid;
//$form_state['build_info']['args'][0]->uid = $registrant_user->uid;
//$form['uid'] = $registrant_user->uid;
// $form['#node']->uid = $registrant_user->uid;
//dpm($contact_wrapper->getPropertyInfo());
$form_state['redirect'] = 'redhen/contact/' . $registrant_id . '/view/edit';
}
function mymodule_node_presave($node) {
if ( $node->type == 'program_registration' ) {
$registrant_id = $node->field_registrant['und'][0]['target_id'];
$contact_wrapper = entity_metadata_wrapper('redhen_contact', $registrant_id);
$registrant_user = $contact_wrapper->user->value();
$node->author = $registrant_user->name;
drupal_set_message('Hook implemented successfully.<br>User ID: ' . $registrant_user->uid);
drupal_set_message($node->author);
sdpm($registrant_user);
}
}
function mymodule_node_insert($node) {
$path = current_path();
if ( $path == 'admin/register-existing-adult' ) {
drupal_set_message('hook_node_insert author: ' . $node->author . '<br>Path: ' . $path);
$registrant_id = $node->field_registrant['und'][0]['target_id'];
$contact_wrapper = entity_metadata_wrapper('redhen_contact', $registrant_id);
$registrant_user = $contact_wrapper->user->value();
$node->author = $registrant_user->uid;
$node->uid = $registrant_user->uid;
}
}
Edit:
Where I'm at now is I've cleaned up the code to see more precisely what's going one:
<?php
function mymodule_node_presave($node) {
if ( $node->type == 'program_registration' ) {
$registrant_id = $node->field_registrant['und'][0]['target_id'];
$contact_wrapper = entity_metadata_wrapper('redhen_contact', $registrant_id);
$registrant_user = $contact_wrapper->user->value();
$node->author = $registrant_user->name;
}
}
function mymodule_node_insert($node) {
$path = current_path();
if ( $path == 'admin/register-existing-adult' ) {
drupal_set_message('author: ' . $node->author);
}
Here hook_node_insert()
is successfully printing the email address for the user associated with the contact entity. So it seems that the code in hook_node_presave()
works, except that it must then be overwritten by Drupal afterwards? Or maybe the email address is not a valid value for the node author?