439
edits
| Line 122: | Line 122: | ||
$extra->category->subcategory->prop = true; // we can set recursive properties, there's not any limit to own deep the hierarchy can be | $extra->category->subcategory->prop = true; // we can set recursive properties, there's not any limit to own deep the hierarchy can be | ||
require_once( | require_once(DOL_DOCUMENT_ROOT.'/customfields/class/customfields.class.php'); | ||
$customfields = new CustomField($db, 'currentmodule'); | |||
$fieldid = 'mycustomfields'; // $fieldid can either be a column_name or an ordinal_position from information_schema.columns | $fieldid = 'mycustomfields'; // $fieldid can either be a column_name or an ordinal_position from information_schema.columns | ||
$result = $customfields->setExtra($fieldid, $extra); // store the extra options inside the database | $result = $customfields->setExtra($fieldid, $extra); // store the extra options inside the database | ||
</source> | |||
You can also use addCustomField() or updateCustomField() with an $extra object, and these functions will also call the setExtra() method. | |||
'''Note''': the extra options you set are '''appended''' to the one already stored in the database. However, you can overwrite them, just use the same name of property but with a different value. | |||
=== Fetching extra options === | |||
Extra options are automatically fetched along with a custom field structure, decoded, and stored inside the extra sub-property of any field object, or CustomFields object. | |||
When fetched, extra options are stored as a sub-object for the current field object (which itself is stored as a subobject of the CustomFields object if caching is enabled). | |||
Eg: | |||
<source lang="php"> | |||
require_once(DOL_DOCUMENT_ROOT.'/customfields/class/customfields.class.php'); | |||
$customfields = new CustomField($db, 'currentmodule'); | |||
$fields = fetchAllFieldsStruct(); // fetching fields structure will automatically fetch extra options at the same time. This works with fetchAllFieldsStruct() and fetchFieldStruct() | |||
// Now that you fetched the fields structure, you can just access the extra options sub-properties: | |||
print_r($fields->mycustomfield->extra); // will print all extra options for the field mycustomfield | |||
print_r($customfields->fields->mycustomfield->extra); // you can also access the same field(s) directly from the CustomFields object if caching is enabled (default behaviour) | |||
</source> | |||
=== Modifying extra options === | |||
You can just combine the two previous points to edit extra options of a custom field: just load a custom field, edit on-the-fly the extra property with PHP code, and then store it back directly into the database: | |||
<source lang="php"> | |||
require_once(DOL_DOCUMENT_ROOT.'/customfields/class/customfields.class.php'); | |||
$customfields = new CustomField($db, 'currentmodule'); | |||
$fields = fetchAllFieldsStruct(); | |||
$fields->mycustomfield->extra->newproperty = 'This is a new property'; // add a new property over the old extra options sub-object | |||
$customfields->setExtra('mycustomfield', $fields->mycustomfield->extra); // store the modified extra options back into the database for future usage | |||
</source> | </source> | ||
edits