title: Find if featured image on page has changed
body: <p>While looking into <a href="https://stackoverflow.com/questions/38109766/find-whether-an-image-is-used-anywhere">this question</a> I came up with the following solution that is called from <code>canDelete()</code> in an extension to <code>File</code>:</p>
  
  <pre><code>protected function isFileInUse()
  {
      $owner = $this-&gt;getOwner();
      $dataObjectSubClasses = ClassInfo::subclassesFor('DataObject');
      $classesWithFileHasOne = [];
      foreach ($dataObjectSubClasses as $subClass) {
          $hasOnes = array_flip($subClass::create()-&gt;hasOne());
          if (array_key_exists($owner-&gt;class, $hasOnes)) {
              $classesWithFileHasOne[$subClass] = $hasOnes[$owner-&gt;class];
          }
      }
  
      $threshold = (Director::get_current_page()-&gt;class == 'AssetAdmin') ? 1 : 2;
      $uses = 0;
      foreach ($classesWithFileHasOne as $class =&gt; $relation) {
          $uses += count($class::get()-&gt;filter("{$relation}ID", $this-&gt;owner-&gt;ID));
          if ($uses &gt;= $threshold) {
              return true;
          }
      }
  
      return false;
  }
  </code></pre>
  
  <p>There is one edge case I can't get around though. If, say, a featured image is changed on a blog post then if there is exactly one other use of the same image then with this approach it will still allow it to be deleted. This is because until the page is saved the current change doesn't count towards uses of  the image.</p>
  
  <p>The threshold is set differently in CMS Pages and the Media Manager to allow an image to be deleted from within the page that is using it.</p>
  
  <p>Is there a way that I can access the containing page (or other element - we're using <a href="https://github.com/dnadesign/silverstripe-elemental" rel="nofollow noreferrer">Elemental</a>) from within my File extension to see if its associated image has changed?</p>