Search code examples
drupaldrupal-7drupal-modulesdrupal-rules

Dynamically creating field using Drupal


Okay lets say I have a basic demographic content type (Client) that I am saving as a node.

I need to make a key for an external database that we occasionally need to work with.

I want Drupal to take the fragments of three fields being collected in the Client content type and join them into a single field to be saved within the Client content type. Ideally this should be done when I create a new (Client) node.

The new field should contain the following First 3 letters of firstname First 3 letters of lastname and last 4 digits of Social Security Number.

For example
firstname: John
lastname: Doe
SSN: 123-45-6789

A new field should be created, let’s call it fk_database. Using the example above the field >created would be -> johdoe6789

Can this be done via Rules module, or should I approach this in a different way? If I need to do this via a PHP script where exactly should that be added in the Drupal structure?


Solution

  • The easiest way would probably be to use the Computed Field module, it's pretty much built for exactly what you want to do:

    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.

    If you do need to do it in code though, have a look into hook_entity_presave() which would probably be the best place to run your code from.