You are here

README.txt in Computed Field 6

Same filename and directory in other branches
  1. 5 README.txt
  2. 7 README.txt
------------------The Computed Field Drupal Module----------------------------

Computed Field is a cck module which lets you add a computed field to custom
content types. You can choose whether to store your computed field in the
database. You can also choose whether to display the field, and how to format
it. The value of the field is set using php code, so it can draw on anything
available to drupal, including other fields, the current user, database
tables, etc. The drawback of this is of course that you need to know some php
to use it.

Computed Field requires the content module (cck).

-------------------------Update-------------------------------

As of 2006-8-11 the 'display format' setting has changed. You'll need to
update any existing computed fields: If your display format was 'This is the
value: %value', then change it to '$display = "This is the value: " .
$node_field_item['value'];'


-------------------------Usage--------------------------------

----------Getting Started-----------------------------------

Before you can use Computed Field, you'll need to get CCK and enable (at the
very least) the 'content' module. You will probably also want to enable the
other cck modules, such as 'text', 'number', 'date', etc.

To add a computed field to a content type, go to administer > content >
content types, select the content type you want to add to, and click on the
'add field' tab. One of the field types available should be 'Computed', and it
should have one bullet point under it, also labelled 'Computed'. If you select
this, give your field a name, and submit the form, you will get to the
configuration page for your new computed field.


--------Configuration---------------------------------------

A Computed Field can be configured with the usual cck field options, as well
as the following extra options:

Computed Code -- This is the code that will assign a value to your computed
field. It should be valid php without the <?php ?> tags.

Display Format -- This is also php code which should assign a string to the
$display variable. It has '$node_field_item['value']' available, which is the
value of the computed field. It also has '$field' available, and you can call
any drupal functions you want to display your field.

Store using the database settings below -- If this is checked then the field
is computed on node save and stored. If it isn't stored then it will be
recomputed every time you view a node containing this field.

Database Storage Settings
	Data Type -- This is the sql data type to use to store the field. Let us
	know if you need any other storage types, or if you would like an 'other'
	option :).

	Data Length (varchar/text) -- The length of the field in the database. For 
	storing usernames or other short text with a varchar field, 32 may be 
	appropriate. Only valid for varchar or text fields.
	
	Data Size (int/float) -- The size of the field stored in the database.
	Only valid for int or float fields.
	
	Data Precision (decimal) -- The total number of digits to store in the 
	database, including those to the right of the decimal. Only valid for 
	decimal fields. 
	
	Data Scale (decimal) -- The number of digits to the right of the decimal.
	Only valid for decimal fields. 

	Default Value -- Leave this blank if you don't want the database to store
	a default value if your computed field's value isn't set.

	Not NULL -- Leave unchecked if you want to allow NULL values in the
	database field.

	Sortable  -- Used in Views to allow sorting a column of this field.


--------Examples------------------------------------------

Here are some usage examples to get you started with Computed
Field. 

-----Make a node link to itself-----------------

This example isn't very useful, but it demonstrates how to get
hold of the nid.

In your computed field's configuration:

- Computed Code:
// store the nid in our computed field
$node_field[0]['value'] = $node->nid;

- Check 'Display this field'

- Display Format:
$display = l('A link to this node', 'node/'.$node_field_item['value']);

- Uncheck 'Store using the database settings below'. You could store this if
  you wanted to, but it's not costly to compute this field and is already
  stored in the node table. One reason why you may want to store it is if you
  want the value available to Views.

When you display a node of the content type containing this field it should
now have a link to itself.

-----Adding two other fields----------------------
Imagine you have two existing number fields, called field_product_price and
field_postage_price. You want to create a computed field field_total_cost
which adds these two fields. Create a new computed field with the name 'Total
Cost', and in your computed field's configuration set the following:

- Computed Code:
$node_field[0]['value'] =
$node->field_product_price[0]['value'] +
$node->field_postage_price[0]['value'];

