Skip to content

Commit

Permalink
Update publications code to use dynamic media directory accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Feb 9, 2023
1 parent dc92431 commit 39bd25e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Publications\Commands;

use Closure;
use Hyde\Hyde;
use Hyde\Publications\Actions\CreatesNewPublicationPage;
use Hyde\Publications\Commands\Helpers\InputStreamHandler;
use Hyde\Publications\Models\PublicationFieldDefinition;
Expand Down Expand Up @@ -146,7 +147,7 @@ protected function captureMediaFieldInput(PublicationFieldDefinition $field): ?P

$mediaFiles = PublicationService::getMediaForPubType($this->publicationType);
if ($mediaFiles->isEmpty()) {
return $this->handleEmptyOptionsCollection($field, 'media file', "No media files found in directory _media/{$this->publicationType->getIdentifier()}/");
return $this->handleEmptyOptionsCollection($field, 'media file', "No media files found in directory ".Hyde::getMediaDirectory()."/{$this->publicationType->getIdentifier()}/");
}

return new PublicationFieldValue(PublicationFieldTypes::Media, $this->choice('Which file would you like to use?', $mediaFiles->toArray()));
Expand Down
2 changes: 1 addition & 1 deletion packages/publications/src/PublicationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ protected static function getPublicationFiles(string $directory): array

protected static function getMediaFiles(string $directory, string $extensions = '{jpg,jpeg,png,gif,pdf}'): array
{
return glob(Hyde::path("_media/$directory/*.$extensions"), GLOB_BRACE);
return glob(Hyde::mediaPath("$directory/*.$extensions"), GLOB_BRACE);
}
}
17 changes: 17 additions & 0 deletions packages/publications/tests/Feature/PublicationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,23 @@ public function testGetMediaForPubTypeWithMedia()
File::deleteDirectory(Hyde::path('_media/test-publication'));
}

public function testGetMediaForPubTypeWithCustomMediaDirectory()
{
Hyde::setMediaDirectory('_assets');
$this->createPublicationType();
mkdir(Hyde::path('_assets/test-publication'), recursive: true);
file_put_contents(Hyde::path('_assets/test-publication/image.png'), '');

$this->assertEquals(
new Collection([
'_assets/test-publication/image.png',
]),
PublicationService::getMediaForPubType(PublicationType::get('test-publication'))
);

File::deleteDirectory(Hyde::path('_assets/test-publication'));
}

public function testParsePublicationFile()
{
$this->createPublicationType();
Expand Down

0 comments on commit 39bd25e

Please sign in to comment.