本文介绍了TYPO3 6.2 - 如何在前端(FE)创建FileReference?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有假设的动物园扩展,其中动物模型与照片字段和FrontEnd(FE)插件具有典型的CRUD操作。 照片字段是典型的FAL的 FileReference ,它可以在后端(BE)和普通的TCA IRRE配置下正常工作。



我可以成功将文件上传到存储设备,它在 Filelist 模块中可见,我可以在我的动物编辑过程中使用它,无论如何,我不能在我的FE插件中创建 FileReference 。



我目前的方法是这样的: p>

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

//从上传的`photo` form's $ _FILES
$ file = $ this-> getFromFILES('tx_zoo_animal','photo');
$ b $ if 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:使目标存储可配置

//这将上传的文件完全添加到存储中
$ fileObject = $ storage-> addFile($ file ['tmp_name'],$ storage - > getRootLevelFolder(),$ file ['name']);

//这里我卡住了......下面的行不起作用(抛出异常1:/)
//这是因为$ fileObject是FileInterface的类型,FileReference是必需的
$ animal-> addPhoto($ fileObject);

}

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

试图通过这行创建引用抛出异常:

  $ animal-> addPhoto($ fileObject); 

我该如何解决这个问题?

检查: DataHandler 方法()也不起作用,因为FE用户不可用。

TL; DR

如何添加 FileReference 到动物现有模型(刚刚创建的)FAL记录?

解决方案

你需要做几件事情。这个是我得到信息的地方,有些东西是从(和

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



您需要:


  • 从File对象创建一个FAL文件引用对象。这可以使用FALs资源工厂来完成。

  • 将它包装在 \TYPO3\CMS\Extbase\Domain\Model\FileReference
  • code>(方法 - > setOriginalResource )
  • 编辑: TYPO3 6.2.11和7.2是不必要的,你可以直接使用类 \TYPO3\CMS\Extbase\Domain\Model\FileReference 。



    但是,由于extbase模型在6.2.10rc1中未命中某个字段( $ uidLocal ), 。您需要从extbase模型继承,添加该字段并填充它。不要忘记在TypoScript中添加一个映射,以将自己的模型映射到 sys_file_reference 。

      config.tx_extbase.persistence.classes.Zoo\Zoo\Domain\Model\FileReference.mapping.tableName = sys_file_reference 

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

      class FileReference扩展\TYPO3\CMS\Extbase\Domain\Model\FileReference {
    $ b $ ** / **
    *我们需要这个属性,以便Extbase持久性可以正确地持久化对象
    *
    * @var整数
    * /
    保护$ uidLocal;
    $ b $ **
    * @param \TYPO3\CMS\Core\Resource\ResourceInterface $ originalResource
    * /
    public function setOriginalResource(\\ \\ TYPO3 \CMS\Core\Resource\ResourceInterface $ originalResource){
    $ this-> originalResource = $ originalResource;
    $ this-> uidLocal =(int)$ originalResource-> getUid();


    code $
    $添加到图像的TCA中字段,在配置部分(适应你的表和字段名称当然):

     'foreign_match_fields'=> array $(
    'fieldname'=>'photo',
    'tablenames'=>'tx_zoo_domain_model_animal',
    'table_local'=>'sys_file',
    ),


  • 编辑:使用 \ 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);


  • 不要告诉任何人你做了什么。



I have the hypothetical Zoo extension in which I've Animal model with photo field and FrontEnd (FE) plugin with typical CRUD actions. photo field is typical FAL's FileReference and it works perfectly in backend (BE) with common TCA IRRE config.

I'm able to successful upload the file to the storage, it's visible in the Filelist module, and I can use it in BE during my Animal editing, anyway I can't create FileReference within my FE plugin.

My current approach looks like this:

/**
 * @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');
}

anyway attempt to create reference by this line throws exception:

$animal->addPhoto($fileObject);

How can I resolve this?

Checked: DataHandler approach (link) won't work also, as it's unavailable for FE users.

TL;DR

How to add FileReference to Animal model from existing (just created) FAL record?

解决方案

You need to do several things. This issue on forge is where I got the info, and some stuff is taken out of Helmut Hummels frontend upload example (and the accompanying blogpost) which @derhansen already commented.

I'm not entirely sure if this is everything you need, so feel free to add things. This does not use a TypeConverter, which you should probably do. That would open further possibilities, for example it would be easily possible to implement deletion and replacement of file references.

You need to:

  • Create a FAL file reference object from the File object. This can be done using FALs resource factory.
  • Wrap it in a \TYPO3\CMS\Extbase\Domain\Model\FileReference (method ->setOriginalResource)
  • EDIT: This step is unnecessary as of TYPO3 6.2.11 and 7.2, you can directly use the class \TYPO3\CMS\Extbase\Domain\Model\FileReference.

    But, because the extbase model misses a field ($uidLocal) in 6.2.10rc1, that won't work. You need to inherit from the extbase model, add that field, and fill it. Don't forget to add a mapping in TypoScript to map your own model to sys_file_reference.

    config.tx_extbase.persistence.classes.Zoo\Zoo\Domain\Model\FileReference.mapping.tableName = sys_file_reference
    

    The class would look like this (taken from the forge issue):

     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();
          }
      }
    

  • Add this to the TCA of the image field, in the config-section (adapt to your table and field names of course):

    'foreign_match_fields' => array(
        'fieldname' => 'photo',
        'tablenames' => 'tx_zoo_domain_model_animal',
        'table_local' => 'sys_file',
    ),
    

  • EDIT: Use \TYPO3\CMS\Extbase\Domain\Model\FileReference in this step if on TYPO3 6.2.11 or 7.2 or above.

    So at the end add the created $fileRef instead of $fileObject

    $fileRef = GeneralUtility::makeInstance('\Zoo\Zoo\Domain\Model\FileReference');
    $fileRef->setOriginalResource($fileObject);
    
    $animal->addPhoto($fileRef);
    

  • Don't tell anyone what you have done.

这篇关于TYPO3 6.2 - 如何在前端(FE)创建FileReference?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 19:07