2

I want done a action (change some charachter) on every field in saving node.
I know for specified field can use hook_node_presave ,but I want do this on all field in all content type (proximity 300 field and 40 content types) with out specify one by one . Have drupal api for hook_filed_presave or something like it?

This is work for specified field

function custom_changes_node_presave($node){

    if ($node->type ==='designer_background_images'){

        $filename =$node->original->field_db_image['und']['0']['filename'];
        $uri       =$node->original->field_db_image['und']['0']['uri'];

        $filename    = str_replace (" ", "_", $filename);
        $uri        = str_replace (" ", "_", $uri);

        $node->original->field_db_image['und']['0']['filename']= $filename;
        $node->original->field_db_image['und']['0']['uri']       = $uri;

        //field_attach_presave('node', $node);

            // And the actual field change for the given node (e.g. entity).

        //field_attach_update('node', $node);

        // echo "<pre>";
            // print_r($node);
        // exit;
        // print $node->original->field_db_image['und']['0']['filename'];
        // print $node->original->field_db_image['und']['0']['uri'];
    }
}
5

You're probably looking for hook_entity_presave():

Act on an entity before it is about to be created or updated.

It's equivalent to hook_node_presave(), but as the name suggests is run for all entity types.

From there you can use the Field Info API functions to get a list of fields that should be attached to that entity, then loop through them and do the character replacement.

  • Thanks for your antention ,but I don't want work on field one by one, I need a popular functon act of any filed, something like hook_field_access . – Yusef Mohamadi Sep 9 '12 at 12:18
  • The problem with field hooks is they're only called for the module that implements them. There's hook_field_presave() which would be ideal, but you can't implement it unfortunately. Plus different fields have different column names arranged in different ways so implementing a generic function for that would be very difficult – Clive Sep 9 '12 at 12:33
  • Thanks. You think we have not way to achieve this populary without writing code to first get all fields of content type and after it apply my job on those? – Yusef Mohamadi Sep 10 '12 at 4:50
  • 1
    @zhilevan Sorry I missed your last comment somehow. Yes, I think you'll need to enumerate all fields that are relevant to whatever context you run this code in, and alter them that way. I'd love to see an easier way but I don't know of one – Clive Sep 12 '12 at 18:25
  • Thank you,I really say your one of best drupal developer I have been see. when you say don't know means almost of drupal developer don't know :). – Yusef Mohamadi Sep 12 '12 at 18:31
2

With this method you have access all fields used in your content types.
entity_get_info Doc and field_info_instances Doc take me idea how can I got them.

function custommodule_entity_presave($entity,$type) {
    $allFields=field_info_instances('node');
    $savedFields=$allFields[$entity->type];
    foreach($savedFields as $index=>$field) {
        /// this is just general answer and for perform this act I have to be more careful
        $entity->$field['und'][0] =my_custom_function ($entity->$field['und'][0]);
    }
}
-1

You can do it with this modules:

This 3 modules are a "must have" in every drupal projects with fields.

If they are all values, you should activated the "Retroactive update" to update all the old fields.

Oskar

  • I want to change value of filed in node save :( – Yusef Mohamadi Sep 9 '12 at 10:50
  • You have the options in transliteration and filefield_path to run a batch api process to update all the old fields to a new configuration. – Oskar Calvo Sep 9 '12 at 12:13

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.