How can i check if structure entries inside site.yml do match with the selected tags inside event page?

Hello,

I am trying this method to translate user added tags: Translatable user added tags - #4 by texnixe, but i don’t really get the last part:

Then in the frontend use the tags field stored value to loop over the structure field, match entry, and fetch the translations

Inside site.yml file i have a structure with translations:

tagsTranslation:
  label: Translations
  type: structure
  translate: false
  fields:
    lt:
      label: LT
      type: text   
    en:
      label: EN
      type: text 

Then inside event-article.yml I have tags field for event categories with options querying from the site structure:

#category
category:
  label: Category
  type: tags
  accept: options
  translate: false
  options:
    type: query
    query: site.tagsTranslation.toStructure
    text: "{{ item.lt }}"
    # value: "{{ item.lt }}"

And finally home.php template file:

<!-- events dates -->
<div class="events--dates" id="events--dates">

<?php foreach($events as $eventPost): ?>

  <?php foreach($site->tagsTranslation()->toStructure() as $translation): ?>
    
    <p class="info-text">         
        
        <?php if($kirby->language() == 'lt'): ?>
    
            <?php 
            echo $translation->lt(); 
            ?>
    
        <?php endif; ?>
    
        <?php if($kirby->language() == 'en'): ?>
    
            <?php
            echo $translation->en();
            ?>
    
        <?php endif; ?>
    
    </p>
    
    <?php endforeach; ?>
<?php endforeach; ?>

But how can i check if structure entries inside site.yml do match with the selected tags inside event page and then displaying only those matched entries?

Because right now i get all declared tags inside site.yml structure:
Screenshot 2023-03-09 at 16.42.33

Any input is really appreciated, thank you.

You current code doesn’t make sense. You want to get the currently selected tags:

<?php $siteTags = $site->tagsTranslation()->toStructure();?>
<?php foreach($events as $eventPost): ?>
<?php
$languageCode = $kirby->language()->code();
$tags = $eventPost->tags()->split();
foreach ($tags as $tag) {
  $structureItem = $siteTags->findBy('lt', $tag);
  if ($structureItem) {
    echo $structureItem->{$languageCode}();
  }
}
?>
<?php endforeach ?>

Would make sense to hide this mess away in a custom method.

1 Like

Sometimes it just helps to post a question and immediately find a solution… :sweat_smile:

<p class="info-text">         
                                                
  <?php if($kirby->language() == 'lt'): ?>
    
      <?php foreach($eventPost->categories() as $category): ?>
      <?php if($translation == $category): ?>
    
          <?php 
          echo $translation->lt(); 
          ?>
    
      <?php endif; ?>
      <?php endforeach; ?>
    
  <?php endif; ?>
    
  <?php if($kirby->language() == 'en'): ?>
    
      <?php foreach($eventPost->categories() as $category): ?>
      <?php if($translation == $category): ?>
    
          <?php
          echo $translation->en();
          ?>
    
      <?php endif; ?>
      <?php endforeach; ?>
    
  <?php endif; ?>
  
</p>

Yes, my code was terribly illogical… Thank you for your time :slightly_smiling_face: