Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an Organization entity type with a Farm bundle #849

Draft
wants to merge 29 commits into
base: 3.x
Choose a base branch
from

Conversation

mstenta
Copy link
Member

@mstenta mstenta commented May 29, 2024

For discussion see: https://farmos.discourse.group/t/organization-level-data/2003

This adds a new "Organization" (organization) entity type to farmOS for storing organization-level data. Organization entities have the following base-fields (similar to plan entities, with an additional user field):

  • Data
  • Notes
  • Files
  • Images
  • Users

The "Users" (user) field allows users a group to reference one or more user accounts that are associated with it. This does not serve any purpose other than informational at this point, but could be used in the future for building additional UX and/or access control features on top of.

An initial "Farm" (farm) bundle of the Organization entity is provided as a way of representing "farm organizations". Farm organizations do not have any bundle-specific fields on them at this point, but new ones could be considered in the future. Contrib modules will also be able to add their own for specific purposes.

A farm organization reference base field is added to all assets for associating them with a farm organization entity.

This is still just a draft to get things started. The initial data architecture is ready for review, and some minimal UI elements are added. More UI/UX is needed to make this usable from the frontend. Potential next steps (for this PR):

  • Add a tab to view all assets in a farm.
  • Add "Farm" column to asset Views.
  • Add support for filtering assets by farm.
  • Allow setting farm via asset CSV import.
  • Include farm in asset CSV exports.
  • Add a bulk action for assigning assets to a farm.
  • Add support for filtering location hierarchy by farm.
  • ...?

We are also discussing adding a telephone and address field to organization entities (or just farm bundles). That is not done yet in this PR (pending community feedback in the forum discussion linked above).

@paul121
Copy link
Member

paul121 commented Jul 11, 2024

Added a few commits in my branch: https://github.com/paul121/farmOS/tree/3.x-organization

@paul121
Copy link
Member

paul121 commented Jul 11, 2024

Added an "Assets" view + tab in my branch:
Screenshot from 2024-07-11 11-04-52

@paul121
Copy link
Member

paul121 commented Jul 11, 2024

Add support for filtering assets by farm.

Because the asset.farm field is a bundle field, this is working on the by_type asset pages but not the "All assets" page. I'm not sure the best way to include this on the "All assets" page.

There's a good amount of logic in our farm_ui_views_add_bundle_handlers() function that would be ideal to re-use. I feel like we should refactor this and/or add another helper function that allows adding a single bundle field handler when needed... _add_bundle_field_handler($field_name, ...). We could add a case for the "All assets" view in farm_ui_views_views_pre_render and use this function to add just the asset.farm filter. I also think this helper function could be useful in other contexts (allow any module to add their own bundle field to asset/log views)

I think the alternative is hard-coding some of this for only the asset.farm field on this single view, but even then I feel like we'll end up duplicating existing logic to load the views data table name, handler settings, etc. Thus maybe we take a step and refactor the existing function... hmm..

@mstenta mstenta force-pushed the 3.x-organization branch from 17b8888 to 14c896d Compare July 12, 2024 15:23
@mstenta
Copy link
Member Author

mstenta commented Jul 12, 2024

Added a few commits in my branch: https://github.com/paul121/farmOS/tree/3.x-organization

Thanks @paul121 - these look great! I merged them into the PR.

I added an additional commit to remove roles from Views remember_roles config. There were a couple in this PR, but also some in other farmOS Views, so I just did it all in a single commit.

@mstenta
Copy link
Member Author

mstenta commented Jul 12, 2024

@paul121 and I just discussed in chat and agreed that farm should be a base field, not a bundle field. I pushed a commit to change it.

This avoids the current issue with bundle fields not allowing writes via the API: https://www.drupal.org/project/farm/issues/3314741

However, this means that the farm field will not be automatically added to things like Views and CSV importers and exporters. This might require refactoring the logic in farm_ui_views, farm_import_csv, and farm_export_csv (maybe others) to add support for base fields, which could be done in a separate PR as a prerequisite for this one. I will update the description above to make note of that.

@mstenta mstenta force-pushed the 3.x-organization branch 2 times, most recently from 7eda625 to 27fb452 Compare July 25, 2024 16:17
@mstenta
Copy link
Member Author

