Select field for languages in the panel

I would like to add a select field to the panel, where the admin can select the languages that have been completed and should therefore been added to the language-menu.
Unfortunately I can’t figure out the query for languages, nor find examples in the resources or the form.
I also failed to create a collection.

  languages:
    label: Active Languages
    type: select
    width: 1/2
    options: query
    query: site.languages

I guess I could also solve this by editing the language blueprint and adding an active toggle. Is there an example how to add custom fields to the language blueprint?

Should be kirby.languages, not site.languages. If you want to select multiple languages, a multiselect field would probably make more sense?

See also here: Query all site languages - #2 by texnixe

Thanks @pixelijn,
Indeed a multiselect makes more sense.
I did start with kirby.languages but it did not work with the simple query method.
Fetching, like explained in the linked thread, does work. Thanks a lot!

  languages:
    label: Active Languages
    type: multiselect
    width: 1/2
    options: query
    query: 
      fetch: kirby.languages
      text: "{{ item.name }}"
      value: "{{item.code }}"

Dear @pixelijn, I think it would actually be better to add a toggle to the language directly in the settings. Do you think that would be easily achievable?

I can’t tell you how easy that would be, but the bigger problem is that you cannot query custom fields in the language files. So if you add a custom field to en.php for example, there is no way to retrieve that value via the $language object.

Example:

return [
    'code' => 'de',
    'default' => false,
    'direction' => 'ltr',
    'locale' => [
        'LC_ALL' => 'de_DE'
    ],
    'name' => 'Deutsch',
    'customfield' => 'hallo',
    'translations' => [

    ],
    'url' => NULL
];

Trying to retrieve this with:

  foreach( $kirby->languages() as $language) {
    dump($language->customfield());
  }

will result in an error (Call to undefined method Kirby\Cms\Language::customfield()).

Aww, that’s unfortunate.
I’m struggling to combine the multiselect on the $site level with the actual $kirby->languages() in one foreach loop.
But I’m also not a programmer (that’s why Kirby became my best friend).

How are people hiding languages that are not ready yet for the public?

Could you post your code please?

This is without the selection, based on the Switch B tutorial:

	<div class="lang-menu">
	  <button class="lang-menu__button" id="lang-menu-open"><?= $kirby->language()->name() ?></button>
  		<ul class="lang-menu__items">
    		<?php foreach($kirby->languages()->not($kirby->language()) as $language): ?>
    		<li<?php e($kirby->language() == $language, ' class="active"') ?>>
      		<a href="<?= $page->url($language->code()) ?>" hreflang="<?php echo $language->code() ?>">
        		<?= html($language->name()) ?>
      		</a>
    		</li>
    		<?php endforeach ?>
  		</ul>
	  </div>

The multiselect field is $site->publiclanguages(). (not any longer in the code)

<?php $publicLanguages = $site->publiclanguages()->split(','); ?>
<div class="lang-menu">
	  <button class="lang-menu__button" id="lang-menu-open"><?= $kirby->language()->name() ?></button>
  		<ul class="lang-menu__items">
    		<?php foreach($kirby->languages()->not($kirby->language()) as $language): ?>
                <?php if (! in_array($language->code(), $publicLanguages) { continue; } ?>
    		<li<?php e($kirby->language() == $language, ' class="active"') ?>>
      		<a href="<?= $page->url($language->code()) ?>" hreflang="<?php echo $language->code() ?>">
        		<?= html($language->name()) ?>
      		</a>
    		</li>
    		<?php endforeach ?>
  		</ul>
	  </div>

Thanks a lot, that looks promising.
Sadly I’m getting an ParseError:

syntax error, unexpected 'continue' (T_CONTINUE)

in this line

<?php if (! in_array($language->code(), $publicLanguages) { continue; } ?>

Sorry, missing closing parenthesis:

<?php if (! in_array($language->code(), $publicLanguages)) { continue; } ?>

Amazing! Works perfect now. :slight_smile:
This was definitely above my php skills.