You are here

README.txt in Computed Field 5

Same filename and directory in other branches
  1. 6 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 this field -- Check this box to have this field appear on your node
view pages. You will usually want this unless you want your field to be a
hidden value.

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 -- This value will simply be passed on to sql. For storing up
	to 10 digit ints, enter 10. For storing currency as a float, use 10,2
	(unless you'll store larger than 10 figure amounts!).  For storing
	usernames or other short text with a varchar field, 64 may be appropriate.

	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:
// ensure the node has an id by saving it if it is new.
if (!$node->nid) node_save($node);
// 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: float

- Data Length: 10,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 Length:</b> 3,2

- 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 this field -- Check this box to have this field appear on your node
  32. view pages. You will usually want this unless you want your field to be a
  33. hidden value.
  34. Display Format -- This is also php code which should assign a string to the
  35. $display variable. It has '$node_field_item['value']' available, which is the
  36. value of the computed field. It also has '$field' available, and you can call
  37. any drupal functions you want to display your field.
  38. Store using the database settings below -- If this is checked then the field
  39. is computed on node save and stored. If it isn't stored then it will be
  40. recomputed every time you view a node containing this field.
  41. Database Storage Settings
  42. Data Type -- This is the sql data type to use to store the field. Let us
  43. know if you need any other storage types, or if you would like an 'other'
  44. option :).
  45. Data Length -- This value will simply be passed on to sql. For storing up
  46. to 10 digit ints, enter 10. For storing currency as a float, use 10,2
  47. (unless you'll store larger than 10 figure amounts!). For storing
  48. usernames or other short text with a varchar field, 64 may be appropriate.
  49. Default Value -- Leave this blank if you don't want the database to store
  50. a default value if your computed field's value isn't set.
  51. Not NULL -- Leave unchecked if you want to allow NULL values in the
  52. database field.
  53. Sortable -- Used in Views to allow sorting a column of this field.
  54. --------Examples------------------------------------------
  55. Here are some usage examples to get you started with Computed
  56. Field.
  57. -----Make a node link to itself-----------------
  58. This example isn't very useful, but it demonstrates how to get
  59. hold of the nid.
  60. In your computed field's configuration:
  61. - Computed Code:
  62. // ensure the node has an id by saving it if it is new.
  63. if (!$node->nid) node_save($node);
  64. // store the nid in our computed field
  65. $node_field[0]['value'] = $node->nid;
  66. - Check 'Display this field'
  67. - Display Format:
  68. $display = l('A link to this node', 'node/'.$node_field_item['value']);
  69. - Uncheck 'Store using the database settings below'. You could store this if
  70. you wanted to, but it's not costly to compute this field and is already
  71. stored in the node table. One reason why you may want to store it is if you
  72. want the value available to Views.
  73. When you display a node of the content type containing this field it should
  74. now have a link to itself.
  75. -----Adding two other fields----------------------
  76. Imagine you have two existing number fields, called field_product_price and
  77. field_postage_price. You want to create a computed field field_total_cost
  78. which adds these two fields. Create a new computed field with the name 'Total
  79. Cost', and in your computed field's configuration set the following:
  80. - Computed Code:
  81. $node_field[0]['value'] =
  82. $node->field_product_price[0]['value'] +
  83. $node->field_postage_price[0]['value'];
  84. - Check 'Display this field'
  85. - Display Format:
  86. $display = '$' . $node_field_item['value'];
  87. - Check 'Store using the database settings below'
  88. - Data Type: float
  89. - Data Length: 10,2
  90. - Default Value: 0.00
  91. - Check 'Not NULL'
  92. - Check 'Sortable'
  93. -----Calculating a Duration given a start and end time-----
  94. This example uses KarenS' date module (http://drupal.org/project/date) to
  95. create two date fields field_start_time and field_end_time which record hours
  96. and minutes. We then create a new computed field to work out the duration as a
  97. decimal number of hours (so 1.5 is 1hour, 30minutes).
  98. Computed field settings:
  99. - Computed Code:
  100. $start = $node->field_start_time[0]['value'];
  101. $end = $node->field_end_time[0]['value'];
  102. $start_decimal = $start['hours'] + ($start['minutes'] / 60);
  103. $end_decimal = $end['hours'] + ($end['minutes'] / 60);
  104. $node_field[0]['value'] = $end_decimal - $start_decimal;
  105. - Check 'Display this field'
  106. - Display Format:
  107. $display = $node_field_item['value'] . " hours";
  108. - Check 'Store using the database settings below
  109. - Data Type: float
  110. - Data Length: 3,2
  111. - Check 'Sortable'
  112. Now if you set the start time field to 9am and the end time to 11:30am, your
  113. computed field will store the value '2.5' and display '2.5 hours'.
  114. -----Send more examples!---------------------------------
  115. If you have another useful (or instructive) example send it to me
  116. (http://drupal.org/user/59132/contact) and I'll add it here for the benefit of
  117. humankind.
  118. -----------------------About Computed Field-----------------------------------
  119. Computed Field was created by Agileware (http://www.agileware.net).