$field->toStructure() correct count, but empty fields

Hey there,

I am trying to access data from a text file in a template using the $field->toStructure() method. I´ve done this successfully in kirby2, now I fail in kirby3 (on my local MAMP) even when copying the code directly from the online-reference, I´m clueless.

The content in the text file looks like this:


uebergaenge:

-
	abschnitt:	Strecke 1	
	number:	BÜ 1	
	name:	Bahnhofstraße	
	zustand:	offen
-
	abschnitt:	Strecke 1
	number:	BÜ 2	
	name:	Andreaskreuz	
	zustand:	offen
-
	abschnitt:	Strecke 2	
	number:	BÜ 3	
	name:	Kreuzstraße	
	zustand:	geschlossen
-
	abschnitt:	Strecke 1	
	number:	BÜ 4	
	name:	Hägar der Schreckliche Straße	
	zustand:	offen

In my template I try accessing it like this:

<?php foreach ($page->uebergaenge()->toStructure() as $uebergang): ?>
	<?= $uebergang->name() ?>
<?php endforeach ?>

It creates the correct numbers of divs, but they remain empty. When I dump the array like so:

<?= dump($page->uebergaenge()->yaml()) ?>

the contents of the array look correct, but they array is dumped twice. Here´s the output (without the repetition):

Array
(
    [0] => Array
        (
            [0] => abschnitt:	Strecke 1
            [1] => number:	BÜ 1
            [2] => name:	Bahnhofstraße
            [3] => zustand:	offen
        )

    [1] => Array
        (
            [0] => abschnitt:	Strecke 1
            [1] => number:	BÜ 2
            [2] => name:	Andreaskreuz
            [3] => zustand:	offen
        )

    [2] => Array
        (
            [0] => abschnitt:	Strecke 2
            [1] => number:	BÜ 3
            [2] => name:	Kreuzstraße
            [3] => zustand:	geschlossen
        )

    [3] => Array
        (
            [0] => abschnitt:	Strecke 1
            [1] => number:	BÜ 4
            [2] => name:	Hägar der Schreckliche Straße
            [3] => zustand:	offen
        )

)

Both copies of the array are contained in pre-tags in the resulting source code.

Have I overlooked something super obvious? Probably I´m gonna feel reeeeeaaaaally stupid, but I´m stuck already for two days :confused:

Thanks for any suggestions!

Just did a quick test:

If I run this

<?php foreach ($page->uebergaenge()->toStructure() as $uebergang): ?>
	<?= $uebergang->name() ?>
<?php endforeach ?>

I get the correct content (I think)

16

As for the second issue (the content dumped twice) I think the issue is that you’re echoing with <?= and as a result you get the second copy

1 Like

I copied your text content into a file and the problem seems to be the tabs after the colons instead of a space character. When I then use the code, $uebergang->name() in fact doesn’t echo anything. Removing the tabs and replacing with spaces works, however.

The code as such is correct and should give you the correct results.

That is also the reason why your yaml array looks so funny, when it should look like this:

Array
(
    [0] => Array
        (
            [abschnitt] => Strecke 1
            [number] => BÜ 1
            [name] => Bahnhofstraße
            [zustand] => offen
        )

    [1] => Array
        (
            [abschnitt] => Strecke 1
            [number] => BÜ 2
            [name] => Andreaskreuz
            [zustand] => offen
        )

    [2] => Array
        (
            [abschnitt] => Strecke 2
            [number] => BÜ 3
            [name] => Kreuzstraße
            [zustand] => geschlossen
        )

    [3] => Array
        (
            [abschnitt] => Strecke 1
            [number] => BÜ 4
            [name] => Hägar der Schreckliche Straße
            [zustand] => offen
        )

)
1 Like

@texnixe Sublime is probably smart enough to convert the tab into spaces then. Because I literally copy pasted his code on a txt file and everything is working just fine on my end.

Nice catch.

That probably depends on your settings, yes.

Thank you @texnixe. That was the problem. Like I said: I am feeling quite silly. But that´ll pass … Have a super nice day, you too @manuelmoreale and thanks to you too.

2 Likes

I have a similar problem. My structure field looks like this:

Meldungen:

-
  anrede: Herr
  akad_titel: Prof. Dr. med.
  nachname: Heisel
-
  anrede: Herr
  akad_titel: Dr. med.
  nachname: Kaiser
-
  anrede: Herr
  akad_titel: Dr.
  nachname: Hirsch

When I convert it to YAML and dump it

$regs = $page->meldungen()->yaml();
var_dump($regs);

I get this

array(3) {
  [0]=>
  array(3) {
    ["anrede"]=>
    string(4) "Herr"
    ["akad_titel"]=>
    string(14) "Prof. Dr. med."
    ["nachname"]=>
    string(6) "Heisel"
  }
  [1]=>
  array(3) {
    ["anrede"]=>
    string(4) "Herr"
    ["akad_titel"]=>
    string(8) "Dr. med."
    ["nachname"]=>
    string(6) "Kaiser"
  }
  [2]=>
  array(3) {
    ["anrede"]=>
    string(4) "Herr"
    ["akad_titel"]=>
    string(3) "Dr."
    ["nachname"]=>
    string(6) "Hirsch"
  }
}

When I convert it to a structure and dump it

$regs = $page->meldungen()->toStructure();
var_dump($regs);

I get this

object(Kirby\Cms\Structure)#386 (3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
}

No tabs in the structure field.

Kirby 3.2.3 on local MAMP and on uberspace, same result.

I´m quite perplexed.

Yes, the output from collections is very limited (only the keys). If you do a

dump($regs->toArray());

you get more useful output for debugging.

@texnixe Thanks again, you´re the best!

I tried to filter the structure and

meldungen()->toStructure()->filterBy('nachname', $param );

gave me only this supposed empty object, that I couldn´t convert to CSV for export. Now I use

meldungen()->toStructure()->filterBy('nachname', $param )->toArray();

and my CSV export route works.

Thanks a lot again and sorry for abusing this actually solved thread.

@steenweg No problem.