Say I know the name of a custom field (either kind):
custom_345 (profiles)
custom_story_overline (config)
Is there a way to look up or determine its defined type?(text, textarea, radio, checklist, select, number, date, datetime, email, phone number, url, etc)
Context: “Style Checker”. I only want to apply changes to ‘text’ and ‘textarea’ custom fields.
I’m reading $_LW->save_data, but the values available are given as either arrays (‘checkboxes’ and ‘datetime’) or strings (everything else). Thus, for instance, I cannot currently tell a ‘text’ from a ‘radio’ field, and I need to exclude the latter given it shouldn’t have its value changed mid-save.
I’m hoping to have the option to have the script check custom fields automatically (when a setting is enabled), rather than needing to define each field to check manually. The ability to find a custom field’s original type to compare with seems the best option, if it exists…
With help from LiveWhale support, here’s what I have to get the types of both config and profile custom fields.
protected function createCustomFieldLookup($data_type) {
global $_LW;
$custom_field_type_lookup = NULL;
// CONFIG CUSTOM FIELDS
// For custom fields defined in the config files, for any type.
// Short-term caching: universal values that rarely change, but for editors rather than users.
$key = 'style_checker_config_custom_fields';
$custom_field_type_lookup = $_LW->getVariable($key);
// Rebuild cache if empty.
if (empty($custom_field_type_lookup)) {
// Build content.
if (isset($_LW->CONFIG['CUSTOM_FIELDS'])) {
foreach ($_LW->CONFIG['CUSTOM_FIELDS'] as $scope => $fields ) {
foreach ($fields as $settings ) {
if (isset($settings['name']) && isset($settings['type'])) {
if (!isset($custom_field_type_lookup['custom_'.$settings['name']])) { // if a new field
$custom_field_type_lookup[$settings['name']] = $settings['type']; // add name/type to lookup
}
else { // we have two fields with the same name (bad configuration)
$_LW->logDebug('[Custom field "'.$settings['name'].'" is repeated in your definitions.] Consider giving each field a unique name.');
$custom_field_type_lookup['custom_'.$settings['name']] = 'unknown'; // disable changes for both
}
}
}
}
}
// Assign to cache.
if (!empty($custom_field_type_lookup)) {
$_LW->setVariable($key, $custom_field_type_lookup, 600); // 10 minutes
} // else save nothing; try again later
}
// PROFILES CUSTOM FIELDS
// If we are working with a profile, then we'll also add the type's custom fields to the lookup.
// Given code limits itself to only the current type, no caching is used.
if ($data_type=='profiles') {
// Breaking out query terms for human readability.
$action = 'select';
$object = 'livewhale_profiles_types_fields.id, livewhale_profiles_types_fields.type'; // only the fields we need
$from = 'livewhale_profiles_types_fields';
$where = 'livewhale_profiles_types_fields.pid='.(int)$_LW->save_data['tid']; // only fields for this type
// run query, get array, add each field to lookup
foreach ($_LW->dbo->query($action, $object, $from, $where)->run() as $field) {
if (isset($field['id']) && isset($field['type'])) {
$custom_field_type_lookup['custom_'.$field['id']] = $field['type'];
}
}
}
// Make the lookup available for this edit/save.
$_LW->REGISTERED_APPS['custom_field_type_lookup'] = $custom_field_type_lookup;
}
The above makes an array like this:
[completed_training] => [checkbox]
[training_date] => [text]
[statistics_text] => [text]
[statistics_link_text] => [text]
[statistics_link_url] => [text]
[video_id] => [unknown] // example of duplicate name in config
...
[is_staff_counselor] => [checkbox]
[territories_interests] => [textarea]
[text_number] => [text]
[goal_text] => [textarea]
[custom_331] => [text] // this and following are specific to a profile type
[custom_332] => [textarea]
[custom_333] => [textarea_long]
[custom_334] => [number]
[custom_335] => [radio_button]
[custom_336] => [checkbox]
[custom_337] => [select_menu]
[custom_338] => [date]
[custom_339] => [date_time]
[custom_340] => [email]
[custom_341] => [phone]
[custom_342] => [url]