Auto Assign existing nodes to new users content profile

Events happening in the community are now at Drupal community events on www.drupal.org.
Jamie_S's picture

Hi all,

I'm having what seems to be a faily common problem with a directory site, I've not been able to find a sollution by searching so here we go.

I have problems linking existing nodes (directory entries) to new accounts. I'm using a combination of Content Profile, Profile (core), CCK and Rules to acheive what I want.

Drupal Core 6.16
Content Profile 6.x-1.0-beta4
Rules 6.x-1.1

Users are able to register for an account either by selecting an existing directory entry and claiming it, or just create an unlinked account.

I have a custom field in the registration form provided by the core Profile module which gets auto filled if an existing node is chosen.

When the user registers and their email address is confirmed, at first login I use a Rule to run some custom php code like this:

global $user;
profile_load_profile($user);

$targetnode = node_load($user->profile_business_link);

if($targetnode and $targetnode->type == "business_details" and $targetnode->uid == 1)
{
   $targetnode->uid = $user->uid;
   $targetnode->revision = '0';
  node_save($targetnode);
}

This has the desired effect of automatically setting the node uid of the selected business listing to the new user uid.

However, while the business listing shows up on the user page as though the profile is linked to the user, the edit tab at the top is missing and if they try to edit the node by navigating to the page directly they get an access denied page.

I know the permissions are correct for the role because new users who do not select a business on signup are able to hapilly create profiles and edit them. They also get the correct edit tab at the top of the user page.

Has anyone else hit this stage and was able to resolve it? Do I need to do more than just change the uid on the target node to change ownership?

Any thoughts appreciated.

Comments

Resolved

Jamie_S's picture

Well, I seriously doubt this is the most elegant way to handle this problem but after banging my head against it for a few days, here is the result.

I was not able to gain edit privileges for the new user on existing nodes so I rewrote my triggered Rules code to do a quick copy->create->delete. A new node is created from a copy of the existing content with the new user as creator, then the existing content is deleted.

Here is the code:

global $user;
profile_load_profile($user);

$targetnode = node_load($user->profile_business_link);//Core profile module variable set at registration

if($targetnode and $targetnode->type == "business_details" and $targetnode->uid == 1)//Check the type and not already claimed
{
    try
      {
        $profile_node = $targetnode;
        $profile_node->created = $time;
        $profile_node->changed = $time;
        $profile_node->status = 1;
        $profile_node->promote = 0;
        $profile_node->sticky = 0;
        $profile_node->format = 1;       // Filtered HTML
        $profile_node->uid = $user->uid; // UID of new user
        node_save($profile_node);
        node_delete($targetnode->nid);//delete old version
      }
      catch (Exception $e)
      {
        //tmp debug
        echo 'Caught exception: ',  $e->getMessage(), "\n";
      }
}

It's strange that you have

skylake's picture

It's strange that you have delete permission but no update permission.

Profiles as nodes

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week