Manual gallery sorting on multi language site?

Hi
sorting image files manually by drag an drop in the panel is great but leads to saving the “sort” value in only one language txt file. Does it mean for the second language I will have to redo all sorting manually? Otherwise I would end up with two different gallery sequences…

Am I missing something? Or is there any way to sync file sorting between the languages?
thanks,
Markus

I don’t quite understand what you mean and how sorting files in the panel is reflected in a text file? Are you adding them to a structure field?

Manual file sorting results in the “sort: #” value, written by Kirby in the corresponding meta data “_.jpg.en.txt” file (or “_jpg.de.txt” for german) stored next to the image.

http://getkirby.com/docs/panel/blueprints/file-settings#sortable-files

Oh, I see, I’ve never used this feature yet. Maybe you can use a panel hook panel.file.sort and automatically create the meta data files for the missing language. http://getkirby.com/docs/panel/hooks

The sorting works like this: If you sort image while on main language, all other languages will inherit the same sorting order. But if you sort while not on main language, the sorting order will permanently be desynchronised.

Yes, they will inherit the order and synchronize as long as you do not enter any other meta data for your images: once you have pit a title translation in your second language, there will be one jpg.txt file for each language, and also different “sort” values in those files.

So when you try to change the sequence later, it is not synchronized any more…

I think as long as you don’t do any sorting in the none-default language, the sort field is not added to the meta data file and the sort order of the default language is used. But it seems that it is quite easy to get it wrong, accidentally do the sorting in the wrong language and mess it all up.

No, texnixe. I do sorting only in the default language. The “sort” field plus value is automatically copied to the none-default language, that is the problem. So once I do a translation of a title or anything I have 2 txt-files and 2 values…

Maybe there is a way to tell kirby to NOT copy the sort field to other languages? I guess no one wants different sequencing when using the same galleries in all languages.

Oh yes, you are right, that field is automatically taken over from the default language when you add further meta data.

While there may be reasons to have other sorting orders in different languages, I agree that it should be possible to synchronize the sort order if required.

As I mentioned above, one such way would probably be a panel hook. Or get the sort order from the default language meta file in your templates (I currently don’t know how to achieve that but maybe there is a way).

ok. But what does a panel hook do? And how could it help me here? Sorry, but I am quite new with coding…

I have a similar problem. I want to synchronize a few fields and tried this hook:

kirby()->hook('panel.page.update', function($page) {
  $site = site();
    if ($site->language()->code() === 'de') {
      $page->content('en')->update(array(
        'date' => $page->date()
      ));
    } else {
      $page->content('de')->update(array(
        'date' => $page->date()
      ));
    }
});

Unfortunately this does not work because update() is not available for the $content object. Is it possible to get the $page object of a specific language?

Further Reading

http://getkirby.com/docs/cheatsheet/page/content
http://getkirby.com/docs/cheatsheet/page/update
http://getkirby.com/docs/panel/hooks

not tested, but try;

kirby()->hook('panel.page.update', function($page) {
  $site = site();
    if ($site->language()->code() === 'de') {
      $page->update(array(
        'date' => $page->content('en')->date()
      ));
    } else {
      $page->update(array(
        'date' => $page->content('de')->date()
      ));
    }
});

Quick question: where would you put the hook code? In which file?

You can put the hook into your config.php file or register the hook in a plugin file, see docs

1 Like

Fiew years later…

Hi !

I’m in the same situation than people before.
My client use a multilanguage website and as long as he don’t change the sorting on his images when he is inside the non-default language settings, it’s ok. However if he do that mistake, the files sorting are all desynchronized in that actual project.

I looked on the forum and didn’t find a proper solution, but I found the issue on Github :

