Filling the field

I want to create a custom module and put the result of the following query into the field I created in the log record. how can I do this?

select name from taxonomy_term_data
where tid in(
select x.entity_id
from(
select point.entity_id,st_contains(polygon.geom,point.geom) sonuc
from (select ST_GEOMFROMTEXT(ST_AsText(ST_GeomFromWKB(field_farm_geofield_geom))) as geom,entity_id from field_data_field_farm_geofield where bundle=‘farm_areas’) point,
(select ST_GEOMFROMTEXT(ST_AsText(ST_GeomFromWKB(field_farm_geofield_geom))) as geom from field_data_field_farm_geofield
where entity_id=109) polygon) x
where sonuc=1);

I’m not exactly sure what the query does, but one example you might be able to learn from is the way in which farmOS automatically fills in the geometry field on Logs when they are saved.

This happens via hook_entity_presave() in the farm_movement module (in farmOS 1.x):

https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_entity_presave/7.x

Note that this function runs after the “Save” button is clicked on a log form (as well as whenever a log is saved via API or programatically), giving you the ability to perform logic at that time. So you can fill in a field with a value if you want (you may also want to be sure to confirm that the field does not already have a value, otherwise you will overwrite it).

As for running the actual query, there is good documentation on drupal.org: https://www.drupal.org/docs/7/api/database-api/database-api-overview

When we draw the polygon on the map in the geometry section, it takes the areas inside the polygon, I add those areas to the field_farm_area
Or I want to print it to the textfield area I just created.

Just found your other post - linking here so other people see the context for future reference: Get information from database

Ok so it sounds like you want these things to happen in the frontend UI while you are in the process of editing a log?

In that case, the approach I suggested above will not work, because that happens server-side after the “Save” button is clicked.

In order to do things like this client-side you will need JavaScript (and also maybe some Drupal Form API Ajax). This is definitely “advanced” customization - but it sounds like a really cool feature idea! :slight_smile:

Here are some resources that might help:

Hope that helps!

Where is the areas information in the log records written in the database?

Is this? field_data_field_farm_area

Yes, in farmOS 1.x, the “area reference” data is stored in field_data_field_farm_area, where the entity_type, bundle, and entity_id columns are used to reference the log, and the field_farm_area_tid column references the term ID of the area in the taxonomy_term_data table.

In farmOS 2.x, all of this is different, but the basic relationships are the same. Areas become Assets (not taxonomy terms), and logs have a “Location reference” field for referencing “location assets” (aka areas). In farmOS 2.x there is an asset database table (with all assets), a log table (with all logs), and a log__location table (which links the log to the location asset it references. Much shorter/clearer names overall, which is nice. :slight_smile:

(I know you are already invested in farmOS 1.x, but I do highly encourage you to start thinking about farmOS 2.x and planning how you will move to it when the first beta release is available. The more custom code you write for farmOS 1.x, the more you will need to re-write for farmOS 2.x. So it might be worth holding off on getting too deep right now, in the interest of saving yourself work in the long run. But I understand it’s a tough thing to do when you also need to use it for real data right now. I just need to say this so that you can think ahead.) :slight_smile:

1 Like

Well, like in the example, I have 2 fields and it creates 2 rows in the field_data_field_farm_area table. Why when I delete 1 row from the database, there are still 2 in this field. 1 of them are not deleted. I could not understand.

Try clearing your cache and see if it disappears in the form.

It’s not recommended to make changes directly to the database. Drupal manages that, and ensures that other related actions are taken when changes are made through the Drupal APIs (clearing field cache is just one example).

1 Like

Try clearing your cache

Specifically the Drupal cache (via /admin/config/development/performance), not your browser cache, to be clear.

Although browsers will also save form values so refreshing the page may not update it. Open the same form in a new tab to reload it fresh and see if that updates it too.

yes it is fixed thanks, but I have to do this every update process.