YAML within structure field?


#1

Hello, I’m using the Kirby List Field inside of the Kirby Engineer Field. With the code I have written, if I put more than one item in the list field the output is “projects/example-project-1 yaml”. Putting just one list item outputs the correct content. What am I doing wrong here?

project.php

   <?php foreach($page->projectteam()->toStructure() as $member):?>
      <div class="team-entry">
          <?= $member->role()->esc() ?>
      </div>
      <div>
          <?php foreach($member->name()->yaml() as $name): ?>
              <?= $name ?>
          <?php endforeach; ?>
      </div>
   <?php endforeach ?>

project.yml

  projectteam:
    label: Project Team
    type: engineer
    fields:
      role:
        label: Collaborator Role
        type: text
        width: 1/2
      name:
        label: Collaborator Name
        type: list
        placeholder: Add a collaborator
        width: 1/2

#2

Interestingly, $member->name()already returns a structure field, so this works:

<?php foreach($member->name() as $name): ?>
  <?= $name ?>
<?php endforeach; ?>

#3

The names are now showing but it is also outputting the url of the project “project/example-project-1” and “name” the name of the field.


#4

Could you. show me

  • what is. in your content file of that page
  • the output (screenshot)

#5

A var_dump of $member. I would just like to show the role and the names of the people that did the role.

object(Structure)#302 (2) {
  ["role"]=>
  object(Field)#303 (3) {
    ["page"]=>
    string(26) "projects/example-project-1"
    ["key"]=>
    string(4) "role"
    ["value"]=>
    string(10) "Architects"
  }
  ["name"]=>
  object(Structure)#304 (3) {
    [0]=>
    object(Field)#305 (3) {
      ["page"]=>
      string(26) "projects/example-project-1"
      ["key"]=>
      int(0)
      ["value"]=>
      string(18) "Wendy"
    }
    [1]=>
    object(Field)#306 (3) {
      ["page"]=>
      string(26) "projects/example-project-1"
      ["key"]=>
      int(1)
      ["value"]=>
      string(24) "Jill"
    }
    [2]=>
    object(Field)#307 (3) {
      ["page"]=>
      string(26) "projects/example-project-1"
      ["key"]=>
      int(2)
      ["value"]=>
      string(24) "Simon"
    }
  }
}

#6

Yes, that looks perfectly ok. But that doesn’t show me where it echoes the URL of the page if you loop through. it. A field always contain the page information, but that is not echoed.


#7

Thank you so much for the help.

Project referenced in dump

48

Another example

project.php

   <div class="team-list">
        <div class="em-below">Project Team:</div>
        <?php foreach($page->projectteam()->toStructure() as $member):?>
            <div class="team-entry">
                <?= $member->role()->esc() ?>
            </div>
            <div>
                <?php foreach($member->name() as $name): ?>
                    <div><?= $name ?></div>
                <?php endforeach; ?>
            </div>
        <?php endforeach ?>
    </div>

#8

That’s really weird, I get the expected output in a starter kit:

Don’t know where URI. and the key come from in your example where Amin and Editor are. the roles.

Does you content file look like it should?


#9

Yes, I believe so.

project.txt

----

Projectteam: 

-
  role: "Partner in Charge"
  name: "Alessandra"
-
  role: "Architects"
  name: 
   - "Wendy"
   - "Jill"
   - "Simon"

----

and

----

Projectteam: 

-
  role: "Director"
  name: 
   - "Herve"
   - "Leslie"
-
  role: "Make Up"
  name: "Brigitte "
-
  role: "Hair"
  name: "Martyn"

----

#10

That seems to be caused by this line, because with only one entry, it doesn’t store a yaml list in your file.

It would probably better to use a field type that generates a comma separated list.


#11

It would probably better to use a field type that generates a comma separated list…`

What is an example of this?


#12

Of the built in fields: a simple text field, a tags field

Unfortunately, I’m not aware of plugin that compares to the list field but stores a comma separated list instead. Modifying the list field to store a comma separated list would be an option. If your collaborators are stored anywhere, a multi-select field could also be an option.


#13

Thank you so much for the help. Perhaps I will just nest another engineer filed with a simple text field within the engineer field.


#14

The List Plugin is much more suitable from a UI perspective for the client. So I want to attempt to add the comma. However, PHP is not my strong point. Can it be just as simple as concatenating a comma somewhere in this following function?

  public function inputField($value) {

    $input = new Brick('input', null);
    $input->addClass('input');
    $input->attr(array(
      'type'         => $this->type(),
      'value'        => $value,
      'required'     => $this->required(),
      'name'         => $this->name() . '[]',
      'autocomplete' => $this->autocomplete() === false ? 'off' : 'on',
      'autofocus'    => $this->autofocus(),
      'readonly'     => $this->readonly(),
      'disabled'     => $this->disabled(),
      'id'           => $this->id()
    ));

    if(!is_array($value)) {
      $input->val($value, false);
    }

    if($this->readonly()) {
      $input->attr('tabindex', '-1');
      $input->addClass('input-is-readonly');
    }

    return $input;

  }

#15

No, I think you have to make changes to the result and value methods, where one is responsible for what. is stored and. the other for what is. read again from file. But not sure if it. needs some other adjustments as well. You’d. have to look into what is inherited from the parent methods.