Is it possible to use custom fields and filtering in asset views and filter by a range of birth dates?

Hello,
I have recently discovered farmOS and have been having a go at setting it up for our farm (2.0.0-beta2). It definitely beats digging through everyone’s diaries looking for something :).

Some of the animals and equipment on the property are owned by neighbours and I would like to keep a record of who owns what and where it is. To do this, I have made a user account for each person (even if they never log in) and added a custom field containing a reference to a user as the owner for the animal and equipment assets. This seems to work well, however I can’t work out how (or not sure if it is currently possible or requires modifying the php code) to make this field appear in the list of all animal or equipment assets. i.e. Show and allow filtering by the owner in the /assets/animal page. Being able to filter by a custom field would also be handy - for example finding all animals belonging to someone.

Whilst on the topic of views for assets, it appears farmOS v1 had support for filtering by a range of birth dates . Does farmOS v2 have this or something similar or is this still a todo?

Thanks everyone for your efforts in writing what is some very useful software.

1 Like

Hi @jgOhYeah - welcome to the farmOS forum! :slight_smile:

To do this, I have made a user account for each person (even if they never log in) and added a custom field containing a reference to a user as the owner for the animal and equipment assets.

Sounds like you’re digging right in! Great!

This seems to work well, however I can’t work out how (or not sure if it is currently possible or requires modifying the php code) to make this field appear in the list of all animal or equipment assets.

I assume you did this by enabling the Field UI module and creating a new field via “Administration > Structure > Asset types”? This is one way to add fields to asset/log types. It is very flexible, but has some limitations/considerations (which you have discovered).

Fields can also be added via a custom module: Fields | farmOS

The benefit of this approach (custom module) is that farmOS will automatically add these as columns+filters to the default Views configurations (eg: /assets/animals). We have some “smart” logic to wire all this up for you. The intention was to make exactly your use-case easier. :slight_smile:

Adding fields created via Field UI to these Views is also possible, but requires overriding more configuration, and in general I would not recommend it. Creating a custom module is easy (explained here) and makes it easier to maintain your customizations moving forward (eg: via Git).

Also, for reference, we have an old feature request open for “Asset ownership” here: https://www.drupal.org/project/farm/issues/2400065 - feel free to share your notes/experimentation details there (or open a dedicated forum topic and we can cross-link them) - maybe we can move towards making this an official feature of farmOS!

Whilst on the topic of views for assets, it appears farmOS v1 had support for filtering by a range of birth dates . Does farmOS v2 have this or something similar or is this still a todo?

This came up in another topic: Recording Cow Service Date & Bull Details - #5 by mstenta

If you (or anyone) is interested in undertaking this, I’d be happy to review a pull request and/or point in the right direction… just needs some dev time. :slight_smile:

Thanks for the pointers.
I was using the Field UI module. In the end I made a custom module for ownership based off the Fields | farmOS bundle documentation you linked. As a beginner with drupal, it may be useful to put a note on there at some point to say they given code should be pasted in the [modulename].module file, although I managed to skip over reading this in the main modules page, so it will probably be more obvious to others :).

For anyone attempting to do similar things, it may also be useful to look at the soure code for the field definitions to get the available options and data types. If it would be helpful, I can have a go at submitting a pull request to add this to the documentation.

I will have more of a look at the feature request and date range when I have time and are a little more familiar with the code base / structure.

Thanks again for your help.

2 Likes

If it would be helpful, I can have a go at submitting a pull request to add this to the documentation.

Great ideas @jgOhYeah! Please feel free to open a PR!

You probably already found them, but if not the relevant files on GitHub are here: https://github.com/farmOS/farmOS/tree/2.x/docs/development/module

@jgOhYeah Hey! Just got off the weekly farmOS dev call and we discussed the “Asset Ownership” issue a bit.

Have you created a module that adds an Owner field to assets already? One of the things we discussed on the call was adding that field in farmOS core, in the same module that adds it to Logs: farmOS/farm_owner.module at eb6f1043da00b7b21d122d3cba626c4d026371c3 · farmOS/farmOS · GitHub

It would be easy enough for farmOS core to provide this field. But I realized this could create a conflict with your custom module in the future if you create a field with the same name.

Would you like to open a PR to add the field to farmOS core in that module, instead of doing it in your own module?

