-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sharmasahil
committed
Jan 29, 2025
1 parent
f17c7b8
commit f3c3bb5
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
modules/tide_search/modules/tide_data_pipeline/src/Source/Resource/PrivateFile.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\tide_data_pipeline\Source\Resource; | ||
|
||
use Drupal\Core\Field\BaseFieldDefinition; | ||
use Drupal\Core\StringTranslation\TranslatableMarkup; | ||
use Drupal\data_pipelines\Entity\DatasetInterface; | ||
use Drupal\data_pipelines\Source\Resource\SourceResourceBase; | ||
use Drupal\file\FileInterface; | ||
|
||
/** | ||
* A class for the Privatefile. | ||
*/ | ||
final class PrivateFile extends SourceResourceBase { | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getResource(DatasetInterface $dataset, string $field_name) { | ||
if (!empty($file = self::getFieldValue($dataset, $field_name, 'entity'))) { | ||
assert($file instanceof FileInterface); | ||
|
||
// Check if the file is stored in the private file system. | ||
if ($file->getFileUri()) { | ||
// Ensure the file is part of the private files folder. | ||
$file_uri = $file->getFileUri(); | ||
if (strpos($file_uri, 'private://') === 0) { | ||
return fopen($file_uri, 'r'); | ||
} | ||
} | ||
} | ||
return NULL; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getResourceBaseFieldDefinition(array $source_plugin_definition): BaseFieldDefinition { | ||
// Create the field definition for the file. | ||
$field_definition = BaseFieldDefinition::create('file') | ||
->setLabel(new TranslatableMarkup('Private File')) | ||
->setDescription(new TranslatableMarkup('The file for the dataset.')) | ||
->setSetting('file_extensions', $source_plugin_definition['id']) | ||
->setRequired(TRUE) | ||
->setDisplayOptions('form', [ | ||
'type' => 'file_generic', | ||
]); | ||
|
||
// Set the upload location to the private directory. | ||
$field_definition->setSetting('file_directory', 'private://'); | ||
|
||
return $field_definition; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
modules/tide_search/modules/tide_data_pipeline/tide_data_pipeline.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
tide_data_pipeline.source_resource.privatefile: | ||
class: Drupal\tide_data_pipeline\Source\Resource\PrivateFile | ||
tags: | ||
- { name: data_pipelines_source_resource } |