function farm_log_prepopulate_log_form_references in farmOS 7
Helper function for populating entity reference fields in log forms.
Parameters
array $form: The form array to modify, passed by reference.
1 call to farm_log_prepopulate_log_form_references()
- farm_log_form_log_form_alter in modules/
farm/ farm_log/ farm_log.module - Implements hook_form_FORM_ID_alter().
File
- modules/
farm/ farm_log/ farm_log.module, line 521 - Code for the Farm Log feature.
Code
function farm_log_prepopulate_log_form_references(&$form) {
// Get the log type from the form. Bail if none.
if (empty($form['#bundle'])) {
return;
}
$log_type = $form['#bundle'];
// Ask modules for information about fields that should be prepopulated for
// this log type.
$fields = module_invoke_all('farm_log_prepopulate_reference_fields', $log_type);
// Allow modules to alter the field information.
drupal_alter('farm_log_prepopulate_reference_fields', $fields, $log_type);
// Populate the fields.
foreach ($fields as $field => $info) {
// Start with an empty array of IDs.
$ids = array();
// If the field does not exist on the log, skip it.
if (!isset($form[$field])) {
continue;
}
// If a URL param is available, get a list of entity IDs from it.
if (!empty($info['url_param'])) {
// Get query parameters.
$params = drupal_get_query_parameters();
// If the URL param is set, pull the IDs out.
if (!empty($params[$info['url_param']])) {
$ids = $params[$info['url_param']];
}
}
elseif ($info['entity_type'] == 'user') {
global $user;
if (!empty($user->uid)) {
$ids[] = $user->uid;
}
}
// Ensure that the IDs are an array.
if (!is_array($ids)) {
$ids = array(
$ids,
);
}
// Allow modules to add log categories.
if ($field == 'field_farm_log_category' && !empty($form['#entity'])) {
$categories = module_invoke_all('farm_log_categories_populate', $form['#entity']);
if (!empty($categories)) {
foreach ($categories as $category) {
if (is_string($category)) {
$ids[] = t($category);
}
}
}
}
// If there are no IDs, skip.
if (empty($ids)) {
continue;
}
// Look up taxonomy term IDs, if necessary.
if ($info['entity_type'] == 'taxonomy_term' && !empty($info['lookup']) && !empty($info['vocabulary'])) {
$term_names = $ids;
$ids = array();
foreach ($term_names as $name) {
$term = farm_term($name, $info['vocabulary'], FALSE);
if (!empty($term->tid)) {
$ids[] = $term->tid;
}
}
}
// Prepopulate with the farm_fields helper function.
farm_fields_prepopulate_entityreference($form, $info['entity_type'], $field, $ids);
}
}