1

I have field in content type called field_new_author which is User reference field.

I need to make a Rule to change Node author field to match value in field_new_author. I tryed this solution: Change node author on node save with rules

But I'm getting database error: PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4141-0-all' for key 'PRIMARY': INSERT INTO {node_access} (nid, realm, gid, grant_view, grant_update, grant_delete) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5); Array ( [:db_insert_placeholder_0] => 4141 [:db_insert_placeholder_1] => all [:db_insert_placeholder_2] => 0 [:db_insert_placeholder_3] => 1 [:db_insert_placeholder_4] => 0 [:db_insert_placeholder_5] => 0 ) in node_access_write_grants() (line 3440 of /home2/test/modules/node/node.module).

I also tryed different setups in Rules but could not find anything that would work.

1

I assume you are using D7 with Entity Reference. You should be able to do this with stock Rules, without any PHP code:

  1. Create a new rule
  2. Event, select "After updating existing content"
  3. Condition, select "Content is of type" to match whatever content type it is. This will qualify the fields so that you can use them later
  4. Condition, select some other limiting condition so that you don't target every single node with updated content. You could, for example, do a data comparison to check if the content has a given taxonomy term
  5. Action, select "Set data value". Your target should be node(updated content):author. Your new value should be node(updated content):field-new-author
1
  • It only change the node uid in the node table not in the node revision table.
    – Ahmad
    Apr 13 '17 at 11:01
0

I'm not that experienced with the Rules module so I can't help you in that regard. However you could create your own module and implement this hook:

hook_node_presave

This hook allows you to modify the node object before it is saved or updated within the database. You'll then be able change the Node author value to the value of your User reference field within your own module. Remember to check the content type of the node first to ensure values are only changed on your intended nodes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.