Page 1 of 1

[solved] ListIt2 - get options for field definition

Posted: Sun Nov 23, 2014 9:23 pm
by Cerulean
I'd like to be able to get the array of options that exist for a specific field definition (e.g. the list of options that are defined for a dropdown control).

I can do that fine if going via a ListIt2 item - I can load an item with ListIt2Loader or in a foreach loop and do:

Code: Select all

$item->fielddefs.myfield->GetOptions()
The problem is that this relies on an item being available. What if my foreach loop returns no items, or the item I try and load with ListIt2Loader has been deleted? I should be able to get the list of options for a field definition even if there are no items existing, because the options exist independent of any items. Does anyone know how to access the list of options without having to load an item?

Re: ListIt2 - get options for field definition

Posted: Tue Nov 25, 2014 9:33 pm
by psy
Try

Code: Select all

$item_obj = $mod->LoadItemByIdentifier('item_id', '')
This is the code used to add/edit an item. Before the item is saved there is no item_id so it returns an empty object. You can then use $item_obj to retrieve the field options.

Code: Select all

$item_obj->fielddefs.myfield->GetOptions()
This assumes the $mod is the same as the LI2 instance module of your template.
To retrieve the options from another LI2 instance, use a UDT, eg called 'get_options':

Code: Select all

$field_id=$params['field_id'];
$instance = cms_utils::get_module('myli2instance');
$item_obj=$instance->LoadItemByIdentifier('item_id', '');
$options=$item_obj->fielddefs[$field_id]->GetOptions();
$smarty->assign('options',$options);
and in your template {get_options field_id=7} (or whatever the field id number is). Then use $options in your template.

Re: ListIt2 - get options for field definition

Posted: Wed Nov 26, 2014 3:41 am
by Cerulean
That is brilliant!
Thanks.