Filter and stripdown URL

Hi,

I’m trying to filter my url. I already filter my $journal items by looking if it’s a vimeo or youtube video.

But now I also want to filter the url, to get only the vimeo and youtube video ID. I want to put it in the data-youtube and data-vimeo attribute I created in the link.

https:// youtube . com /watch?v=uf2-5uDEkS8
https:// vimeo .com /176537673

This is my current code:

<?php if(!$journal->filterBy('videoUrl', 'vimeo')): ?>
  <li class="item">
   <img src="<?php echo $journal->overviewImage()->toFile()->focusCrop(371)->url() ?>" alt="<?php echo $journal->title()->html() ?>" />
   <a href="<?php echo $journal->videoUrl() ?>" data-vimeo="<?php echo $journal->videoUrl() ?>" >
    <span class="caption">
     <h2 class="title">
      <?php echo $journal->title()->html() ?>
     </h2>
     <span class="details">
      <?php echo $journal->date('F j, Y') ?>
     </span>
   </span>
  </a>
 </li>
 <?php else: ?>
  <li class="item">
   <img src="<?php echo $journal->overviewImage()->toFile()->focusCrop(371)->url() ?>" alt="<?php echo $journal->title()->html() ?>" />
   <a href="<?php echo $journal->videoUrl() ?>" data-youtube="<?php echo $journal->videoUrl() ?>" >
    <span class="caption">
     <h2 class="title">
      <?php echo $journal->title()->html() ?>
     </h2>
    <span class="details">
     <?php echo $journal->date('F j, Y') ?>
    </span>
   </span>
  </a>
 </li>
<?php endif ?>

I hope someone can help me out!

Cheers!

Maybe this link helps for YouTube videos: https://gist.github.com/simplethemes/7591414

You could probably come up with something similar for vimeo IDs.

The problem with this approach is that the URL structures of the services might change over time.

@texnixe thnx for your reply, I found another solution to get the video ID of the url.

But in the meantime I noticed that my <?php if(!$journal->filterBy('videoUrl', 'vimeo')): ?> is not working as I expected.

Is is not possible to filter a URL field by a ‘string’ part?

What am I doing wrong?

Not like this, your filter checks if the URL is ‘vimeo’, which will not return anything. You can use a filter with callback instead https://getkirby.com/docs/toolkit/api/collection/filter and within that filter check if the string contains “vimeo”.

FilterBy() with a filter method will also work:

 $journal->filterBy('videourl', '*=', 'vimeo');

Would you mind to share with us how you get the ID?