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() ?>">

<?php endforeach ?>

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

object(Field)#1140 (3) {
  string(23) "projects/a-sum-of-parts"
  string(12) "extvideotype"
  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

    label: Video Type
    type: fieldtoggle
      extvideo: External Video
      webvideo: Website Video
      extvideo: extvideotype
      webvideo: webvideolink websitelink
      extvideo: webvideolink websitelink
      webvideo: extvideotype

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

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

Would be more interesting to see a dump of $extvideo


Ah yes. The output is

array(3) {
  string(22) "interstitials.mp4"
  string(27) "https://vimeo.com/channels/staffpicks/274716344"

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) {
  string(18) "introduction.mp4"
  string(0) ""


array(2) {
  string(15) "draw_record.mp4"
  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() ?>">
        <?php endif ?>

<?php endforeach ?>