pepebe
1/9/2015 - 7:56 AM

The filepicker for static resources cannot be restricted to a specific media source. As a workaround, we handle the file with a fileTV and c

The filepicker for static resources cannot be restricted to a specific media source. As a workaround, we handle the file with a fileTV and copy the value of the TV into the content field of the resource on onDocFormSave.

<?php
/*
  updateStaticResource.plugin.php
  author: info@pepebe.de

  The filepicker for static resources cannot be restricted to a specific media source.
  As a workaround, we upload the file with a fileTV 
  and copy the value of the TV into the content field of the resource on onDocFormSave.

  1. Create a media source with an assets path of your choice.
  2. Create a fileTV and set it to the media source created above.
  3. Create a new plugin (updateMediaSource) and set it to onDocFormSave
  4. After confirming that everything runs smoothly, 
     you might want to hide the content field with manager form customition (optional).
  5. Done.
*/

  $fileTV = 3; // add the id of your fileTV here

  switch ($modx->event->name) {
  
      case 'OnDocFormSave':
  
        if($content_type == 8) {
          $id             = $resource->get('id');      
          $pagetitle      = $resource->get('pagetitle');
          $fileTVValue = $resource->getTVValue($fileTV);
          
          $base_path = $modx->getOption('base_path');
          
          if (file_exists($base_path.$fileTVValue)) {
            $resource->set('content', $fileTVValue);
            $resource->save();
            $msg = "[updateStaticResource] Success: ". $pagetitle . "(" . $id . ") updated to" . $fileTVValue;
          }
          else{
            $msg = "[updateStaticResource] Error: ". $pagetitle . "(" . $id . ") could not be updated because " . $fileTVValue . " could not be found.";
          }
        }
      break;
      default:
      break;
  }
  
return $msg;