Cannot create nodeprofile programatically with drupal_execute

davidtrainer - April 9, 2008 - 18:11
Project:Node Profile
Version:5.x-1.x-dev
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

I am trying to use drupal_execute to submit a nodeprofile programatically, for a user other than the logged-in user. it will work when the logged-in user is an Administrator (with administer nodes access) or Anonymous. I can't change the uid in the _validate hook before nodefamily gets to it. So whenever I try to do this with an authenticated user, nodefamily throws the error, "You can't create more nodes of this type for this user."

#1

EgbertB - April 11, 2008 - 13:38

We suddenly have the same problem using the userprofile module. Did you find a solution yet.

EgbertB

#2

jeetendra - April 28, 2008 - 17:39

i face the same problem because i insert user profile direct from my php script and leave node_revision table
blank so it produce error , please check

#3

dorien - May 14, 2008 - 07:49

subscribing

#4

olivers - May 23, 2008 - 15:48

Same with me.
I populate the forms of my nodeprofile subnodes (nodefamily-nodes) with the uid and name of the newly registering user and then use drupal_execute to submit those forms.
If I do so with a 'real' new user it works - when i use the user_register form via drupal_get_form in my management section it fails with the error given above.

It seems, nodeprofile or node family do not make use of my given uid but insert the uid themselves.

#####EDIT:
I just had a look at the sources and came across this section in nodefamily_nodeapi:

    case 'validate':
        if (!$node->nid && user_access('administer nodes')) {
          if ($account = user_load(array('name' => $node->name))) {
            $node->uid = $account->uid;
          }
          else {
            $node->uid = 0;
          }
        }

This means,
1. the user that uses the form needs to have 'administer nodes'-permission,
2. the form/subform needs to include the user name field
otherwise the nodefamily handler sets the uid to 0.

My workaround:

1. in my form I populate user name and id from the given user account (all of this is done in my hook_user function under "register":

$node = array(
      'type' => $nodetype,
      'title' => check_plain($nodetitle),
);
...
$fieldValues['uid'] = $account->uid;
$fieldValues['name'] = $account->name;
...
drupal_execute($nodetype.'_node_form', $fieldValues, $node);

2. strip out the user_access portion in nodefamily_nodeapi :

(maybe someone could provide a patch for that)

case 'validate':
        if (!$node->nid && user_access('administer nodes')) {

needs to look like
case 'validate':
        if (!$node->nid) {

Now my user_register form and subforms are working.

btw, this seems to be a nodefamily issue actually...

#5

vesale - August 13, 2008 - 16:00

Could you explain more in details what you mean by:

"1. in my form I populate user name and id from the given user account (all of this is done in my hook_user function under "register":"

Thx

 
 

Drupal is a registered trademark of Dries Buytaert.