# Conditional fields depending on file type

**URL:** <https://forum.getkirby.com/t/conditional-fields-depending-on-file-type/15018>\
**Category:** Questions\
**Tags:** v3\
**Created:** [July 21, 2019, 7:49pm UTC](https://forum.getkirby.com/t/conditional-fields-depending-on-file-type/15018 "2019-07-21T19:49:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![trych](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@trych](https://forum.getkirby.com/u/trych)\
**Post date:** [July 21, 2019, 7:49pm UTC](https://forum.getkirby.com/t/conditional-fields-depending-on-file-type/15018/1 "2019-07-21T19:49:01Z")

</div>

Hello again,

I am trying to set up a file blueprint that works both for images and videos (as you can only assign one type of template in a section, correct?). Now, as the metadata I require for each are slightly different, I would like to show some fields/sections only conditionally depending on the file being an image or video.

Is there any way to do this? What would the syntax look like?

Thank you!

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [July 21, 2019, 8:00pm UTC](https://forum.getkirby.com/t/conditional-fields-depending-on-file-type/15018/2 "2019-07-21T20:00:15Z")

</div>

I don’t think you can trigger the conditional stuff based on file type. you could put tabs in the meta and put the video stuff in one tab and image fields in the other.

Personally i just use two sections, and set the template on each respectively. I leave the video one in list mode so it doesn’t take much room, and set the images one to cards. I normally put these under a "Media’ tab in the main page blueprint.

alternatively you could put a toggle field in the meta for toggling between video and image, and trigger the fields the way.

thirdly, you could use an upload hook to set the desired template in the files meta based on the file type. Its a bit of a cheat but should work.

This should do it in the config

```auto
'hooks' => [
  'file.create:after' => function ($file) {
      if (strpos($file->filename(), '.mp4') !== false) {
		$file->update([
			 'template' => 'YOUR TEMPLATE NAME',
		 ]);
      }
  },
]

```

Set the images meta template on the section, and the above code should reset it if the uploaded file happens to be a an MP4 video.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 21, 2019, 8:34pm UTC](https://forum.getkirby.com/t/conditional-fields-depending-on-file-type/15018/3 "2019-07-21T20:34:38Z")

</div>

> [@jimbobrjames](#):
>
> strpos($file-\>filename(), ‘.mp4’)

Why do you always use `strpos()` instead of checking the file extension?

```auto
if($file->extension() === 'jpg') {
  // do stuff
}

```

In the current case I’d probably use `$file->type()`

Instead of using different templates, you could also store the file type via a hook in a field and then show fields depending on that value.

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [July 21, 2019, 8:39pm UTC](https://forum.getkirby.com/t/conditional-fields-depending-on-file-type/15018/4 "2019-07-21T20:39:47Z")

</div>

> [@texnixe](#):
>
> Why do you always use `strpos()` instead of checking the file extension?

Well… because i did not know there was a thing for that 🙂 I just yoinked what I did in my Gilmour MP3 plugin… i can go and clean that up a little now…

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 21, 2019, 8:46pm UTC](https://forum.getkirby.com/t/conditional-fields-depending-on-file-type/15018/5 "2019-07-21T20:46:24Z")

</div>

> [@jimbobrjames](#):
>
> i can go and clean that up a little now…

🧹

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [July 21, 2019, 8:47pm UTC](https://forum.getkirby.com/t/conditional-fields-depending-on-file-type/15018/6 "2019-07-21T20:47:27Z")

</div>

Normally I google for how to find something in a string and it comes back with `strpos()`… i forget to check if Kirby has a helper.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 21, 2019, 9:45pm UTC](https://forum.getkirby.com/t/conditional-fields-depending-on-file-type/15018/7 "2019-07-21T21:45:14Z")

</div>

BTW: There are already two ideas issues that would help solve your use case and which you therefore might want to upvote (especially the first one)

> <https://github.com/getkirby/ideas/issues/231>

  

> <https://github.com/getkirby/ideas/issues/297>

---

<div class="post-metadata">

**Author:** ![trych](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@trych](https://forum.getkirby.com/u/trych)\
**Post date:** [July 21, 2019, 10:11pm UTC](https://forum.getkirby.com/t/conditional-fields-depending-on-file-type/15018/8 "2019-07-21T22:11:00Z")

</div>

> [@texnixe](#):
>
> Instead of using different templates, you could also store the file type via a hook in a field and then show fields depending on that value.

Thank you, that’s what I ended up doing now.  
Here’s the code for the hook, in case anyone needs something similar in the future:

```auto
  'hooks' => [
    'file.create:after' => function ($file) {
      $file->update(['filetype' => $file->type()]);
    }
  ]

```

And then, to show my section only if it’s an image:

```plaintext
sections:
  content:
    type: fields
    when:
      filetype: image

```
