I have built a form using the field API. The function hook_field_widget_form gets called for every field. I have a bit of code that populates a select box but it is being called for every field in the form - see the first 10 lines of the code below.
function mango_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$library = libraries_detect('d3_custom_scripts');
if($library) {
$libs = libraries_get_libraries();
$custom_scripts_path = $libs['d3_custom_scripts'];
$files = file_scan_directory($custom_scripts_path, '/.*\.js$/');
$custom_script_list[''] = t('Please select');
foreach ($files as $file => $info) {
$custom_script_list[$info->filename] = $info->filename;
}
}
dpm($items[$delta]);
$item =& $items[$delta];
$element += array(
'#type' => 'fieldset',
);
$element['enabled'] = array(
'#title' => t('Enabled'),
'#type' => 'checkbox',
'#default_value' => isset($item['enabled']) ? $item['enabled'] : '',
);
$element['custom_script'] = array(
'#title' => t('Attach script'),
'#type' => 'select',
'#options' => $custom_script_list,
'#default_value' => isset($item['custom_script']) ? $item['custom_script'] : '',
);
return $element;
}
Is there a ay to only run the code once and pass in the variables into the hook_field_widget_form function so I can avoid calling the same code over and over when I only really need to call it once? Many thanks.