Custom Video block error: Array to string conversion in block type & combining original video block with custom block

Hello,

I’m trying to build a custom video block based on the audio cookbook but when I try to add the attributes to the video stored in an array I get this error message: “Array to string conversion in block type”. I’m just not very fluent on php so I’m sure the mistake is the way I’m trying to pass the attributes array into the video tag. here’s my localvideo.php code:

<?php
//video
$vidfile = $block->vidfile()->tofile();



// Video Contols
$vidoptions = [
  'preload'       => 'metadata',
  'controls'      => $block->controls()->toBool() ? 'true' : null,
  'playsinline'   => $block->playsinline()->toBool() ? 'true' : null,
  'autoplay'      => $block->autoplay()->toBool() ? 'true' : null,
  'muted'         => $block->mute()->toBool() ? 'true' : null,
  'loop'          => $block->loop()->toBool() ? 'true' : null
];

?>



<div class="swiper-slide" >
<div class="slide-container">
<?php if ($vidfile = $block->vidfile()->tofile()): ?>
<div class="video">
  <figure class="video-player">
<video  <?php echo $vidoptions?> >
  <source src="<?= $vidfile->url() ?>" type="<?= $vidfile->mime() ?>">
  </video>
  </figure>
  <?php endif ?>
  <button>sound on</button>
  </div>

</div></div>

and here’s my blueprint

name: Local Video
icon: file-video
tabs:
  source:
    label: Main
    fields:
      vidfile:
        label: Video
        type: files
        uploads: video
        multiple: false
        width: 1/1
      controls:
        label: Controls
        type: toggle
        width: 1/3
      mute:
        label: Mute
        type: toggle
        width: 1/3
      autoplay:
        label: Autoplay
        type: toggle
        width: 1/3
      loop:
        label: Loop
        type: toggle
        width: 1/3
      playsinline:
        label: Playsinline
        type: toggle
        width: 1/3

I also would like to ask if someone could point me in what would be the best way to combine this custom block with the original video block since the idea is to be able to upload vimeo files and local files and it seems a bit redundant to use 2 different blocks for this or is it better to keep them separated?

I really appreciate your help since I’m really stuck on how to make the array work. Thank you so much!

You could take a look at my plugin over here GitHub - HashandSalt/kirby3-video: Kirby 3 plugin for embedding local videos for pointers

Has support for Kirby Tags as as custom block. I decided to make a seperate block for localvideos because it was simpler. You should be able to copy the default block into your plugin and add to code to it to do what you want tho… i was just lazy :slight_smile:

This line is the source of your error. $vidoptions is an array and you are trying to echo this array. What you’d need here is to implode() the array again to get a string, or don’t create an array in the first place.

<?= implode(' ', $vidoptions) ?>

Thank you so much for both your answers and sorry for the late reply, unfortunately the implode didn’t work because I only got the state of the boolean “true” instead of the value but I did it without the array as you suggested like this:

  <figure class="video-player">
<video id='localvideo' <?= $block->controls()->isTrue() ? 'controls' : '' ?>
      <?= $block->autoplay()->isTrue() ? 'autoplay' : '' ?> 
      <?= $block->mute()->isTrue() ? 'muted' : '' ?> 
      <?= $block->loop()->isTrue() ? 'loop' : '' ?> 
      >
  <source src="<?= $vidfile->url() ?>" type="<?= $vidfile->mime() ?>">
  </video>
  <figcaption><?= $block->caption() ?></figcaption>
  </figure>
  <?php endif ?>

Now I am having difficulties merging this custom block with the default vimeo block, on my local video blueprint I added another tab for the vimeo block and it’s working but on the localvideo.php I can’t manage to figure it out how to put them together. If I only add a vimeo video it says “no video was added” it seems it only assumes the local video. here is my localvideo.php

<?php
//video
$vidfile = $block->vidfile()->tofile();

?>



<div class="swiper-slide" >
<div class="slide-container">
<?php if ($vidfile = $block->vidfile()->tofile()): ?>

  <figure class="video-player">
<video id='localvideo' <?= $block->controls()->isTrue() ? 'controls' : '' ?>
      <?= $block->autoplay()->isTrue() ? 'autoplay' : '' ?> 
      <?= $block->mute()->isTrue() ? 'muted' : '' ?> 
      <?= $block->loop()->isTrue() ? 'loop' : '' ?> 
      >
  <source src="<?= $vidfile->url() ?>" type="<?= $vidfile->mime() ?>">
  </video>
  <figcaption><?= $block->caption() ?></figcaption>
  </figure>
  <?php endif ?>
  <a id="sound-button" onclick="toggleMute();">Unmute</a>


</div></div>

<?php /** @var \Kirby\Cms\Block $block */ ?>
<?php if ($video = video($block->url()->isNotEmpty(),

[
  'vimeo' => [
    'controls' => 1
  ],
]
)): ?>

could you please point me in the right direction? I want to be able to add either a local video or a vimeo video, thank you so much!