0

I need to programmatically save field data (during node save I want to take the title & path alias and store it in a link field called field_alternative_titles_title).

Which functions can I use here? Do I use field_attach_insert or hook_field_storage_pre_insert to save the link field?

9
  • The nature of the code will obviously depend on the specifics - can you edit your question and show the code you've tried, explaining where it's going wrong?
    – Clive
    Commented Aug 7, 2015 at 14:28
  • I hope that made it clearer
    – Eluchel
    Commented Aug 7, 2015 at 14:39
  • I think so...if I understand you right, hook_node_presave() might be the best place. It gives you access to the whole node before changes get sent to the database, and you can safely manipulate field data before it gets persisted
    – Clive
    Commented Aug 7, 2015 at 15:21
  • what entity your field is attached to? is it attached to the same bundle? or you want to store it directly to DB? Commented Aug 7, 2015 at 15:23
  • My field is attached to a field collection item, and that field collection item is attached to a content type of mine
    – Eluchel
    Commented Aug 7, 2015 at 15:26

1 Answer 1

2

At first you need to implement hook_node_presave($node)

Act on a node being inserted or updated.

This hook is invoked from node_save() before the node is saved to the database. Parameters

$node: The node that is being inserted or updated.

So it reacts when a node is going to be saved , then you have to check if the node type is the proper node type . then setting field value , so this is some code snippet :

function mymodule_node_presave($node) {
  if($node->type == "Your Node Type") {
      //dsm($node);//it will show the structure of node
      // as example
      $data = $node->title.'-'.drupal_get_path_alias('node/'$node->nid);
      $node->field_alternative_titles_title[$node->language][0]['value'] = "data";//check the node structure,this code wont work for sure
  }
}

of-course this is just an snippet , to make it work you have to know the structure of your content type , Devel module will help you , install and enable it so dsm function will work.

4
  • Will my field_alternative_titles_title table in my database get populated with this code? @Alireza Tabatabaeian
    – Eluchel
    Commented Aug 8, 2015 at 1:24
  • It had to, if the node which is going to be saved contains field_alternative_titles_title , then it will affected in DB Commented Aug 8, 2015 at 6:35
  • Now, if the node doesn't contain the field_alternative_titles_title field, but instead contains the field collection item field_alternative_titles, and the field collection item field_alternative_titles contains the field field_alternative_titles_title. Would it still work if that was the case?
    – Eluchel
    Commented Aug 10, 2015 at 14:24
  • if it is a part of node (no matter how or how deep just seen in form in enough in this case) yes it will works but you have to address it right . maybe $node->field_alternative_titles_title is not the right destination , to find the right destination use dsm function Commented Aug 10, 2015 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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