Prevent Error If Fileset is Deleted

Permalink
I have been stuck on this one for a long time. I am building a block that works with file sets. If the file set is deleted, I get a "Call to a member function getFileSetID() on a non-object" error. See attached.

What do I need to prevent an error from being thrown if there are no filesets, or the chosen fileset was deleted?

1 Attachment

PineCreativeLabs
 
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
Hi PineCreativeLabs,

Often adding a check to see if something is an object first will address this.
http://php.net/manual/en/function.is-object.php...
PineCreativeLabs replied on at Permalink Reply
PineCreativeLabs
I'm not clear on this. What is supposed to be checked if it is an object?

I tried using this on $fs and $fsID, but the error is unchanged.

If it helps, I am only seeing the error when signed in.
Gondwana replied on at Permalink Reply
Gondwana
I'd be guessing $fs. You could probably just see if it's null.

If worst comes to worst, could you just catch the exception?
MrKDilkington replied on at Permalink Reply
MrKDilkington
@PineCreativeLabs

Please include the code that is throwing errors.
PineCreativeLabs replied on at Permalink Reply
PineCreativeLabs
Here is what I have in the block controller for the view function:
public function view(){
      $sets = FileSet::getMySets();
      $this->set("sets",$sets);
         $fs = FileSet::getByID($fsID);
         $bID = $this->bID;
         $db = Database::connection();
         $existingThumbs = $db->fetchAll('SELECT * from btEzModalGallery WHERE bID = ? ORDER BY sort', array($bID)); //gives us all the files we've already saved/sorted
         $existingThumbIDs = array();
         foreach($existingThumbs as $thumb){
            $existingThumbIDs[] = $thumb['fID'];
         }
         $fileList = new FileList();
         $fileList->filterBySet($fs);
         $fileList->filterByType(FileType::T_IMAGE);
         $fileList->sortByFileSetDisplayOrder();


Here is what I have for the view:
<?php if (empty($items)){ ?><p><?php echo t('Empty file set')?></p><?php } ?>
<?php if (empty($sets)){ ?><p><?php t('No filesets')?></p><?php } else {
   foreach($items as $item){            
      $fileObj = File::getByID($item['fID']);
      $ih = Core::make("helper/image");
      $thumb = $ih->getThumbnail($fileObj,$thumbsize,$thumbsize,true);
      if(is_object($fileObj)){ ?>
         <a href="<?php echo $fileObj->getURL(); ?>" rel="gallery-<?php echo $bID ?>" class="modaal-<?php echo $bID ?>"><img src="<?php echo $thumb->src?>" /></a>
   <?php } }  ?>