View source
<?php
include_once 'farm_fields.features.inc';
function farm_fields_farm_ui_entities() {
return array(
'taxonomy_term' => array(
'farm_log_categories' => array(
'label' => t('Log category'),
'label_plural' => t('Log categories'),
),
),
);
}
function farm_fields_farm_access_perms($role) {
$perms = array();
$perms[] = 'use text format farm_format';
return $perms;
}
function farm_fields_field_default_field_instances_alter(&$fields) {
foreach ($fields as &$field) {
if ($field['field_name'] == 'field_farm_images') {
$field['settings']['file_extensions'] = farm_fields_file_types('image');
}
elseif ($field['field_name'] == 'field_farm_files') {
$field['settings']['file_extensions'] = farm_fields_file_types('file');
}
}
}
function farm_fields_file_types($type = '', $separator = ' ') {
$file_types = array(
'image' => array(
'png',
'gif',
'jpg',
'jpeg',
),
'file' => array(
'csv',
'doc',
'docx',
'logz',
'odt',
'odp',
'ods',
'pdf',
'ppt',
'pptx',
'txt',
'xls',
'xlsx',
'kml',
'kmz',
'mp3',
'wav',
'ogg',
),
);
if ($type == 'image') {
$types = $file_types['image'];
}
elseif ($type == 'file') {
$types = $file_types['file'];
}
else {
$types = array_merge($file_types['image'], $file_types['file']);
}
return implode($separator, $types);
}
function farm_fields_prepopulate_entityreference(&$form, $entity_type, $field_name, $entity_ids) {
$form_entity_type = $form['#entity_type'];
$form_entity_bundle = $form['#bundle'];
$field_base = field_info_field($field_name);
$field_instance = field_info_instance($form_entity_type, $field_name, $form_entity_bundle);
$value_key = 'value';
switch ($field_base['type']) {
case 'entityreference':
$value_key = 'target_id';
break;
case 'taxonomy_term_reference':
$value_key = 'tid';
break;
}
$entities = entity_load($entity_type, $entity_ids);
$entity_ids = array();
foreach ($entities as $entity) {
$entity_ids[] = entity_id($entity_type, $entity);
}
if (empty($entity_ids)) {
return;
}
if (in_array($field_instance['widget']['type'], array(
'options_buttons',
'options_select',
))) {
if (empty($form[$field_name][LANGUAGE_NONE]['#default_value'])) {
$form[$field_name][LANGUAGE_NONE]['#default_value'] = $entity_ids;
}
}
elseif (in_array($field_instance['widget']['type'], array(
'entityreference_autocomplete',
'entityreference_autocomplete_tags',
))) {
$labels = array();
foreach ($entities as $id => $entity) {
$labels[] = entity_label($entity_type, $entity) . ' (' . $id . ')';
}
if ($field_instance['widget']['type'] == 'entityreference_autocomplete') {
foreach ($labels as $key => $label) {
if (!empty($form[$field_name][LANGUAGE_NONE][$key][$value_key]['#default_value'])) {
continue;
}
$form[$field_name][LANGUAGE_NONE][$key] = $form[$field_name][LANGUAGE_NONE][0];
$form[$field_name][LANGUAGE_NONE][$key][$value_key]['#default_value'] = $label;
$form[$field_name][LANGUAGE_NONE][$key][$value_key]['#delta'] = $key;
$form[$field_name][LANGUAGE_NONE][$key][$value_key]['#weight'] = $key;
if ($key > 0) {
$form[$field_name][LANGUAGE_NONE][$key][$value_key]['#required'] = 0;
}
$form[$field_name][LANGUAGE_NONE]['#max_delta'] = $key;
$form[$field_name][LANGUAGE_NONE][$key]['_weight']['#delta'] = $key;
$form[$field_name][LANGUAGE_NONE][$key]['_weight']['#default_value'] = $key;
}
}
elseif ($field_instance['widget']['type'] == 'entityreference_autocomplete_tags') {
if (empty($form[$field_name][LANGUAGE_NONE]['#default_value'])) {
$form[$field_name][LANGUAGE_NONE]['#default_value'] = htmlspecialchars(implode(', ', $labels));
}
}
}
elseif ($field_instance['widget']['type'] == 'entityreference_view_widget') {
$children = element_children($form[$field_name][LANGUAGE_NONE]);
foreach ($children as $child) {
if (!empty($form[$field_name][LANGUAGE_NONE][$child][$value_key]['#value'])) {
return;
}
}
foreach ($entities as $id => $entity) {
$form[$field_name][LANGUAGE_NONE][$id][$value_key] = array(
'#type' => 'checkbox',
'#return_value' => $id,
'#value' => $id,
'#title_display' => 'after',
'#attributes' => array(
'checked' => 'checked',
),
'#title' => check_plain(entity_label($entity_type, $entity)),
);
}
}
}