I’m trying to iterate over some YAML output video is they exist but I’m running into the error:
Call to a member function isNotEmpty() on string
This is also problem when calling toFile()
Call to a member function toFile() on string
What is the correct way of doing this? I did read this but I can’t get my ahead around how it work.
<?php foreach($project->extvideotype()->yaml() as $extvideo):?>
<?php if($extvideo['extvideosource']->isNotEmpty()):?>
<?php $extvideosource = $extvideo['extvideosource']->toFile();?>
<a href="<?= $project->url() ?>">
<video autoplay loop>
<source src="<?= $extvideosource->url() ?>" type="<?= $extvideosource->mime() ?>">
</video>
</a>
<?php endif ?>
<?php if($extvideo['vimeolink']->isNotEmpty()):?>
<?php $vimeovideo = $extvideo['vimeolink']->toFile();?>
<a href="<?= $project->url() ?>">
<video autoplay loop>
<source src="<?= $vimeovideo->url() ?>" type="<?= $vimeovideo->mime() ?>">
</video>
</a>
<?php endif ?>
<?php endforeach ?>
isNotEmpty()
is a field method, but
$extvideo['extvideosource']
is not a field object but a string (you are looping through an array here), so you can’t use a field method.
You can check if a string is empty like this:
<?php if($extvideo['extvideosource'] !== ''):?>
Thank you so much. How do I get the video object from the string?
var_dump($project->extvideotype());
object(Field)#196 (3) {
["page"]=>
string(26) "projects/tfl-bike-data-vis"
["key"]=>
string(12) "extvideotype"
["value"]=>
string(47) "-
extvideosource: "tfl.mp4"
vimeolink: """
}
if($video = $page->file($extvideo['extvideosource'])) {
// do stuff
}
The important part is the if statement, in fact, if you do this check, you don’t have to check if the string is empty or not, because even if it is, the if statement will return false, anyway.
If, however, you check if your string is empty and it is not, but the file nevertheless doesn’t exist, then your call to the url()
method in the next step would throw an error without theif($video...)
statement.
I have another problem, I have decided to use the embed plugin for the vimeo video field however the nothing is showing up on the page. Embed is working through kirby text but I cannot get it to work with the YAML.
<?php if($vimeovideo = $project->file($extvideo['vimeolink'])): ?>
// embed field method
<?= $vimeovideo->embed() ?>
// embed global php helper function
<?= embed($video['vimeolink']) ?>
<?php endif ?>
A var_dump
outputs the following
object(Field)#803 (3) {
["page"]=>
string(23) "projects/a-sum-of-parts"
["key"]=>
string(12) "extvideotype"
["value"]=>
string(89) "-
extvideosource: "asop_interstitials.mp4"
vimeolink: "https://vimeo.com/274430991""
}
$vimeovideo is a. file object, now you are trying to call a field method on a file object.
Try to use toStructure()
instead. of. yaml and then within your. loop
$items = $project->extvideotype()->structure();
foreach($items as $item) {
echo $item->vimeolink()->embed();
}
Not sure if this correct without knowing your blueprint…