# Multilang page restrict structur field

**URL:** <https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752>\
**Category:** Questions\
**Tags:** v3\
**Created:** [February 17, 2022, 1:43pm UTC](https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752 "2022-02-17T13:43:55Z")\
**Posts on this page:** 8\
**Page:** 2

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 19, 2022, 10:04am UTC](https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752/21 "2022-02-19T10:04:33Z")

</div>

That might well be, but for `$page->content('en')` etc. to work, you need to define the translations.

Example (shortened):

```auto

<?php

class MembersPage extends Page
{
  public function children()
  {
    $usersPages = [];
    $users = kirby()->users();
    foreach ($users as $key => $user) {
        $content = [
            'title' => $user->username(),
            'role' => $user->role(),
        ];
        $contentEn = [
            'about' => $user->content('en')->about(),
            'dates' => $user->content('en')->dates()->yaml(),
        ];
        $contentDe = [
            'about' => $user->content('de')->about(),
            'dates' => $user->content('de')->dates()->yaml(),
        ];

      $userPages[] = [
        'slug' => Str::slug($user->username()),
        'num' => $user->indexOf($users),
        'template' => 'member',
        'model' => 'member',
        'content' => array_merge($content, $contentEn),
        'translations' => [
            'de' => [
                'code' => 'de',
                'content' => array_merge($content, $contentDe),
            ],
            'en' => [
                'code' => 'en',
                'content' => array_merge($content, $contentEn),
            ],
        ]
      ];
    }
    return Pages::factory($userPages, $this);

  }

}

```

---

<div class="post-metadata">

**Author:** ![Perry](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/perry/32/1336_2.png) [@Perry](https://forum.getkirby.com/u/Perry)\
**Post date:** [February 19, 2022, 11:05am UTC](https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752/22 "2022-02-19T11:05:57Z")

</div>

Do I understand correctly you merge the complete content ? but I only want to merge dates().

Could I also just merge the dates() field of all languages, or does that not work because there is a conflict when changing the language?

```auto
   <?php

class MembersPage extends Page
{
  public function children()
  {
    $userPages;
    $users = kirby()->users();
    foreach ($users as $key => $user) {

      $content_de = [
        'language' => $user->language(),
        'title' => $user->username(),
        'role' => $user->role(),
        'type' => $user->member_type()->value(),
        'street' => $user->street(),
        'city' => $user->city(),
        'zip' => $user->zip(),
        'website' => $user->website(),
        'about' => $user->about(),
        'userid' => $user->id(),
        'cover' => $user->cover()->yaml(),
        'canton' => $user->canton(),
        'dates' => $user->content('de')->dates()->yaml(),
      ];
      $content_fr = [
        'language' => $user->language(),
        'title' => $user->username(),
        'role' => $user->role(),
        'type' => $user->member_type()->value(),
        'street' => $user->street(),
        'city' => $user->city(),
        'zip' => $user->zip(),
        'website' => $user->website(),
        'about' => $user->about(),
        'userid' => $user->id(),
        'cover' => $user->cover()->yaml(),
        'canton' => $user->canton(),
        'dates' => $user->content('fr')->dates()->yaml(),
      ];
      $content_it = [
        'language' => $user->language(),
        'title' => $user->username(),
        'role' => $user->role(),
        'type' => $user->member_type()->value(),
        'street' => $user->street(),
        'city' => $user->city(),
        'zip' => $user->zip(),
        'website' => $user->website(),
        'about' => $user->about(),
        'userid' => $user->id(),
        'cover' => $user->cover()->yaml(),
        'canton' => $user->canton(),
        'dates' => $user->content('it')->dates()->yaml(),
      ];

      $userPages[] = [
        'slug' => Str::slug($user->username()),
        'num' => $user->indexOf($users),
        'template' => 'member',
        'model' => 'member',
        'content' => array_merge($content_de, $content_fr, $content_it),
        'translations' => [
          'fr' => [
            'code' => 'fr',
            'content' => array_merge($content_de, $content_fr, $content_it),
          ],
          'it' => [
            'code' => 'it',
            'content' => array_merge($content_de, $content_it, $content_it),
          ],
        ]

      ];
    }
    return Pages::factory($userPages, $this);
  }
}

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 19, 2022, 11:55am UTC](https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752/23 "2022-02-19T11:55:52Z")

</div>

In the example above, I’m only merging the `$content` array (which is the same for all languages) with the language specific content for each language in the model, so that you get a translation with all content for each language.

Yes, you could do the merging for the dates field here already if you want, or do that in your template.

---

<div class="post-metadata">

**Author:** ![Perry](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/perry/32/1336_2.png) [@Perry](https://forum.getkirby.com/u/Perry)\
**Post date:** [February 19, 2022, 12:35pm UTC](https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752/24 "2022-02-19T12:35:45Z")

</div>

isn’t the value overwritten with the same key with array merge?

> **[PHP Tryit Editor v1.2](https://www.w3schools.com/php/phptryit.asp?filename=tryphp_func_array_merge2)**

```auto
<?php

class MembersPage extends Page
{
  public function children()
  {
    $userPages;
    $users = kirby()->users();
    foreach ($users as $key => $user) {

      $content_de = [
        'language' => $user->language(),
        'title' => $user->username(),
        'role' => $user->role(),
        'type' => $user->member_type()->value(),
        'street' => $user->street(),
        'city' => $user->city(),
        'zip' => $user->zip(),
        'website' => $user->website(),
        'about' => $user->about(),
        'userid' => $user->id(),
        'cover' => $user->cover()->yaml(),
        'canton' => $user->canton(),
        'dates' => $user->content('de')->dates()->yaml(),
      ];
      $content_fr = [
        'dates' => $user->content('fr')->dates()->yaml(),
      ];
      $content_it = [
        'dates' => $user->content('it')->dates()->yaml(),
      ];

      $userPages[] = [
        'slug' => Str::slug($user->username()),
        'num' => $user->indexOf($users),
        'template' => 'member',
        'model' => 'member',
        'content' => array_merge($content_de, $content_it),
        'translations' => [
          'fr' => [
            'code' => 'fr',
            'content' => array_merge($content_de, $content_fr),
          ],
          'it' => [
            'code' => 'it',
            'content' => array_merge($content_de, $content_it),
          ],
        ]

      ];
    }
    return Pages::factory($userPages, $this);
  }
}

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 19, 2022, 1:04pm UTC](https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752/25 "2022-02-19T13:04:10Z")