mstenta commented Jul 29, 2024

Pushed a commit that adds organization entity support in the farm_role module. Otherwise the default permissions for creating organizations are not added to roles. We may want to consider changing some of the defaults for this (eg: should farm_worker be able to create/edit orgs? maybe not.)

@mstenta mstenta mentioned this pull request Jul 31, 2024
@mstenta mstenta added this to the v3.3.0 milestone Jul 31, 2024
@mstenta mstenta modified the milestones: v3.3.0, v3.4.0 Sep 19, 2024
@mstenta
Copy link
Member Author

mstenta commented Nov 18, 2024

Picking this back up and thinking through the remaining pieces listed above...

Add "Farm" column to asset Views.
Add support for filtering assets by farm.

For these I was imagining the same functionality we currently have for bundle fields. We have logic that automatically adds fields and filters to the farm_asset View for bundle fields defined by hook_farm_entity_bundle_field_info(). However, the farm field is currently defined as a base field, not a bundle field, so it is not automatically added.

Makes me think about Log lists though... Logs do not have a farm field on them in this PR. I wonder if it would be worth adding a computed farm field to Log entities, which looks at the asset / location referenced by the log for farm association. 🤔

Allow setting farm via asset CSV import.

We actually have similar logic to what I just described for automatically adding CSV import columns. But again, it only applies to "bundle fields". It doesn't happen automatically for "base fields". This actually came up in another issue recently (in the context of CSV exports, but same basic ideas): #812 (comment)

Maybe it's time to explore adding a mechanism for automatically adding Views fields/filters and CSV import columns for base fields. 🤔

Include farm in asset CSV exports.

I wonder if this is already happening? Or maybe it needs some extra logic to support the new organization entity type. Needs testing...

Add a bulk action for assigning assets to a farm.

This should be pretty easy. Just a matter of adding a new action to Views.

Add support for filtering location hierarchy by farm.

I'm imagining a dropdown of Farms in the /locations page, which filters the hierarchy to only include assets in that farm.

This was cloned directly from the asset module, with no other
modifications, using the following commands:

cp -r modules/core/asset/ modules/core/organization
find modules/core/organization -depth -exec rename 's/asset/organization/g' {} \;
find modules/core/organization -depth -exec rename 's/Asset/Organization/g' {} \;
find modules/core/organization -type f -exec sed -i 's/asset/organization/g' {} +
find modules/core/organization -type f -exec sed -i 's/Asset/Organization/g' {} +
- Use statements should be sorted alphabetically
- Line exceeds 80 characters
This was copied from the asset module, which required a
migration path from Drupal 7. The new organization entities
did not exist on Drupal 7 so they don't need a migration path.
These are remnants of the farmOS v2 branch.
'view' => 10,
],
],
'user' => [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could remove the user field to leverage it from a Group integration instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good question @pcambra! I've been thinking about it ever since it came up in conversation.

(For readers who may not be familiar with recent conversations, this is referring to ideas for a potential contrib farmOS module that uses the data types provided by this PR + the Drupal Group module to enable a "multi-tenant" farmOS instance with access control between groups/farms).

In the context of the multi-tenant farmOS module, there needs to be a way to link users to Groups (farms), so that access control can be applied to them. The Group module provides its own way of doing this.

However, as of right now the plan is for that to be a contrib module which extends core farmOS data model. So perhaps the question right now is: are there use-cases for linking users to farms outside of the multi-tentant access controlled context? If so, then we may need a user relationship in core as well.

In reviewing the forum discussion, I found this, which I had forgotten about:

Discussed on the farmOS dev call today (with @symbioquine @paul121 @Fosten):

  • We decided to wait on adding the “Users” field to Organization entities. There isn’t a direct use for it in the current PR, and we may want to consider putting an Organization reference field on the User records instead.

Since there is a use-case now (multi-tenant farmOS), perhaps we should discuss it together again. If there is any possibility that we will want a user relationship in the core data model, then we should add that now and the multi-tenant module should work around it IMO. Otherwise we may end up with multiple different implementations for the same thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied most of this to the forum topic: https://farmos.discourse.group/t/organization-level-data/2003/10

Let's discuss over there (and in a future dev call).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants