Having a separate form submit function on user register/edit functions does not seem to be a good idea. Lets say, I were to use services module and I need to create users using some custom service that I created, I'll be calling user_save function over there and that should add the profile entity as well. But because I'm using an external submit function which gets added only in case of form alter, this wont work. Alternatively, profile2 should implement hook_user_insert and hook_user_update and then, by doing so, I'll be rest assured that on adding/updating any user account on the system, the profiles get added/updated respectively.

I can take this task and create a patch if the idea sounds good to the owner of this module.

Comments

fago’s picture

Title: Profile2 should implement hook_user_insert and hook_user_update instead of a separate function on user register/edit forms » Profile2 and services

I don't think so. Profiles are separate entities and not attached to the user - it's just on the form, so I think the code belongs to the form also. Thus for services, I think there should be a separate profile service.

Not sure whether that already works generally for entities with the services module, but at least with RestWS it already works.

mukesh.agarwal17’s picture

I was trying to import users using feeds module when I realized it would have been much better if I can import the profile entity along with user as well. Else, I'll have to map the UIDs with the user name and then use that uid as a field in mapping during profile import.

The point being that since we are doing the check in the submit function and only based on the profile values do we take any action, why not bundle it with user insert/update. Eventually, that is what the target is - club the profile entity with the user entity. We are already implementing hook_user_delete, hook_user_view over here.

In case, we continue with the current implementation, we'll have to re-write the same piece of submit code separately for all other tasks like services, import, etc.

To explain my point further, here is a patch that I have submitted in the feeds module - http://drupal.org/node/1228062#comment-5436554 which will allow me to create a generic import - i.e. attach any type of entity with any other type of entity.

Having said all of the above, I've not used profile2 extensively, so I cannot comment on various use cases that come out of this.

mukesh.agarwal17’s picture

Status: Active » Needs work

The issue has been inactive for quite some time. I can take the lead on this, if the owner thinks this is useful.

joey-santiago’s picture

I have a similar problem...

i am creating my users and would like to have their profile too, storing there some information. Then at hook_user_insert i need to send an email with those information, but at the moment a call to profile2_load_by_user($account) returns null.

the only idea i have at this point is to store that information in some user field instead...

skyredwang’s picture

Category: task » feature
Status: Needs work » Active

Sounds like that we need Profile2 Services support.

joachim’s picture

Status: Active » Closed (won't fix)

Should work with https://drupal.org/project/services_entity.

Reopen if Profile entities need any special handling that's not taken care of over there.

skyredwang’s picture

Category: Feature request » Support request
Issue summary: View changes
Status: Closed (won't fix) » Active

I have tried Services Entity API, from what I understand, it seems that we can use Services Entity API to create a Profile2 object. But, we cannot use Services Entity API to populate Profile2 fields.

This code will call Profile2 to create an object.

function _services_entity_resource_create($entity_type, $values) {
  $resourceclass = variable_get('services_entity_resource_class', 'ServicesEntityReso$
  $resource = new $resourceclass;
  return $resource->create($entity_type, $values);
}

Then the code below will create an object with only "type" and "uid".

function profile2_create(array $values) {
  return new Profile($values);
}

Even if the $values contain other fields information (passed in by Services), profile2_create won't take them.

skyredwang’s picture

Regarding comment above, the work around is to create an profile2 object. Then, use entity->update to add field information to the object. So, two services calls to achieve this goal.

joachim’s picture

> Even if the $values contain other fields information (passed in by Services), profile2_create won't take them.

Are you sure?

    // Set initial values.
    foreach ($values as $key => $value) {
      $this->$key = $value;
    }

The parent class of Profile does this.