# If check and fallback in Kirby Meta plugin

**URL:** <https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843>\
**Category:** Questions\
**Tags:** v2\
**Created:** [October 23, 2017, 4:35pm UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843 "2017-10-23T16:35:23Z")\
**Posts on this page:** 20\
**Page:** 1

<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:** [October 23, 2017, 4:35pm UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/1 "2017-10-23T16:35:23Z")

</div>

Im using the the meta plugin to generate facebook and twitter images, but im struggling to add a check for the feild being empty:

```auto
'image' => function($page) {
  return $page->isHomePage()
  ? site()->shareimage()->toFile()->focusCrop(1200,630)->url()
  : $page->shareimage()->toFile()->focusCrop(1200,630)->url();
},

```

The syntax is a little weird to what im used to.

i want to check that `$page->shareimage()` has a value and fallback to a static image stored `/assets/images`. (lets call it fallback.jpg) if its empty to prevent the whoops page showing up. How do i do that please?

---

<div class="post-metadata">

**Author:** ![pedroborges](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/pedroborges/32/7038_2.png) [@pedroborges](https://forum.getkirby.com/u/pedroborges)\
**Post date:** [October 23, 2017, 5:49pm UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/2 "2017-10-23T17:49:21Z")

</div>

Hi @jimbobrjames, this should do the trick:

```php
$image = $page->isHomePage()
            ? site()->feature()->toFile()
            : $page->feature()->toFile();

if (! $image) {
  $image = new asset('assets/images/fallback.jpg');
}

return $image->focusCrop(1200, 630)->url();

```

---

<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:** [October 23, 2017, 5:57pm UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/3 "2017-10-23T17:57:18Z")

</div>

Note that this will still throw an error if the fallback image does not exist.

---

<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:** [October 24, 2017, 10:28am UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/4 "2017-10-24T10:28:30Z")

</div>

@pedroborges Thanks but unfortunately this doesnt work.

```auto
    'og' => [
        'title' => function($page) {
            return $page->isHomePage()
            ? site()->seotitle()
            : $page->seotitle().' | '.site()->seotitle();
        },
        'image' => function($page) {
          $image = $page->isHomePage()
                      ? site()->feature()->toFile()
                      : $page->feature()->toFile();

          if (! $image) {
            $image = new asset('assets/images/fallback.png');
          }

          return $image->focusCrop(1200, 630)->url();
        },
        'type' => 'website',
        'site_name' => site()->seotitle(),
        'url' => function($page) { return $page->url(); },
        'description' => function($page) {
            return $page->isHomePage()
            ? site()->seometa()
            : $page->seometa();
        },
    ], 

```

I get a Call to undefined method Asset::focusCrop() error.

Any other ideas?

---

<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:** [October 24, 2017, 10:40am UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/5 "2017-10-24T10:40:35Z")

</div>

`focusCrop()` probably doesn’t work with an instance of the Asset class; maybe use standard `crop()` instead (for the asset, this will not have a cropping point anyway). You can still continue using `focusCrop()` for the homepage image.

---

<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:** [October 24, 2017, 10:43am UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/6 "2017-10-24T10:43:04Z")

</div>

I see, well the fallback image i guess doesnt need that, its a fixed image as a placeholder. its only the real ones that need focus crop because they may or may not be art directed.

---

<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:** [October 24, 2017, 10:47am UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/7 "2017-10-24T10:47:35Z")

</div>

```auto
 'image' => function($page) {
          $image = $page->isHomePage()
                      ? site()->feature()->toFile()
                      : $page->feature()->toFile();

          if($image) {
            return $image->focusCrop(1200, 630)->url();        
          } else {
            $image = new Asset('assets/images/fallback.png');
            if($image) {
               return $image->url();
            } else {
               return '';
            }
          }

          
        },

```

---

<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:** [October 24, 2017, 10:52am UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/8 "2017-10-24T10:52:40Z")

</div>

Thanks @texnixe, it looks like it might work but its doing the oppisite of what i want. The field has a value and its deciding to use the fallback regardless. If i switch the if statement round, im back at the old error again with focus crop.

---

<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:** [October 24, 2017, 10:59am UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/9 "2017-10-24T10:59:36Z")

</div>

Does the field have a valid value, i.e. one that returns an image?

---

<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:** [October 24, 2017, 11:01am UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/10 "2017-10-24T11:01:29Z")

</div>

Yup. Lower down i have this code that is unaltered that generates an image for twitter:

```auto
        'image' => function($page) {
          return $page->isHomePage()
          ? site()->shareimage()->toFile()->focusCrop(800,400)->url()
          : $page->shareimage()->toFile()->focusCrop(800,400)->url();
        },

```

the result is using the same field,

```auto
<meta name="twitter:image" content="//localhost:3000/thumbs/shareimagehome-800x400-12-18.png">

```

With your last code in the facebook rule, the result is:

```auto
<meta property="og:image" content="//localhost:3000/assets/images/fallback.png">

```

---

<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:** [October 24, 2017, 11:05am UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/11 "2017-10-24T11:05:42Z")

</div>

> [@jimbobrjames](#):
>
> ‘image’ =\> function($page) {  
> return $page-\>isHomePage()  
> ? site()-\>shareimage()-\>toFile()-\>focusCrop(800,400)-\>url()  
> : $page-\>shareimage()-\>toFile()-\>focusCrop(800,400)-\>url();  
> },

Here the field is called `shareimage`, not `feature`!

---

<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:** [October 24, 2017, 11:06am UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/12 "2017-10-24T11:06:58Z")

</div>

Oh for goodness sake! that was @pedroborges. I copied his example. Sorry, he changed the field name ☹

---

<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:** [October 24, 2017, 11:36am UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/13 "2017-10-24T11:36:18Z")

</div>

Hmmm. Still not working. I set the feild name correctly:

```auto
'image' => function($page) {
         $image = $page->isHomePage()
                     ? site()->shareimage()->toFile()
                     : $page->shareimage()->toFile();
         if($image) {
           return $image->focusCrop(1200, 630)->url();
         } else {
           $image = new Asset('assets/images/fallback.png');
           if($image) {
              return $image->url();
           } else {
              return '';
           }
         }

},

```

But the result is:

```auto
<meta property="og:image" content="//localhost:3000/content/shareimagehome.png">

```

Which i think means the `toFile()` part isnt working.

If i empty the field out, to trigger the fallback, i get the following whoops page:

```auto
Call to a member function focusCrop() on null

```

---

<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:** [October 24, 2017, 12:27pm UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/14 "2017-10-24T12:27:54Z")

</div>

Do you really get the whoops page from this bit of code or because you are trying to get the image from the same field in another part of the code, where you don’t use the check if the image exists?

---

<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:** [October 24, 2017, 1:27pm UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/15 "2017-10-24T13:27:09Z")

</div>

If i understand what you mean correctly, i think so yes, because the other bit of code doesnt use the `$image` variabe

---

<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:** [October 24, 2017, 1:29pm UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/16 "2017-10-24T13:29:53Z")

</div>

> [@jimbobrjames](#):
>
> ‘image’ =\> function($page) {  
> return $page-\>isHomePage()  
> ? site()-\>shareimage()-\>toFile()-\>focusCrop(800,400)-\>url()  
> : $page-\>shareimage()-\>toFile()-\>focusCrop(800,400)-\>url();  
> },

Yes, but in the above code, you are calling the `focusCrop()` method without even checking if the image exists 🙄! And if you deleted the field content - as you said above - then this code will throw an error. So please check which part of your code is causing the error message (Whoops will tell you that).

---

<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:** [October 26, 2017, 2:45pm UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/17 "2017-10-26T14:45:25Z")

</div>

Ok, I am not getting the whoops page any more, but its still not right.

Both the `site.en.txt` and `home.en.txt` file has a value for the `shareimage()` field. It does not seem to be paying any attention to `home.en.txt`, instead its reading the value from `site.en.txt`. If i empty the field in `site.en.txt`, it does fall back to the `fallback.png`. Why is it skipping the current page and going straight to the `site.en.txt` for a value? It should only do that if `home.en.txt` has an empty field.

Heres the code im using

```auto
'image' => function($page) {
   $image = $page->isHomePage()
   ? site()->shareimage()->toFile()
   : $page->shareimage()->toFile();
   if($image) {
     return $image->focusCrop(1200, 630)->url();
   } else {
     $image = new Asset('assets/images/fallback.png');
     if($image) {
        return $image->url();
     } else {
        return '';
     }
   }
},

```

---

<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:** [October 26, 2017, 2:50pm UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/18 "2017-10-26T14:50:37Z")

</div>

The logic is like this:

If the page is the homepage, use the shareimage file from site.txt  
If it is any other page, use the shareimage file from page.txt

That means, home.txt is never used (unless you have changed your homepage to something else and still have a home folder)

---

<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:** [October 26, 2017, 3:04pm UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/19 "2017-10-26T15:04:33Z")

</div>

Duh. Of course. So i got rid of the check for the home page and amended it to this, and its working now. Thanks all.

```auto
'image' => function($page) {
   $image = $page->shareimage()->toFile();
   if($image) {
     return $image->focusCrop(1200, 630)->url();
   } else {
     $image = site()->shareimage()->toFile();
     if($image) {
        return $image->url();
     } else {
        $image = new Asset('assets/images/fallback.png');
        return $image->url();
     }
   }
},

```

---

<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:** [November 23, 2017, 11:50am UTC](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843/20 "2017-11-23T11:50:53Z")

</div>

Ok so now its the future and the [Meta Tags Plugin](https://github.com/pedroborges/kirby-meta-tags) has been updated for Kirby 2.5.7 which has meant some slight changes to the syntax it uses because I [found an issue](https://forum.getkirby.com/t/upgrade-to-2-5-6-2-5-7-cannot-declare-class-asset-because-the-name-is-already-in-use/9020).

Heres the updated version of the fallback above, using the new syntax, in case its useful to somebody.

```auto
// SEO Settings
// Global
c::set('meta-tags.default', function(Page $page, Site $site) {
    return [
        'title' => $page->isHomePage()
          ? $site->seotitle()
          : $page->seotitle().' | '.$site->seotitle(),
          'meta' => [
              'robots' => 'index,follow,noodp',
              'description' => $page->isHomePage()
                ? $site->seometa()
                : $page->seometa(),
              'keywords' => $page->isHomePage()
                ? $site->seokeywords()
                : $page->seokeywords(),
              'robots' => 'index,follow,noodp',
          ],
        'link' => [
            'canonical' => $page->url()
        ],
        'og' => [
            'title' => $page->isHomePage()
                ? $site->seotitle()
                : $page->seotitle().' | '.$site->seotitle(),
            'type' => 'website',
            'site_name' => $site->title(),
            'url' => $page->url(),

            'image' => function($page) {
               $image = $page->shareimage()->toFile();
               if($image) {
                 return $image->focusCrop(1200, 630)->url();
               } else {
                 $image = site()->shareimage()->toFile();
                 if($image) {
                    return $image->url();
                 } else {
                    $image = new Asset('assets/images/fallback.png');
                    return $image->url();
                 }
               }
            },

            'description' => $page->isHomePage()
                ? $site->seometa()
                : $page->seometa().' | '.$site->seotitle(),
        ],
        'twitter' => [
            'title' => $page->isHomePage()
                ? $site->seotitle()
                : $page->seotitle().' | '.$site->seotitle(),
            'card' => 'summary',
            'site' => $site->twitterusername(),
            'creator' => $site->twitterhandle(),
            'image' => function($page) {
               $image = $page->shareimage()->toFile();
               if($image) {
                 return $image->focusCrop(800, 400)->url();
               } else {
                 $image = site()->shareimage()->toFile();
                 if($image) {
                    return $image->url();
                 } else {
                    $image = new Asset('assets/images/fallback.png');
                    return $image->url();
                 }
               }
            },
            'url' => $page->url(),
            'description' => $page->isHomePage()
                ? $site->seometa()
                : $page->seometa().' | '.$site->seotitle(),
        ]
    ];
});

// Specific Pages
c::set('meta-tags.templates', function(Page $page, Site $site) {
    return [
      'newsarticle' => [
          'og' => [
              'type' => 'article',
          ],
          'twitter' => [
              'card' => 'summary_large_image',
              'image' => function($page) {
                 $image = $page->shareimage()->toFile();
                 if($image) {
                   return $image->focusCrop(800, 400)->url();
                 } else {
                   $image = site()->shareimage()->toFile();
                   if($image) {
                      return $image->url();
                   } else {
                      $image = new Asset('assets/images/fallback.png');
                      return $image->url();
                   }
                 }
              },
          ],
      ],
    ];
});
```

[Next page](https://forum.getkirby.com/t/if-check-and-fallback-in-kirby-meta-plugin/8843.md?page=2)
