Skip to content

Commit

Permalink
Issue #3136210: Replace 'default_value' with 'data_schema' in farm.js…
Browse files Browse the repository at this point in the history
…on resource fields info
  • Loading branch information
mstenta committed May 13, 2020
1 parent 6b05852 commit 1916fd8
Showing 1 changed file with 43 additions and 1 deletion.
44 changes: 43 additions & 1 deletion modules/farm/farm_ui/farm_ui.module
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,33 @@ function farm_ui_farm_info() {
// Load information about farmOS entities.
$entities = farm_ui_entities();

// Define default data schemas by field type.
// We hard-code this information because it's relatively stable, and easier
// than trying to figure out what the RestWS module is going to produce for
// each field type. This information is useful to clients like Field Kit.
$field_type_schemas = array(
'list_text' => '',
'file' => '{"id": null}',
'geofield' => '{"geom": ""}',
'image' => '{"id": null}',
'taxonomy_term_reference' => '{"id": null}',
'number_integer' => 'null',
'datestamp' => '""',
'text_long' => '""',
'entityreference' => '{"id": null}',
'text' => '""',
'field_collection' => 'null', // See $field_collection_schemas logic below.
'list_boolean' => 'null',
'fraction' => 'null',
);
$field_collection_schemas = array(
'field_farm_movement' => '{"area": [{"id": null}], "geometry": ""}',
'field_farm_membership' => '{"group": [{"id": null}]}',
'field_farm_quantity' => '{"measure": "", "value": null, "unit": {"id": null}, "label": ""}',
'field_farm_inventory' => '{"asset": {"id": null}, "value": null}',
'field_farm_animal_tag' => '{"id": "", "location": "", "type": ""}',
);

// Add information about entity type bundles.
$info = array();
foreach ($entities as $entity_type => $bundles) {
Expand All @@ -58,7 +85,7 @@ function farm_ui_farm_info() {
}

// Load the fields on this bundle and some basic information about them:
// label, required, type, and default value.
// label, required, type, and data schema information.
/**
* @todo
* This is removing the `field_farm_` prefix, so that it is consistent
Expand All @@ -72,9 +99,24 @@ function farm_ui_farm_info() {
'type' => $field['type'],
'required' => $field_instance['required'],
);
/**
* @deprecated
* The 'default_value' is not reliable, and will be removed from farmOS
* in a future release.
*/
if (array_key_exists('default_value', $field_instance)) {
$field_info['default_value'] = $field_instance['default_value'];
}
if (array_key_exists($field['type'], $field_type_schemas)) {
$data_schema = $field_type_schemas[$field['type']];
if ($field['type'] == 'field_collection' && array_key_exists($field_instance['field_name'], $field_collection_schemas)) {
$data_schema = $field_collection_schemas[$field_instance['field_name']];
}
if (isset($field['cardinality']) && $field['cardinality'] != 1) {
$data_schema = '[' . $data_schema . ']';
}
$field_info['data_schema'] = drupal_json_decode($data_schema);
}
$field_name = str_replace('field_farm_', '', $field_instance['field_name']);
$info['resources'][$entity_type][$bundle]['fields'][$field_name] = $field_info;
}
Expand Down

0 comments on commit 1916fd8

Please sign in to comment.