Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templates: better support cross-format templates #4463

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/Listener/FileCreatedFromTemplateListener.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stuff I don't really get, when I tried running it and creating a new file it doesn't quite work properly since the template service fillFields method expects a file to be handed to it; with these changes there isn't one. Can you explain a bit more the reasoning behind this or why it would be necessary? I don't completely understand

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I shouldn't have moved the empty template code path, thanks.

Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ public function handle(Event $event): void {
return;
}

if ($this->templateManager->isSupportedTemplateSource($templateFile->getExtension())) {
// Only use TemplateSource if supported filetype
$this->templateManager->setTemplateSource($event->getTarget()->getId(), $templateFile->getId());
}

if ($this->capabilitiesService->hasFormFilling()) {
try {
$filledTemplate = $this->templateFieldService->fillFields($templateFile, $event->getTemplateFields());
$filledTemplate = $this->templateFieldService->fillFields($templateFile, $event->getTemplateFields(), null, $event->getTarget()->getExtension());
$event->getTarget()->putContent($filledTemplate);
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
} else {
if ($this->templateManager->isSupportedTemplateSource($templateFile->getExtension())) {
// Only use TemplateSource if supported filetype
$this->templateManager->setTemplateSource($event->getTarget()->getId(), $templateFile->getId());
}
}

// Avoid having the mimetype of the source file set
Expand Down
9 changes: 1 addition & 8 deletions lib/Service/TemplateFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function fillFields(Node|int $file, array $fields = [], ?string $destinat

$formFormat = [
'name' => 'format',
'contents' => $file->getExtension(),
'contents' => $format === null ? $file->getExtension() : $format,
];

$form = RemoteOptionsService::getDefaultOptions();
Expand All @@ -191,13 +191,6 @@ public function fillFields(Node|int $file, array $fields = [], ?string $destinat

$content = $response->getBody();

if ($format !== null) {
$tmp = $this->tempManager->getTemporaryFile();
file_put_contents($tmp, $content);
$fp = fopen($tmp, 'rb');
$content = $this->remoteService->convertTo($file->getName(), $fp, $format);
}

if ($destination !== null) {
$this->writeToDestination($destination, $content);
}
Expand Down