Categories translations in blueprint don't work

Why do you think you get two strings there? Please take a look at your template code again.

Hi unfortunately I tried everything and I still don’t understand why it keeps displaying text value of the category names on top of each filter. I tried removing or changing the urls per category option and it still doesn’t work and it doesn’t translate either when I switch languages…

<!--Filter-->
     
        <div class="mt-10 z-10 px-12 sticky top-0 start-0">
       <aside id="default-sidebar">

        <div class="mt-6 w-64 fixed flex flex-col space-y-6 transition-transform translate-x-0">

          <a class="bg-lemon-yellow shadow-2xl text-2xl shadow-lemon-yellow rounded-full font-sans-serif text-brown w-fit"
            href="<?= $page->url() ?>">all</a>

            <?php foreach ($filters as $filter): {
            echo $field['category'][$filter][$kirby->language()->code()] ?? $filter;
            }
            ?>
            
           <a class="bg-lemon-yellow shadow-2xl text-2xl shadow-lemon-yellow rounded-full font-sans-serif text-brown w-fit"
            href="<?= $page->url() ?>?filter=<?= $filter ?>"><?= $filter ?></a>
          

            <?php endforeach ?> </div>
            
        </aside></div>

Ok, if you don’t see it.
Here, you fetch and echo the blueprint value for the filder

Then here you output the link with the name of the filter. And this is where you should use that piece of code from above.

And $field['category'] doesn’t seem to make sense either. Please do a dump($field) and show what that gives you.

IMO should be

 $field['options'][$filter][$kirby->language()->code()] 
1 Like

Thank you so much, the filter itself is now solved (it’s no longer double and it works well), however it’s not translating when the language is changed in frontend. When I do a dump($field), it gives me an array of only english options too. Is there a way to fix that?

<?php $field['options'][$filter][$kirby->language()->code()] ?? $filter;
            dump($filter); ?>

i get

Kirby\Content\Field Object
(
    [category] => current
)

Kirby\Content\Field Object
(
    [category] => upcoming
)

Kirby\Content\Field Object
(
    [category] => past
)

Please post the result of

dump($field);

You probably don’t have that variable defined at all, do you pass it from the controller to the template?

in the controller it’s defined like this

$field = $page->blueprint()->field('category');

and when i do dump($field); like this:

<?php $field['options'][$filter][$kirby->language()->code()]; 
           dump($field); ?>

I get a bug that it’s an undefined variable.

but when I do dump($field); from $field = $page->blueprint()->field(‘category’); I get nothing

Variables defined in the controller need to be returned from it in the return array, they are not magically passed to it just by defining them. So check the return array in your controller

1 Like

it’s defined and passed in the return array now, also changed ‘category’ to ‘options’ in the definition, but still no luck with translating

$field = $page->blueprint()->field('options');

this is the return array

return [

        'filterBy' => $filterBy,
        'unfiltered' => $unfiltered,
        'publications' => $publications,
        'pagination' => $pagination,
        'filters' => $filters,
        'field' => $field
    ];

and in the filter code in the template i have

<?php foreach ($filters as $filter):
            ?>
            
           <a href="<?= $page->url() ?>?filter=<?= $field['options'][$filter][$kirby->language()->code()] ?? $filter ?>">
            <?= $field['options'][$filter][$kirby->language()->code()] ?? $filter ?></a>
  

            <?php endforeach ?>

Wasn’t the field called category? You are not supposed to change the field name here.

Here you should be using options, this is correct, to get the options array from the field.

thank you! changed back to category yes but the translation doesn’t work that way either

You still haven’t shown me what

dump($field);

inserted at the top of your template returns.

when i do this one (is that the correct way?)

<?php $field = $page->blueprint()->field(‘category’);
dump($field); ?>

i get the 'undefined constant ‘category’ bug

I said in the template, not in the controller, right after the opening php tag.

Controller:

<?php 

return function ($page) {
    
$kirby = kirby();
$field = $page->blueprint()->field('category');

 // your other stuff

    return [

        'filterBy' => $filterBy,
        'unfiltered' => $unfiltered,
        'publications' => $publications,
        'pagination' => $pagination,
        'filters' => $filters,
        'field' => $field, // here we return the field variable

    ];
}; 

Then template

<?php
dump($field);
// rest of template

And make sure your field name is correct, is it really category? Or maybe categories(plural): Things don’t work if they are not correct. In coding, every single character is important.

