Tagging structured data instead of pages?

Looking for high-level advice on the best route to implement a feature :slight_smile:


Say there’s a business site that includes an employees page and a projects page.

On the projects page, you’ll be able to add projects using a Structure Field.
On the employees page panel, you’ll be able to add employees also using a Structure Field.

When you add a project, you should be able to tag it with a category. When the page is displayed, its layout is going to separate the projects by category.

When you add an employee, you should also be able to tag them with categories. Then, on the projects page, each category section will also have a list of employees tagged with that category.


Can you do this with tags? It looks like maybe Kirby is made so only $pages are meant to be tagged. Similarly, there are fields and plugins for selecting users, but not every employee is going to be a user of the site.

If I need to extend some part of Kirby, looking for general advice on how to think about this.

thanks!

It does not make a difference if you tag a page or a structure field, all you need to do is add a field to the structure field for the category.

Since the structure field leaves you with a collection, you should be able use all collection methods on it, so grouping your projects by category shouldn’t be a problem.

If your list of users is defined anywhere, either as a list or as subpages, you can select them like you can select panel users, all you need to do is query the users or the field that contains the users.

Thanks!

You may have misread about users. I actually can’t use users. But it sounds
like I can just use plain old tags - I’ll give it a go tomorrow and let you
know.

Well, maybe I should not have use the term “users”. Anyway, since your users are added using the select field, where do you want to select these employees? There is no way to query a structure field, but if you need that functionality, you can create a custom field or provide the list of employees as an API that can be queried in a select field. But that depends on your use case.

If I understand correctly, you want to have two pages, employees and projects. Each of them should have a structure field where you can add employee/project data and a category value for each entry.

In your template where you output the projects, you can then grab all employees with the current category like this:

<?php $employees = page('employees')->employees()->toStructure(); ?>
<?php foreach($page->projects()->toStructure() as $project): ?>
  <h2><?php echo $project->title() ?></h2>
  ...

  <?php foreach($employees->filterBy('category', $project->category()->value()) as $employee): ?>
    ...
  <?php endforeach ?>
<?php endforeach ?>

Please note that this solution doesn’t actually use a tags field but a text field as there can only be one category for each project and employee. If you need multiple, that’s possible as well but the code would look different.

It might even make sense to use a select (or checkboxes) field for the categories if you want to limit the options somehow.

1 Like

Please note that this solution doesn’t actually use a tags field but a text field as there can only be one category for each project and employee. If you need multiple, that’s possible as well but the code would look different.

@lukasbestle Could you elaborate on this a bit?

I actually want a sort of many-to-many relationship - Each employee should be able to have multiple categories associated with them. So would it be better for me to use Kirby’s tags, rather than a text field? Sounds like you’re saying I could do multiple categories with a text field but it would just be more involved to parse, but wondering if there is a reason tags are a no-go.

To clarify, for the project page design I actually just have a list of categories. Under each category it lists the projects and then the employees.

No, @lukasbestle only used a text field as an example, if you need multiple categories, the tags field is indeed much better suited.

The downside of the tags field (as well as the text field) is that you cannot control what the user can enter. So if you need a fixed list of categories, a checkboxes field would be better suited. It all depends on your use case.

General procedure:

  1. get a list of all categories
  2. loop through categories
  3. in the loop, filter all projects by each category
  4. get the list of employees, filter them by category as well

In code, that would be something like this:

<?php
$projects = $page->projects()->toStructure();
$employees = page('employees')->employees()->toStructure();
$categories = $projects->pluck('categories', ',', true);

foreach($categories as $category):
  $filteredProjects = $projects->filterBy('categories', $category, ',');
  foreach($filteredProjects as $project): ?>
   <h2><?php echo $project->title() ?></h2>

   <?php foreach($employees->filterBy('categories', $category, ',') as $employee): ?>
    // more code here
  <?php endforeach ?>

<?php endforeach ?>

Just wanted to circle back and say thanks a ton @texnixe and @lukasbestle ! Totally awesome. I was able to adapt this into what I needed pretty easily. Thanks so much for your help.

2 Likes