0

I am trying to set the text in a custom content field when saving a node. I think I need to create a module to do this. (I am new to module creation.)

I started with a blank directory called - let's say myModule. I placed myModule into path sites/all/modules.

I placed into the myModule directory two files that I understand to be required and gave them the following naming conventions which I also believe to be the standard:
myModule.module
myModule.info

myModule.info holds the following text:   
name = Some Descriptive Name   
description = does stuff.   
package = relatedName   
core = 7.x   

Inside myModule.module I have tried various things that are to much to elaborate , but include these exact functions and parameters.

Attempt 1:

function myModule_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {   
    if ($node->type == 'theNameOfACustomContentTypeICreated'){   
        foreach ($items as $delta => $item) {   
          //I am ver unclear at this point, and this function does not seem to be called when creating new content.   
        }   
    }   

Attempt 2:

myModule_nodeapi($node){   
    //this function does not seem to be called when creating new content.   
}   

I am at a loss. Neither function is even getting called - nor working.

Edit, the issue is resolved. The correct start was hook_jba_node_presave($node) , but I had syntax errors.

1

A couple of pointers:

  • Use only lower case for module names.
  • $node is not available in hook_field_presave(). $entity should contain necessary info.
  • hook_nodeapi() is not available in Drupal 7.
  • It's possible you can make these alterations in your theme's template.php file as well.

You can use the devel module to help with debugging, e.g. dpm($entity, '$entity contains').

Also see Module developer's guide and Drupal 7 API reference for more info.

1

Another way to achieve your goal of saving a value to a field without writing a custom module is to use the Computed Field module.

Computed Field is a very powerful CCK field module that lets you add a custom "computed fields" to your content types. These computed fields are populated with values that you define via PHP code. You may draw on anything available to Drupal, including other fields, the current user, database tables, you name it.

0

Check the module creating guide for module names (applies to the names of your functions, too).
Then try to go through the hooks for nodes - just enter "hook_node_" into the search box @ api.drupal.org.
I guess one of these could be useful to you :

good luck :).

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.