Skip to content
This repository has been archived by the owner on May 21, 2021. It is now read-only.

[TASK] Adds an example how to add attributes. #1

Open
wants to merge 2 commits into
base: master
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
53 changes: 52 additions & 1 deletion Classes/Domain/Model/FileReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,62 @@ class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference {
*/
protected $uidLocal;

/**
* title
*
* @var string
*/
protected $title;

/**
* description
*
* @var string
*/
protected $description;

/**
* @param \TYPO3\CMS\Core\Resource\FileReference $originalResource
*/
public function setOriginalResource(\TYPO3\CMS\Core\Resource\FileReference $originalResource) {
$this->originalResource = $originalResource;
$this->uidLocal = $originalResource->getOriginalFile()->getUid();
}
}

/**
* Set title
*
* @param string $title
*/
public function setTitle($title) {
$this->title = $title;
}

/**
* Get title
*
* @return string
*/
public function getTitle() {
return $this->title;
}

/**
* Set description
*
* @param string $description
*/
public function setDescription($description) {
$this->description = $description;
}

/**
* Get description
*
* @return string
*/
public function getDescription() {
return $this->description;
}

}
28 changes: 18 additions & 10 deletions Classes/Property/TypeConverter/UploadedFileReferenceConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ public function convertFrom($source, $targetType, array $convertedChildPropertie

if ($source['error'] !== \UPLOAD_ERR_OK) {
switch ($source['error']) {
case \UPLOAD_ERR_INI_SIZE:
case \UPLOAD_ERR_FORM_SIZE:
case \UPLOAD_ERR_PARTIAL:
return new \TYPO3\CMS\Extbase\Error\Error(\TYPO3\Flow\Utility\Files::getUploadErrorMessage($source['error']), 1264440823);
default:
return new \TYPO3\CMS\Extbase\Error\Error('An error occurred while uploading. Please try again or contact the administrator if the problem remains', 1340193849);
case \UPLOAD_ERR_INI_SIZE:
case \UPLOAD_ERR_FORM_SIZE:
case \UPLOAD_ERR_PARTIAL:
return new \TYPO3\CMS\Extbase\Error\Error(\TYPO3\Flow\Utility\Files::getUploadErrorMessage($source['error']), 1264440823);
default:
return new \TYPO3\CMS\Extbase\Error\Error('An error occurred while uploading. Please try again or contact the administrator if the problem remains', 1340193849);
}
}

Expand Down Expand Up @@ -194,7 +194,7 @@ protected function importUploadedResource(array $uploadInfo, PropertyMappingConf
$uploadFolder = $this->resourceFactory->retrieveFileOrFolderObject($uploadFolderId);
$uploadedFile = $uploadFolder->addUploadedFile($uploadInfo, $conflictMode);

$fileReferenceModel = $this->createFileRefrenceFromFalFileObject($uploadedFile);
$fileReferenceModel = $this->createFileRefrenceFromFalFileObject($uploadedFile, $uploadInfo);

if (isset($uploadInfo['submittedFile']['resourcePointer']) && $replaceResource) {
$this->removeFileReference($this->hashService->validateAndStripHmac($uploadInfo['submittedFile']['resourcePointer']));
Expand Down Expand Up @@ -222,28 +222,36 @@ protected function getDatabaseConnection() {

/**
* @param File $file
* @param array $additionalAttributes
* @return \Helhum\UploadExample\Domain\Model\FileReference
*/
protected function createFileRefrenceFromFalFileObject(File $file) {
protected function createFileRefrenceFromFalFileObject(File $file, $additionalAttributes) {
$fileReference = $this->resourceFactory->createFileReferenceObject(
array(
'uid_local' => $file->getUid(),
'uid_foreign' => uniqid('NEW_'),
'uid' => uniqid('NEW_'),
)
);
return $this->createFileReferenceFromFalFileReferenceObject($fileReference);
return $this->createFileReferenceFromFalFileReferenceObject($fileReference, $additionalAttributes);
}

/**
* @param FileReference $fileReference
* @param array $additionalAttributes
* @return \Helhum\UploadExample\Domain\Model\FileReference
*/
protected function createFileReferenceFromFalFileReferenceObject(FileReference $fileReference) {
protected function createFileReferenceFromFalFileReferenceObject(FileReference $fileReference, $additionalAttributes) {
/** @var $fileReferenceModel \Helhum\UploadExample\Domain\Model\FileReference */
$fileReferenceModel = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Domain\\Model\\FileReference');
$fileReferenceModel->setOriginalResource($fileReference);

$title = trim($additionalAttributes['title']);
$fileReferenceModel->setDescription($title === '' ? NULL : $title);

$description = trim($additionalAttributes['description']);
$fileReferenceModel->setDescription($description === '' ? NULL : $description);

return $fileReferenceModel;
}
}
23 changes: 11 additions & 12 deletions Resources/Private/Partials/Example/FormFields.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@
<label for="title">
<f:translate key="tx_uploadexample_domain_model_example.title" /> <span class="required">(required)</span>
</label><br />
<f:form.textfield property="title" /><br />
<f:form.textfield property="title" /><br />

<label for="image">
<f:translate key="tx_uploadexample_domain_model_example.image" />
</label><br />
Image: <h:form.upload property="image" /> <br>
Additional image title: <f:form.textfield property="image.title" /> <br>
Additional image description: <f:form.textarea property="image.description" cols="30" rows="4"></f:form.textarea>

<hr>

<h:form.upload property="image" >
<f:if condition="{resource}">
<f:image image="{resource}" alt="" width="50"/>
</f:if>
</h:form.upload><br />
<label for="image_collection">
<f:translate key="tx_uploadexample_domain_model_example.image_collection" />
</label><br />
<h:form.upload property="imageCollection.0">
<f:if condition="{resource}">
<f:image image="{resource}" alt="" width="50"/>
</f:if>
</h:form.upload>
<br />
Image <h:form.upload property="imageCollection.0" /> <br>
Additional image title: <f:form.textfield property="imageCollection.0.title" /><br>
Additional image description: <f:form.textarea property="imageCollection.0.description" cols="30" rows="4"></f:form.textarea>
<br />
10 changes: 8 additions & 2 deletions Resources/Private/Partials/Example/Properties.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
<f:translate key="tx_uploadexample_domain_model_example.image" />
</td>
<td>
<f:if condition="{example.image}"><f:image image="{example.image}" alt="" width="200"/></f:if>
<f:if condition="{example.image}"><f:image image="{example.image}" alt="" title="{example.image.title}" width="200"/></f:if>
<p style="font-style: italic;">
{example.image.description}
</p>
</td>
</tr>
<tr>
Expand All @@ -22,7 +25,10 @@
</td>
<td>
<f:for each="{example.imageCollection}" as="image">
<f:image image="{image}" alt="" width="200"/>
<f:image image="{image}" alt="" title="{image.title}" width="200"/>
<p style="font-style: italic;">
{image.description}
</p>
</f:for>
</td>
</tr>
Expand Down