Undefined index using YAML

I want to add a class to an element based on a checkbox field. The checkbox field checksound is nested inside an Engineer field and therefore I have used YAML. However, I am getting the error:

Undefined index: checksound

From the following code:

<?php foreach($project->extvideotype()->yaml() as $extvideo):?>
    <video class="player 
       <?php  if ($extvideo['checksound'] == 'true'): ?>
           sound<?php endif; ?>
       ?>" playsinline controls loop>
       <source src="<?= $extvideosource->url(); ?>" type="<?= $extvideosource->mime() ?>">
    </video>

<?php endforeach ?>

This is odd as the output of var_dump($project->extvideotype()); produces the following:

object(Field)#1140 (3) {
  ["page"]=>
  string(23) "projects/a-sum-of-parts"
  ["key"]=>
  string(12) "extvideotype"
  ["value"]=>
  string(109) "-
  extvideosource: "interstitials.mp4"
  vimeolink: "https://vimeo.com/channels/staffpicks/274716344"
  checksound: true"
}

checksound does exist so where am I going wrong? I have included the blueprint for context below. Any help is very much appreciated.

excerpt of project.yml

fields:
  videotype:
    label: Video Type
    type: fieldtoggle
    options:
      extvideo: External Video
      webvideo: Website Video
    show:
      extvideo: extvideotype
      webvideo: webvideolink websitelink
    hide:
      extvideo: webvideolink websitelink
      webvideo: extvideotype

  extvideotype:
    type: engineer
    fields:
      extvideosource:
        label: Self Video Source
        type: select
        options: videos
        width: 1/2

      vimeolink:
        label: Vimeo Link
        type: embed
        width: 1/2
      
      checksound:
        label: Sound
        type: checkbox

Would be more interesting to see a dump of $extvideo

dump($extvideo);

Ah yes. The output is

array(3) {
  ["extvideosource"]=>
  string(22) "interstitials.mp4"
  ["vimeolink"]=>
  string(27) "https://vimeo.com/channels/staffpicks/274716344"
  ["checksound"]=>
  bool(true)
}

Hm, that’s weird. And the error message definitely refers to this piece of the code?

I realised that perhaps the problem is that there is no checksound index for the other “projects”. I would have thought that checkbox would be output and set to false?

array(2) {
  ["extvideosource"]=>
  string(18) "introduction.mp4"
  ["vimeolink"]=>
  string(0) ""
}

Another:

array(2) {
  ["extvideosource"]=>
  string(15) "draw_record.mp4"
  ["vimeolink"]=>
  string(0) ""
}

Usually I would expect this to create an empty field. Anyway, instead of using yaml() you could use toStructure() and then shouldn’t run into this issue. Or check if the index. exists first.

I used the toStructure field and it works thank you.

<?php foreach($project->extvideotype()->toStructure() as $extvideo):?>
        <?php if($extvideo->extvideosource()->isNotEmpty()): ?>
            <video class="player <?php if($extvideo->checksound()->bool()): ?>sound<?php endif; ?>" playsinline controls loop>
                <source src="<?= $extvideo->extvideosource()->toFile()->url(); ?>" type="<?= $extvideo->extvideosource()->toFile()->mime() ?>">
            </video>
        <?php endif ?>

<?php endforeach ?>