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

bie*_*ior 7 file-upload typo3 extbase typo3-6.2.x fal

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

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

我目前的方法如下:

/**
 * @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');
}
Run Code Online (Sandbox Code Playgroud)

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

$animal->addPhoto($fileObject);
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

已检查:DataHandler方法(链接)也不起作用,因为它对FE用户不可用.

TL; DR

如何从现有(刚刚创建的)FAL记录添加FileReferenceAnimal模型?

Jos*_*ost 13

你需要做几件事.关于伪造的这个问题是我获取信息的地方,而且一些东西是从Helmut Hummels前端上传示例(以及附带的博客帖子)中取出的,@ derhansen已经评论过了.

我不完全确定这是否是你需要的一切,所以随意添加东西.这不使用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中的field($uidLocal),所以不起作用.您需要从extbase模型继承,添加该字段并填充它.不要忘记在TypoScript中添加映射以将您自己的模型映射到sys_file_reference.

    config.tx_extbase.persistence.classes.Zoo\Zoo\Domain\Model\FileReference.mapping.tableName = sys_file_reference
    
    Run Code Online (Sandbox Code Playgroud)

    该类看起来像这样(取自伪造问题):

     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();
          }
      }
    
    Run Code Online (Sandbox Code Playgroud)
  • 将其添加到配置部分中的图像字段的TCA(当然适应您的表和字段名称):

    'foreign_match_fields' => array(
        'fieldname' => 'photo',
        'tablenames' => 'tx_zoo_domain_model_animal',
        'table_local' => 'sys_file',
    ),
    
    Run Code Online (Sandbox Code Playgroud)
  • 编辑:\TYPO3\CMS\Extbase\Domain\Model\FileReference如果在TYPO3 6.2.11或7.2或更高版本上,请在此步骤中使用.

    所以最后添加创建$fileRef而不是$fileObject

    $fileRef = GeneralUtility::makeInstance('\Zoo\Zoo\Domain\Model\FileReference');
    $fileRef->setOriginalResource($fileObject);
    
    $animal->addPhoto($fileRef);
    
    Run Code Online (Sandbox Code Playgroud)
  • 不要告诉任何人你做了什么.