I propose a patch that adds a hook on node_resource.inc which allows other modules to access the original node values (those provided before the node gets saved) and use them to do stuff on the node after it gets created/updated.

My use case is quite specific but I think it could be useful.

I have a node type, with an entity reference field that refers custom entities, and I use the widget inline entity form. On my hook_services_request_preprocess_alter implementation, I parse the POSTed/PUTed data, I create the new entities successfully, but I could not find any way to structure the values, in order to refer the new entities. So the node gets saved with empty values on that field.

Unfortunately, since the node_resource uses the drupal_submit_form in order to create the node, the form of the values depends on the widget. For example, I my widget was "autocomplete (tags style)", then a string that contains the entities ids into parentheses separated by commas, like: 34(34), 45(45), 56(56), would be ok. But it doesn't work for inline entity form. I spent quite a long time on trial and error, but I couldn't find a solution.

I know that my specific use case has nothing to do with the services module itself. However, I think that providing a hook that will allow the developer to have access on the original provided values, as they were before the node got saved, would be convenient in other cases too.

I attach a patch, and also an extra .api.php file to be placed into the docs folder (sorry, I couldn't find a way to include the new file into my patch)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

efpapado created an issue. See original summary.

efpapado’s picture

efpapado’s picture

tyler.frankenstein’s picture

Interesting, thank you for sharing. #2224803: Possible patch for multi-value fields may be related, but I can't say for sure as I've never tried updating an entity reference field that is configured for an inline entity form widget. I would guess the POSTed value should be in this format (and/or converted to a PHP array in the preprocess alter hook mentioned earlier):

field_foo: {
  und: [
    { target_id: 123 },
    { target_id: 456 },
  ]
}

As for the patch itself, it looks good. I'd vote to have both $node and $node_original passed to the hook though. Also it'd be nice if it was abstracted one more level out to the entity level, e.g.

module_invoke_all('services_entity_resource_post_create', 'node', $nid, $node, $node_original);

, that way we could drop it in the other resources in the future.

efpapado’s picture

Hi, thanks for the quick answer!

I tried to use target_id but it didn't work (maybe I did something wrong...)

I agree to your suggestion on an extra level of abstraction, but I can't find a suitable place to add it, since each resource defines its own functions that implement the CRUD functionalities. If you could suggest one, I can make the changes and submit a new patch :)

tyler.frankenstein’s picture

Status: Needs review » Needs work

You're right. Since each entity has its own resource file, we would have to call that single abstracted function from each of those files, one for each core entity type. Thank you!

efpapado’s picture

Status: Needs work » Needs review
FileSize
3.36 KB

I re-rolled the patch for the latest dev. Despite the initial problem I was trying to resolve, I think it would be a nice addition.

Mile23’s picture