TYPO3 6.2 - how to create FileReference in frontend (FE)?

限于喜欢 提交于 2019-12-03 13:37:32

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.

Here is the complete function to upload file in TYPO3 using FAL and create filereference

/**
 * 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, []);
}   

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

And

$obj = //Object of your model for which you are creating file reference

This example does not deserve a beauty prize but it might help you. It works in 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 ));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!