TYPO3 6.2 - 如何在前端(FE)中创建FileReference?

问题描述 投票:7回答:3

我有假设的Zoo扩展,其中我使用Animal字段和具有典型CRUD操作的FrontEnd(FE)插件的photo模型。 photo字段是典型的FAL的FileReference,它在后端(BE)中使用常见的TCA IRRE配置完美地工作。

我能够成功将文件上传到存储,它在Filelist模块中可见,我可以在动物编辑期间在BE中使用它,无论如何我无法在我的FE插件中创建FileReference

我目前的方法如下:

/**
 * @param \Zoo\Zoo\Domain\Model\Animal $animal
 */
public function updateAction(\Zoo\Zoo\Domain\Model\Animal $animal) {

    // It reads proper uploaded `photo` from form's $_FILES
    $file = $this->getFromFILES('tx_zoo_animal', 'photo');

    if ($file && is_array($file) && $file['error'] == 0) {

        /** @type  $storageRepository \TYPO3\CMS\Core\Resource\StorageRepository */
        $storageRepository = GeneralUtility::makeInstance('\TYPO3\CMS\Core\Resource\StorageRepository');
        $storage = $storageRepository->findByUid(5); // TODO: make target storage configurable

        // This adds uploaded file to the storage perfectly
        $fileObject = $storage->addFile($file['tmp_name'], $storage->getRootLevelFolder(), $file['name']);

        // Here I stuck... below line doesn't work (throws Exception no. 1 :/)
        // It's 'cause $fileObject is type of FileInterface and FileReference is required
        $animal->addPhoto($fileObject);

    }

    $this->animalRepository->update($animal);
    $this->redirect('list');
}

无论如何,尝试通过此行创建引用会抛出异常:

$animal->addPhoto($fileObject);

我该如何解决这个问题?

检查:DataHandler方法(link)也不起作用,因为它不适用于FE用户。

TL; DR

如何从现有(刚刚创建的)FAL记录中将FileReference添加到Animal模型?

file-upload typo3 extbase typo3-6.2.x fal
3个回答
13
投票

你需要做几件事。这个issue on forge是我得到信息的地方,有些东西是从@dhansen已经评论过的Helmut Hummels frontend upload example(和accompanying blogpost)中取出来的。

我不完全确定这是否是你需要的一切,所以随意添加东西。这不使用TypeConverter,你可能应该这样做。这将打开更多的可能性,例如,很容易实现删除和替换文件引用。

你需要:

  • 从File对象创建FAL文件引用对象。这可以使用FALs资源工厂完成。
  • \TYPO3\CMS\Extbase\Domain\Model\FileReference包裹它(方法->setOriginalResource
  • 编辑:从TYPO3 6.2.11和7.2开始,此步骤是不必要的,您可以直接使用类\TYPO3\CMS\Extbase\Domain\Model\FileReference。 但是,因为extbase模型在6.2.10rc1中错过了一个字段($uidLocal),所以这不起作用。您需要从extbase模型继承,添加该字段并填充它。不要忘记在TypoScript中添加映射以将您自己的模型映射到sys_file_referenceconfig.tx_extbase.persistence.classes.Zoo\Zoo\Domain\Model\FileReference.mapping.tableName = sys_file_reference 该类看起来像这样(取自伪造问题): class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference { /** * We need this property so that the Extbase persistence can properly persist the object * * @var integer */ protected $uidLocal; /** * @param \TYPO3\CMS\Core\Resource\ResourceInterface $originalResource */ public function setOriginalResource(\TYPO3\CMS\Core\Resource\ResourceInterface $originalResource) { $this->originalResource = $originalResource; $this->uidLocal = (int)$originalResource->getUid(); } }
  • 将其添加到配置部分中的图像字段的TCA(当然适应您的表和字段名称): 'foreign_match_fields' => array( 'fieldname' => 'photo', 'tablenames' => 'tx_zoo_domain_model_animal', 'table_local' => 'sys_file', ),
  • 编辑:如果在TYPO3 6.2.11或7.2或更高版本上,请在此步骤中使用\TYPO3\CMS\Extbase\Domain\Model\FileReference。 所以在最后添加创建的$fileRef而不是$fileObject $fileRef = GeneralUtility::makeInstance('\Zoo\Zoo\Domain\Model\FileReference'); $fileRef->setOriginalResource($fileObject); $animal->addPhoto($fileRef);
  • 不要告诉任何人你做了什么。

0
投票

以下是使用FAL在TYPO3中上传文件并创建文件引用的完整功能

/**
 * Function to upload file and create file reference
 *
 * @var array $fileData
 * @var mixed $obj foreing model object
 *
 * @return void
 */
private function uploadAndCreateFileReference($fileData, $obj) {
    $storageUid = 2;
    $resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();

    //Adding file to storage
    $storage = $resourceFactory->getStorageObject($storageUid);
    if (!is_object($storage)) {
        $storage = $resourceFactory->getDefaultStorage();
    }

    $file = $storage->addFile(
          $fileData['tmp_name'],
          $storage->getRootLevelFolder(),
          $fileData['name']
    );


    //Creating file reference
    $newId = uniqid('NEW_');
    $data = [];
    $data['sys_file_reference'][$newId] = [
        'table_local' => 'sys_file',
        'uid_local' => $file->getUid(),
        'tablenames' => 'tx_imageupload_domain_model_upload', //foreign table name
        'uid_foreign' => $obj->getUid(),
        'fieldname' => 'image', //field name of foreign table
        'pid' => $obj->getPid(),
    ];
    $data['tx_imageupload_domain_model_upload'][$obj->getUid()] = [
        'image' => $newId,
    ];

    $dataHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
        'TYPO3\CMS\Core\DataHandling\DataHandler'
    );
    $dataHandler->start($data, []);
}   

其中$ filedata = $ this-> request-> getArgument('file_input_field_name');

$ obj = //要为其创建文件引用的模型的对象


0
投票

这个例子不值得美容奖,但它可能对你有所帮助。它适用于7.6.x.

private function uploadLogo(){

   $file['name']    = $_FILES['logo']['name'];
   $file['type']    = $_FILES['logo']['type'];
   $file['tmp_name']  = $_FILES['logo']['tmp_name'];
   $file['size']    = $_FILES['logo']['size'];

   // Store the image
   $resourceFactory = \TYPO3\CMS\Core\Resource\ResourceFactory::getInstance();
   $storage = $resourceFactory->getDefaultStorage();

   $saveFolder = $storage->getFolder('logo-companies/');
   $newFile = $storage->addFile(
     $file['tmp_name'],
     $saveFolder,
     $file['name']
   );

   // remove earlier refereces
   $GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_file_reference', 'uid_foreign = '. $this->getCurrentUserCompanyID());

   $addressRecord = $this->getUserCompanyAddressRecord();

   // Create new reference
   $data = array(
     'table_local' => 'sys_file',
     'uid_local' => $newFile->getUid(),
     'tablenames' => 'tt_address',
     'uid_foreign' => $addressRecord['uid'],
     'fieldname' => 'image',
     'pid' => $addressRecord['pid']
   );

   $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_file_reference', $data);
   $newId = $GLOBALS['TYPO3_DB']->sql_insert_id();

   $where = "tt_address.uid = ".$addressRecord['uid'];
   $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tt_address', $where, array('image' => $newId ));
}
© www.soinside.com 2019 - 2024. All rights reserved.