I checked and it is category.

Ah sorry, I put this now at the top of the template and I get nothing, the page looks absolutely the same in frontend

<?php

dump($field); ?>

Ok, then please do the following:

  1. Post your complete blueprint
  2. Post your complete controller
  3. Post your complete template

If there is no output, then you have no field with the given name.

Blueprints:

from index.php

<?php

use Kirby\Cms\App as Kirby;

Kirby::plugin('cookbook/programmable-blueprints', [
    
    'blueprints' => [
       
        'fields/category' => function($kirby) {
            return include __DIR__ . '/blueprints/fields/category.php';
        },

    ],
]); 

from category.php:

<?php

use Kirby\Toolkit\I18n;

return [
    'label' => [
      'en' => 'Category',
      'ka' => 'Санат',
      'ru' => 'Категория'
    ],
    'options' => [
      'current' => I18n::translate('current', null, kirby()->language()->code()),
      'upcoming'  => I18n::translate('upcoming', null, kirby()->language()->code()),
      'past'       => I18n::translate('past', null, kirby()->language()->code())
    ],
    'type' => 'radio',
];

Controller:

<?php 

return function ($page) {
    
$kirby = kirby();
$field = $page->blueprint()->field('category');


$filterBy = get('filter');
$unfiltered = $page->children()->listed();
$publications = $unfiltered

    ->when($filterBy, function($filterBy) {
        return $this->filterBy('category', $filterBy);
    })
    ->paginate(4);

    $pagination = $publications->pagination();
    $filters = $unfiltered->pluck('category', null, true);

    return [

        'filterBy' => $filterBy,
        'unfiltered' => $unfiltered,
        'publications' => $publications,
        'pagination' => $pagination,
        'filters' => $filters,
        'field' => $field
    ];

}; 

Template:

<!--Header!-->

<?php snippet('header') ?>


<?php snippet('pattern') ?>


            
<!--Filter and publications!-->


        <!--Filter-->
<div class="mt-10 z-10 px-12 sticky top-0 start-0 ">
       <aside id="default-sidebar">


        <div class="mt-6 w-64 fixed flex flex-col space-y-6 transition-transform translate-x-0">

          <a class="bg-lemon-yellow shadow-2xl text-2xl shadow-lemon-yellow rounded-full font-sans-serif text-brown w-fit"
            href="<?= $page->url() ?>">all</a>

            <?php foreach ($filters as $filter):
            ?>
            
            
           <a class="bg-lemon-yellow shadow-2xl text-2xl shadow-lemon-yellow rounded-full font-sans-serif text-brown w-fit"
            href="<?= $page->url() ?>?filter=<?= $field['options'][$filter][$kirby->language()->code()] ?? $filter ?>">
            <?= $field['options'][$filter][$kirby->language()->code()] ?? $filter ?></a>
                
                
            <?php endforeach ?> </div>
            
        </aside></div>
    

         <!--Publications-->
        
        <div class="z-10 relative w-fit mx-auto">
        
        <ul class=" border-2 grid grid-template-column-3 ">
          
         <?php snippet('publications') ?> 

            </ul>

        <!--Pagination-->
        <nav>
        
        <a href="<?= $pagination->prevPageUrl() ?>">go to previous page</a>
        <a href="<?= $pagination->nextPageUrl() ?>">go to next page</a>
        </nav>
        



<!--Footer!-->
<?php snippet('footer') ?>

This doesn’t help, there is a field blueprint, but not where this is used with the field name. I need to see the page blueprint, not the field blueprint.

Ah this is the blueprint for page Publication, where category is mentioned

title: Publication

columns:
  main:
    width: 2/3
    sections:
      fields:
        type: fields
        fields:
          category:
            extends: fields/category
          description:
            type: textarea
            size: medium
          price:
            label: Price
            type: text
          page amount:
            label: Page amount
            type: text
          cover type:
            label: Cover type
            type: text
          paper type:
            label: Paper type
            type: text
          publication size:
            label: Publication size
            type: text
          sku:
            label: SKU
            type: text
          featured:
            label: Featured
            type: pages
            query: site.find('community')
                
  sidebar:
    width: 1/3
    sections:
      files:
        type: files
      



And this is the blueprint for Publications which is the parent of Publication

title: Publications

parent: site.find("publications")

sections:
  publications: 
    type: pages