</div>

> [@Perry](#):
>
> isn’t the value overwritten with the same key with array merge?

Yes, in the end it doesn’t matter but it’s kind of superfluous.

---

<div class="post-metadata">

**Author:** ![Perry](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/perry/32/1336_2.png) [@Perry](https://forum.getkirby.com/u/Perry)\
**Post date:** [February 19, 2022, 1:08pm UTC](https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752/26 "2022-02-19T13:08:57Z")

</div>

holy,holy — okay now i get!

thank you @texnixe for your patient help.

**template**

```auto
	foreach ($kirby->languages() as $language) :
		foreach ($article->content($language->code())->dates()->toStructure() as $dates) :

			array_push($events, [
				"username" => $article->title(),
				"date" => strtotime($dates->date()),
				"title" => $dates->title(),
				"location" => $dates->location(),
				"city" => $dates->city(),
				"website" => $dates->website(),
				"festival" => $dates->festival(),
				"canton" => $dates->canton()
			]);

		endforeach;
	endforeach;

```

**model**

```auto

  foreach ($users as $key => $user) {

      $content_de = [
        'language' => $user->language(),
        'title' => $user->username(),
        'role' => $user->role(),
        'type' => $user->member_type()->value(),
        'street' => $user->street(),
        'city' => $user->city(),
        'zip' => $user->zip(),
        'website' => $user->website(),
        'about' => $user->about(),
        'userid' => $user->id(),
        'cover' => $user->cover()->yaml(),
        'canton' => $user->canton(),
        'dates' => $user->dates()->yaml(),
      ];
      $content_fr = [
        'dates' => $user->content('fr')->get("dates")->yaml(),
      ];
      $content_it = [
        'dates' => $user->content('it')->get("dates")->yaml(),
      ];

      $userPages[] = [
        'slug' => Str::slug($user->username()),
        'num' => $user->indexOf($users),
        'template' => 'member',
        'model' => 'member',
        'content' => $content_de,
        'translations' => [
          'de' => [
            'code' => 'de',
            'content' => $content_de,
          ],
          'fr' => [
            'code' => 'fr',
            'content' => $content_fr,
          ],
          'it' => [
            'code' => 'it',
            'content' => $content_it,
          ],
        ]

      ];
    }

```

---

<div class="post-metadata">

**Author:** ![Perry](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/perry/32/1336_2.png) [@Perry](https://forum.getkirby.com/u/Perry)\
**Post date:** [February 19, 2022, 1:11pm UTC](https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752/27 "2022-02-19T13:11:21Z")

</div>

to fully understand I had to use` translations[]` in the model to access languages in the template ?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 19, 2022, 1:15pm UTC](https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752/28 "2022-02-19T13:15:45Z")

</div>

You need the translations array to be able to access `$content($languageCode)`, because this relies on the translation. See

> <https://github.com/getkirby/kirby/blob/04127160ed1ab8dc277763cd8ba4ebc5a097bc18/src/Cms/ModelWithContent.php#L84>

[Previous page](https://forum.getkirby.com/t/multilang-page-restrict-structur-field/24752.md?page=1)