We discussed a few of the considerations around “asset ownership” more generally on the call as well… I will put a few notes in the issue: Asset ownership [#2400065] | Drupal.org

Hello,
I have just submitted a pull request for adding to the fields documentation (Improving fields documentation by jgOhYeah · Pull Request #505 · farmOS/farmOS · GitHub).

I did make a custom module, but haven’t gotten around to publishing it (implementation is almost exactly the same as the logs ownership, with label being 'Owner(s)' and description being 'Who owns this asset.'). It’s field name is farm_owners, so hopefully shouldn’t cause too many conflicts. Entering stuff in from scratch wouldn’t take long, especially if there is a batch assign feature added like there currently is for the logs.

I am happy to create a pull request to add it in, probably following direction 1 in your notes (making it an assignment to, keeping the same wording as the logs rather than ownership of). A legal_ownership field could be added later or as a custom module (potentially this module would handle lease agreements as well) if needed, although direction 1 would be good enough for us.

1 Like

Hello,
I’ve got a development environment set up and am working in this branch. I have changed the if ($entity_type->id() == 'log') { line in farm_owner.module to if ($entity_type->id() == 'log' || $entity_type->id() == 'asset') { and and reinstalled farmOS, While assets now have an owner field, this does not show up in any of the views of assets. Is there something else that needs to be done for the field to show up or am I doing something wrong?

I had a similar experience with writing my own module, but found using a bundle field rather than a base field allowed ithe field to appear in views for each asset type and temporarily used that instead.

Thanks.

1 Like

Yes, so when adding a new “base field” like this, we will also need to add it to the default farm_asset View provided by the farm_ui_views module: https://github.com/farmOS/farmOS/blob/2.x/modules/core/ui/views/config/install/views.view.farm_asset.yml

The “right” way to do that is to edit the View through the Drupal UI (enable the views_ui module and go to /admin/structure/views/view/farm_asset), then export the YML and commit that (enable the “Configuration Manager” module and go to /admin/config/development/configuration/single/export/view/farm_asset to get the YML).

Be sure to stick to the same patterns/conventions when you add the new column via the Views UI (look to other asset reference fields for an example), and be thoughtful about where that field is organized among the columns - maybe after the “Parents” column?

1 Like

Thanks for the instructions. I have gotten that part working.

I think the next step is to get the bulk assign owners working like in the logs. I have had a look at the code and am thinking that the plugin in the owner module would need to be restructured a bit to be more like that in the flag module to cope with multiple entity types. Is this the case or is there a simpler method that I am missing? I have had a small attempt at the rearranging on my local copy, but seem to have created a moderate mess and need to go and do a bit more reading up how it is meant to work before trying again :). If this is painfully obvious or this feature is needed in a hurry, anybody feel free to have a go. Otherwise, I can keep having a look when I have a chance.

1 Like

I agree that’s probably the logical next step.

Note that there are three parts to the “Assign log” action…

  1. The action plugin: https://github.com/farmOS/farmOS/blob/2.x/modules/core/owner/src/Plugin/Action/LogAssign.php
  2. The action form: https://github.com/farmOS/farmOS/blob/2.x/modules/core/owner/src/Form/LogAssignActionForm.php
  3. The routing configuration, which creates the /log/assign path and loads the LogAssignActionForm: https://github.com/farmOS/farmOS/blob/2.x/modules/core/owner/farm_owner.routing.yml

It may be possible to generalize LogAssignActionForm.php to AssignActionForm.php, such that it can be used for both. This looks to be the most important line, where it specifically loads the log entity type definition: https://github.com/farmOS/farmOS/blob/d2b512725ea2659bec10fbabf53fa60a9c3ddbe0/modules/core/owner/src/Form/LogAssignActionForm.php#L140

The routing config can be refactored to provide two paths: /log/assign and /asset/assign, and provide an additional argument to the LogAssignActionForm to define the entity type (asset or log). That should take care of the form.

As for src/Plugin/Action/LogAssign.php - I would probably create a new AssignBase.php class, which that extends from. Then create another AssetAssign.php action plugin class that also extends from it. The most important thing that needs to differ in each is this: https://github.com/farmOS/farmOS/blob/d2b512725ea2659bec10fbabf53fa60a9c3ddbe0/modules/core/owner/src/Plugin/Action/LogAssign.php#L17

@jgOhYeah Another thought just came to mind… In the /people page, we have a column “Assigned logs”. It probably makes sense for there to be another column for “Assigned assets” alongside this new asset owner field.

Thanks for the instructions and hints. I thought of this too and had a go at making this work using the views ui and copying the settings from the assigned logs button, although I forgot I had done it to mention it :slight_smile: .

A couple of screenshots of it currently:


2 Likes

@jgOhYeah: Very nice!!

Hello again,
Apologies it has taken so long, but I think I have got everything working for adding assign users to assets… when installing from scratch. This is pretty much along the lines of the changes you suggested.

I have attempted to write an update hook to add the changes required. The update.php file crashes, but seems to install the updates ok with drush (drush updatedb). Everything then works except that the bulk assign assets option doesn’t appear.

I have been simulating updating by starting from beta 4 and entering some example data, before checking out the branch I have been working in. The update . I also tried to update from beta 2 to this branch on our production system with the same results.

The error (useful part) from running update.php:

[Mon Apr 25 01:17:38.167000 2022] [php7:notice] [pid 14117] [client 192.168.0.186:60250] Uncaught PHP Exception Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorageException: "Table information not available for the 'owner' field." at /var/www/html/farmOS/web/core/lib/Drupal/Core/Entity/Sql/DefaultTableMapping.php line 391

Does anyone know or can give hints on what I am doing wrong or what I should be doing to get this working? Thanks for your help.

1 Like

The update.php file crashes, but seems to install the updates ok with drush ( drush updatedb ).

Hmm that’s suspicious. Can you check /admin/reports/dblog to see if there are any error messages or clues about the error/crash?

Edit: oops I should have finished reading your comment. :slight_smile:

It sounds like the field is not being created in the database. Hmm…

Everything then works except that the bulk assign assets option doesn’t appear.

Ah yea there’s another small thing that needs to be done for this, I think. You need to create a config entity for the action in order to enable it.

Here is the equivalent config for the existing “Assign” action for logs: https://github.com/farmOS/farmOS/blob/2.x/modules/core/owner/config/optional/system.action.log_assign_action.yml

That config entity gets created automatically when the module is installed. But for existing installations you will need to create it via your update hook.

I think it would look like this (untested):

use Drupal\system\Entity\Action;

...
/**
 * Add 'owner' field to assets.
 */
function farm_owner_update_9001() {

  // ... your other code

  // Create action for assigning assets to users.
  $action = Action::create([
    'id' => 'asset_assign_action',
    'label' => t('Assign asset'),
    'type' => 'asset',
    'plugin' => 'asset_assign_action',
    'configuration' => [],
  ]);
  $action->save();
}
1 Like

Thanks. That makes sense. I will have more of a look tomorrow.

1 Like

It sounds like the field is not being created in the database. Hmm…

I’m a bit stumped on this… without testing directly myself. It looks like what you’re doing is correct.

Did I share that example update hook code with you or did you find it somewhere? Can’t remember… :slight_smile:

I’m looking at a recent example where I did this myself, and I actually used hook_post_update_NAME() instead of hook_update_N()

https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Extension!module.api.php/function/hook_post_update_NAME

Example: https://github.com/mstenta/farm_surveystack/blob/61b85d7e237bc5971919ac7c44c5c29849737e94/farm_surveystack.post_update.php#L35

Post-update hooks run after other update hooks, and have a few other important differences (see doc link above for more info). Notably, they have a full Drupal bootstrap available to them, whereas some things are not possible in hook_update_N(). So maybe creating field definitions in this way (perhaps also specifically using the farm_field.factory service like this) is not possible in hook_update_N().

In either case, I’m starting to use post update hooks myself more often, so it would probably be best to move to that anyway. Let’s see if that solves this…

1 Like

Sorry it has taken a while to get back - had a few assignments / tasks needing doing.

The update code was based off the Drupal examples and documentation. I can’t find the exact page, but this example is pretty similar to what I based it off.

I pasted this into the update hook and the assign action does appear and work for assets when running through drush.

Ta for linking that - make a bit more sense of what to aim for. farm_owner.install has been updated to be fairly similar in structure to the example you gave and renamed to farm_owner.post_update.php to match just in case. This works with drush, but still no luck with update.php.

Issue potentially found

I was about to add a few observations and press send, but then I happened to come across what I think is related or is the cause.

Line 17 in farm_owner.module causes the crash if || $entity_type->id() == 'asset' is present. If commented out so that the line is just if ($entity_type->id() == 'log') {, update.php is happy, although the owner field isn’t added to assets. Do you know of a workaround for this or is this likely a wild goose chase and the issue lies elsewhere? Thanks.

1 Like

Huh - that is strange.

One thing I was going to suggest (unrelated to that bug, but for the same line of code) is to change it to:

if (in_array($entity_type->id(), ['asset', 'log'])) {

I don’t know why the original code is causing any issue, though, so I’m not sure that would fix it. Still… maybe worth a try?