- Check 'Display this field'

- Display Format:
$display = '$' . $node_field_item['value'];

- Check 'Store using the database settings below'

- Data Type: decimal

- Decimal Precision: 10

- Decimal Scale: 2

- Default Value: 0.00

- Check 'Not NULL'

- Check 'Sortable'


-----Calculating a Duration given a start and end time-----

This example uses KarenS' date module (http://drupal.org/project/date) to
create two date fields field_start_time and field_end_time which record hours
and minutes. We then create a new computed field to work out the duration as a
decimal number of hours (so 1.5 is 1hour, 30minutes).

Computed field settings:

- Computed Code:
$start = $node->field_start_time[0]['value'];
$end = $node->field_end_time[0]['value'];
$start_decimal = $start['hours'] + ($start['minutes'] / 60);
$end_decimal = $end['hours'] + ($end['minutes'] / 60);
$node_field[0]['value'] = $end_decimal - $start_decimal;

- Check 'Display this field'</li>

- Display Format:</b><code>
$display = $node_field_item['value'] . " hours";

- Check 'Store using the database settings below

- Data Type:</b> float

- Data Size:</b> normal

- Check 'Sortable'

Now if you set the start time field to 9am and the end time to 11:30am, your
computed field will store the value '2.5' and display '2.5 hours'.


-----Send more examples!---------------------------------

