I am writing a module which adds a new field. Similar to the serial module, the field cannot be changed or filled anyhow when creating a new node.
I just want the field to create a different output according to certain conditions, for instance if the user has a certain role or not.

For instance, when a user visits a node of a content type which has this field enabled, check for php conditions and then output different html.
Is the user a guest or registered? If guest, output a link which says "register to do this".

It would be really cool if this would then automatically work with views? Like a view which outputs this field for every node of that content type.

Ive tried using computed field but this module computed the result once and then displays this all the time... only solution would to alter the cache db, which seems a little unsmooth to me.

I already added a field using the examples module, so this is what I got so far:

function proposal_field_field_info() {
  return array(
    // We name our field as the associative name of the array.
    'proposal_field' => array(
      'label' => t('Proposal Field'),
      'description' => t('Adds proposal feature'),
      'default_widget' => 'proposal',
      'default_formatter' => 'proposal_field_simple_text',
  • why dont you add the code required code in node.tpl instead? Jan 18 '13 at 13:18
  • mohammed :) I want to use it with views and just want to have it in a module
    – Alex
    Jan 18 '13 at 13:19
  • You could use the views php module to write the required code. But that is not really advisable as there would be issue for caching and performance. Jan 18 '13 at 13:21
  • Computed Field has an option to compute on read. Just turn it on for your fields.
    – Mołot
    Jun 20 '13 at 13:25

Computed Field seems to meet your needs.

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. (Feeling the power yet? :) ) You can also choose whether to store your computed field values in the database with other content fields, or have them "calculated" on the fly during node views.

Emphasis mine. Either require it in your module and use it, or use it's code, it is open source after all.

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.