So until the v3 is here, there is maybe a way to do it otherwise :

  • Use a images field to sort files instead, and add it a translate: false

  • Make a panel custom css hack to prevent modification when it’s not the default language.
    But I can’t find a selector to do that, and I don’t know how to create-it properly .

  • Use only the sorting of the default language
    $file->meta('default_language_code')->sort()
    This idea would work better with the one above.

  • Use Hooks to apply the the new sorting of whatever language to all the other
    I Think this is the best way to do it so I tried this :

    // The default language in this case is French
    // The other one is English 
    kirby()->hook('panel.file.sort', function($file) {
        $site = site();
        if ($site->language()->code() === 'fr') {
            $file->meta('en')->update(array(
                'sort' => $file->meta('fr')->sort()
            ));
        } else {
            $file->meta('fr')->update(array(
                'sort' => $file->meta('en')->sort()
            ));
        }
    });
    

But I had no results with this last one, and don’t know where it failed.

So if you have a tips for me about all that, I will be very thankful !

Another option that could work would be to use permissions(panel.file.sort) and disallow file sorting based on language. The downside is that you have to set up this rule for each role. And the use the sort order of the default language.

First thing first, thank you for your reply !

That seems to be a nice idea but I’m facing some troubles.

I tried two way :

The first time I left files: sortable: true in my blueprint and did this :

// site/config.php
c::set('roles', [
    [
        'id'      => 'admin',
        'name'    => 'Admin',
        'default' => true,
        'panel'   => true,
        'permissions' => [
            '*'               => true,
            'panel.file.sort' => function() {
                if( $this->site()->language()->code() === 'fr' ) {
                    return true;
                } else {
                    return false;
                }    
            }
         ]
    ],
    [
        'id'      => 'test',
        'name'    => 'Test',
        'panel'   => true,
        'permissions' => [
            '*'               => true,
            'panel.file.sort' => function() {
                if( $this->site()->language()->code() === 'fr' ) {
                    return true;
                } else {
                    return false;
                }    
            }
        ]
    ]
]);

The second time I changed my bluprint for files: sortable: false and did that :

// site/config.php
c::set('roles', [
    [
        'id'      => 'admin',
        'name'    => 'Admin',
        'default' => true,
        'panel'   => true,
        'permissions' => [
            '*'               => true,
            'panel.file.sort' => function() {
                return $this->site()->language()->default();
            }
         ]
    ],
    [
        'id'      => 'test',
        'name'    => 'Test',
        'panel'   => true,
        'permissions' => [
            '*'               => true,
            'panel.file.sort' => function() {   
                return $this->site()->language()->default();
            }
        ]
    ]
]);

Nothing happened. If files are sortable like said in the blueprint it’s true for all languages, and conversely.

I tested to put panel.page.update instead of panel.file.sort and that worked the two different times for the test role but not for the admin.

The admin role cannot be restricted, I’m afraid.

Duly noted. I also tested it by created a Test role, so if it’s possible do make panel.file.sort working this way, I will have to re-create the admin role with the exception of the sorting permission ?

Focusing on a test role :

// site/config.php
c::set('roles', [
    [
        'id'      => 'test',
        'name'    => 'Test',
        'panel'   => true,
        'permissions' => [
            '*'               => true,
            'panel.file.sort' => function() {
                    return $this->site()->language()->default();
                }
            }
        ]
    ]
]);

Any idea why when I’m connecting with the Test user which has a test role, i’m still capable of sorting files in whatever language ?

While when I change panel.file.sort to panel.page.update for example this code do the right thing that is to prevent the test user to update the page if it’s not in default language.

EDIT

I just tested this :

// site/config.php
c::set('roles', [
    [
        'id'      => 'test',
        'name'    => 'Test',
        'panel'   => true,
        'permissions' => [
            '*'               => true,
            'panel.file.sort' => false
        ]
    ]
]);

The panel.file.sort permission doesn’t do much… if it’s false and the files are sortable in the blueprint so there will be sortable.

Inversely if the panel.file.sort is set to true and the files are not set sortable inside the blueprint they will still not be able to move.

Am I doing something wrong or the panel.file.sort permission just don’t work ?

I’m running Kirby 2.5.5 on this project