If you have another useful (or instructive) example send it to me
(http://drupal.org/user/59132/contact) and I'll add it here for the benefit of
humankind.

-----------------------About Computed Field-----------------------------------
Computed Field was created by Agileware (http://www.agileware.net).

File

README.txt
View source
  1. ------------------The Computed Field Drupal Module----------------------------
  2. Computed Field is a cck module which lets you add a computed field to custom
  3. content types. You can choose whether to store your computed field in the
  4. database. You can also choose whether to display the field, and how to format
  5. it. The value of the field is set using php code, so it can draw on anything
  6. available to drupal, including other fields, the current user, database
  7. tables, etc. The drawback of this is of course that you need to know some php
  8. to use it.
  9. Computed Field requires the content module (cck).
  10. -------------------------Update-------------------------------
  11. As of 2006-8-11 the 'display format' setting has changed. You'll need to
  12. update any existing computed fields: If your display format was 'This is the
  13. value: %value', then change it to '$display = "This is the value: " .
  14. $node_field_item['value'];'
  15. -------------------------Usage--------------------------------
  16. ----------Getting Started-----------------------------------
  17. Before you can use Computed Field, you'll need to get CCK and enable (at the
  18. very least) the 'content' module. You will probably also want to enable the
  19. other cck modules, such as 'text', 'number', 'date', etc.
  20. To add a computed field to a content type, go to administer > content >
  21. content types, select the content type you want to add to, and click on the
  22. 'add field' tab. One of the field types available should be 'Computed', and it
  23. should have one bullet point under it, also labelled 'Computed'. If you select
  24. this, give your field a name, and submit the form, you will get to the
  25. configuration page for your new computed field.
  26. --------Configuration---------------------------------------
  27. A Computed Field can be configured with the usual cck field options, as well
  28. as the following extra options:
  29. Computed Code -- This is the code that will assign a value to your computed
  30. field. It should be valid php without the tags.
  31. Display Format -- This is also php code which should assign a string to the
  32. $display variable. It has '$node_field_item['value']' available, which is the
  33. value of the computed field. It also has '$field' available, and you can call
  34. any drupal functions you want to display your field.
  35. Store using the database settings below -- If this is checked then the field
  36. is computed on node save and stored. If it isn't stored then it will be
  37. recomputed every time you view a node containing this field.
  38. Database Storage Settings
  39. Data Type -- This is the sql data type to use to store the field. Let us
  40. know if you need any other storage types, or if you would like an 'other'
  41. option :).
  42. Data Length (varchar/text) -- The length of the field in the database. For
  43. storing usernames or other short text with a varchar field, 32 may be
  44. appropriate. Only valid for varchar or text fields.
  45. Data Size (int/float) -- The size of the field stored in the database.
  46. Only valid for int or float fields.
  47. Data Precision (decimal) -- The total number of digits to store in the
  48. database, including those to the right of the decimal. Only valid for
  49. decimal fields.
  50. Data Scale (decimal) -- The number of digits to the right of the decimal.
  51. Only valid for decimal fields.
  52. Default Value -- Leave this blank if you don't want the database to store
  53. a default value if your computed field's value isn't set.
  54. Not NULL -- Leave unchecked if you want to allow NULL values in the
  55. database field.
  56. Sortable -- Used in Views to allow sorting a column of this field.
  57. --------Examples------------------------------------------
  58. Here are some usage examples to get you started with Computed
  59. Field.
  60. -----Make a node link to itself-----------------
  61. This example isn't very useful, but it demonstrates how to get
  62. hold of the nid.
  63. In your computed field's configuration:
  64. - Computed Code:
  65. // store the nid in our computed field
  66. $node_field[0]['value'] = $node->nid;
  67. - Check 'Display this field'
  68. - Display Format:
  69. $display = l('A link to this node', 'node/'.$node_field_item['value']);
  70. - Uncheck 'Store using the database settings below'. You could store this if
  71. you wanted to, but it's not costly to compute this field and is already
  72. stored in the node table. One reason why you may want to store it is if you
  73. want the value available to Views.
  74. When you display a node of the content type containing this field it should
  75. now have a link to itself.
  76. -----Adding two other fields----------------------
  77. Imagine you have two existing number fields, called field_product_price and
  78. field_postage_price. You want to create a computed field field_total_cost
  79. which adds these two fields. Create a new computed field with the name 'Total
  80. Cost', and in your computed field's configuration set the following:
  81. - Computed Code:
  82. $node_field[0]['value'] =
  83. $node->field_product_price[0]['value'] +
  84. $node->field_postage_price[0]['value'];
  85. - Check 'Display this field'
  86. - Display Format:
  87. $display = '$' . $node_field_item['value'];
  88. - Check 'Store using the database settings below'
  89. - Data Type: decimal
  90. - Decimal Precision: 10
  91. - Decimal Scale: 2
  92. - Default Value: 0.00
  93. - Check 'Not NULL'
  94. - Check 'Sortable'
  95. -----Calculating a Duration given a start and end time-----
  96. This example uses KarenS' date module (http://drupal.org/project/date) to
  97. create two date fields field_start_time and field_end_time which record hours
  98. and minutes. We then create a new computed field to work out the duration as a
  99. decimal number of hours (so 1.5 is 1hour, 30minutes).
  100. Computed field settings:
  101. - Computed Code:
  102. $start = $node->field_start_time[0]['value'];
  103. $end = $node->field_end_time[0]['value'];
  104. $start_decimal = $start['hours'] + ($start['minutes'] / 60);
  105. $end_decimal = $end['hours'] + ($end['minutes'] / 60);
  106. $node_field[0]['value'] = $end_decimal - $start_decimal;
  107. - Check 'Display this field'
  108. - Display Format:
  109. $display = $node_field_item['value'] . " hours";
  110. - Check 'Store using the database settings below
  111. - Data Type: float
  112. - Data Size: normal
  113. - Check 'Sortable'
  114. Now if you set the start time field to 9am and the end time to 11:30am, your
  115. computed field will store the value '2.5' and display '2.5 hours'.
  116. -----Send more examples!---------------------------------
  117. If you have another useful (or instructive) example send it to me
  118. (http://drupal.org/user/59132/contact) and I'll add it here for the benefit of
  119. humankind.
  120. -----------------------About Computed Field-----------------------------------
  121. Computed Field was created by Agileware (http://www.agileware.net).