Extbase - File Abstraction Layer (FAL): Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
 
(4 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
 +
== Links ==
 +
https://wiki.typo3.org/File_Abstraction_Layer
 +
 +
[[Extbase - Datei Upload ohne FAL]]
 +
 +
 
== Allgemein Dateien mit Extbase Handeln ==
 
== Allgemein Dateien mit Extbase Handeln ==
 
 
In general, you should be able to call getOriginalResource() on a \TYPO3\CMS\Extbase\Domain\Model\FileReference object. For more concrete examples, either refer to the doc links or have a look at the wiki for a example handling FileReferences.
 
In general, you should be able to call getOriginalResource() on a \TYPO3\CMS\Extbase\Domain\Model\FileReference object. For more concrete examples, either refer to the doc links or have a look at the wiki for a example handling FileReferences.
 
== Beispiele ==
 
== Beispiele ==
Zeile 9: Zeile 14:
 
=== Beispiel File Referenz dynamisch erzeugen ===
 
=== Beispiel File Referenz dynamisch erzeugen ===
 
http://www.typo3tiger.de/blog/post/extbase-fal-filereference-im-controller-erzeugen.html
 
http://www.typo3tiger.de/blog/post/extbase-fal-filereference-im-controller-erzeugen.html
 +
 +
Dynamisch bedeutet hier, dass nach dem Upload dynamisch eine FAL FileReference zu einem Objekt im Controller erzeugt wird. Somit kann man die neue Datei auch gleich einem vorhandenen oder neu erstellten Objekt zuordnen.
 +
 +
Allgemeines Vorgehen:
 +
* Erweitern der Klasse '''FileReference'''
 +
* '''TCA''' Konfiguration vervollständigen
 +
* '''Parameter''' an TCA übergeben (Methode getFileFieldTCAConfig mit dem Parameter foreign_match_fields
 +
 +
Achtung: Grund für so manche Verwirrung ist, dass TYPO3 zwei unterschiedliche FileReference-Klassen liefert:
 +
Das Extbase Model ''\TYPO3\CMS\Extbase\Domain\Model\FileReference'' sowie das Core Model ''\TYPO3\CMS\Core\Resource\FileReference''
 +
 +
 +
'''Extbase Model (FileReference)'''
 +
<syntaxhighlight lang="php">
 +
 +
namespace Vendor\Ext\Domain\Model;
 +
 +
/**
 +
* Class FileReference
 +
*/
 +
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference {
 +
 +
    /**
 +
    * uid of a sys_file
 +
    *
 +
    * @var integer
 +
    */
 +
    protected $originalFileIdentifier;
 +
 +
    /**
 +
    * setOriginalResource
 +
    *
 +
    * @param \TYPO3\CMS\Core\Resource\FileReference $originalResource
 +
    * @return void
 +
    */
 +
    public function setOriginalResource(\TYPO3\CMS\Core\Resource\FileReference $originalResource) {
 +
        $this->originalResource = $originalResource;
 +
        $this->originalFileIdentifier = (int)$originalResource->getOriginalFile()->getUid();
 +
    }
 +
 +
    /**
 +
    * setFile
 +
    *
 +
    * @param \TYPO3\CMS\Core\Resource\File $falFile
 +
    * @return void
 +
    */
 +
    public function setFile(\TYPO3\CMS\Core\Resource\File $falFile) {
 +
        $this->originalFileIdentifier = (int)$falFile->getUid();
 +
    }
 +
 +
}
 +
</syntaxhighlight>
 +
'''Extbase Model (Person)'''
 +
<syntaxhighlight lang="php">
 +
namespace Vendor\Ext\Domain\Model;
 +
 +
/**
 +
* Class Person
 +
*/
 +
class Person extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
 +
 +
    /**
 +
    * title
 +
    *
 +
    * @var string
 +
    */
 +
    protected $title = '';
 +
 +
    /**
 +
    * image
 +
    *
 +
    * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
 +
    */
 +
    protected $image = NULL;
 +
 +
    /**
 +
    * Returns the title
 +
    *
 +
    * @return string $title
 +
    */
 +
    public function getTitle() {
 +
        return $this->title;
 +
    }
 +
 +
    /**
 +
    * Sets the title
 +
    *
 +
    * @param string $title
 +
    * @return void
 +
    */
 +
    public function setTitle($title) {
 +
        $this->title = $title;
 +
    }
 +
 +
    /**
 +
    * Returns the image
 +
    *
 +
    * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
 +
    */
 +
    public function getImage() {
 +
        return $this->image;
 +
    }
 +
 +
    /**
 +
    * Sets the image
 +
    *
 +
    * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
 +
    * @return void
 +
    */
 +
    public function setImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $image) {
 +
        $this->image = $image;
 +
    }
 +
 +
}
 +
</syntaxhighlight>
 +
'''Extbase Controller (Person)'''
 +
<syntaxhighlight lang="php">
 +
 +
namespace Vendor\Ext\Domain\Model;
 +
 +
/**
 +
* Class PersonController
 +
*/
 +
class PersonController  extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
 +
 +
  /**
 +
    * personRepository
 +
    *
 +
    * @var \Vendor\Ext\Domain\Repository\PersonRepository
 +
    * @inject
 +
    */
 +
    protected $personRepository;
 +
 +
  /**
 +
    * pesistenceManager - not neccessary since 6.2
 +
    *
 +
    * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
 +
    * @inject
 +
    */
 +
    protected $persistenceManager;
 +
 +
    /**
 +
    * Action addImage
 +
    *
 +
    * @param \Vendor\Ext\Domain\Model\Person $person
 +
    * @return void
 +
    */
 +
    public function AddImageAction(\Vendor\Ext\Domain\Model\Person $person) {
 +
 +
        $storageRepository = $this->objectManager->get('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
 +
        $storage = $storageRepository->findByUid('1');
 +
        $targetFolder = $storage->createFolder('path/to/target_folder');
 +
        $originalFilePath = '/tmp/tempfile.png';
 +
        $newFileName = 'myfile123.png';
 +
 +
        if (file_exists($originalFilePath)) {
 +
            $movedNewFile = $storage->addFile($originalFilePath, $targetFolder, $newFileName);
 +
            $newFileReference = $this->objectManager->get('Vendor\\Ext\\Domain\\Model\\FileReference');
 +
            $newFileReference->setFile($movedNewFile);
 +
            $person->setImage($newFileReference);
 +
        }
 +
 +
        $this->personRepository->update($person);
 +
        // Not neccessary since 6.2
 +
        $this->persistenceManager->persistAll();
 +
 +
    }
 +
 +
}
 +
</syntaxhighlight>
 +
'''TCA Konfiguration (Person)'''
 +
<syntaxhighlight lang="php">
 +
'image' => array(
 +
'exclude' => 1,
 +
'label' => 'LLL:EXT:ext/Resources/Private/Language/locallang_db.xlf:tx_ext_domain_model_person.image',
 +
'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
 +
'image',
 +
array(
 +
'maxitems' => 6,
 +
'foreign_match_fields' => array(
 +
'fieldname' => 'image',
 +
'tablenames' => 'tx_ext_domain_model_person',
 +
'table_local' => 'sys_file',
 +
),
 +
),
 +
$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
 +
),
 +
),
 +
</syntaxhighlight>
 +
'''Typoscript Konfiguration'''
 +
<syntaxhighlight lang="php">
 +
config.tx_extbase {
 +
persistence {
 +
classes {
 +
Vendor\Ext\Domain\Model\FileReference {
 +
mapping {
 +
tableName = sys_file_reference
 +
columns {
 +
uid_local.mapOnProperty = originalFileIdentifier
 +
}
 +
}
 +
}
 +
}
 +
objects {
 +
TYPO3\CMS\Extbase\Domain\Model\FileReference.className = Vendor\Ext\Domain\Model\FileReference
 +
}
 +
updateReferenceIndex = 1
 +
}
 +
}
 +
</syntaxhighlight>
 +
 
=== Upload Beispiel - Reusable Code ===
 
=== Upload Beispiel - Reusable Code ===
 
Um wiederverwendbar für alle Controller zu sein, darf der Code nicht im Controller sein. Dann wirds aber komplizierter. Hier ein Beispiel wie es geht:
 
Um wiederverwendbar für alle Controller zu sein, darf der Code nicht im Controller sein. Dann wirds aber komplizierter. Hier ein Beispiel wie es geht:
 
  https://github.com/helhum/upload_example
 
  https://github.com/helhum/upload_example
 
  http://insight.helhum.io/post/85015526410/file-upload-using-extbase-and-fal-in-typo3-62
 
  http://insight.helhum.io/post/85015526410/file-upload-using-extbase-and-fal-in-typo3-62
 +
http://ab-softlab.tumblr.com/post/119838114044/fileupload-in-frontend-using-typo3-6x-or-7-fal (Step by Step Anleitung)
 +
http://wiki.zone30.info/wikizone/images/8/8b/Upload_example-master.zip

Aktuelle Version vom 22. Oktober 2015, 10:40 Uhr

Links[Bearbeiten]

https://wiki.typo3.org/File_Abstraction_Layer

Extbase - Datei Upload ohne FAL


Allgemein Dateien mit Extbase Handeln[Bearbeiten]

In general, you should be able to call getOriginalResource() on a \TYPO3\CMS\Extbase\Domain\Model\FileReference object. For more concrete examples, either refer to the doc links or have a look at the wiki for a example handling FileReferences.

Beispiele[Bearbeiten]

Basics[Bearbeiten]

http://www.typo3tiger.de/blog/post/extbase-fal-beispiel.html

Beispiel Dateiupload[Bearbeiten]

http://www.koller-webprogramming.ch/tipps-tricks/typo3-extension-entwicklung-extbase/dateien-und-bilder-mit-extbase-61-uploaddownload-via-fal-file-abstraction-layer/

Beispiel File Referenz dynamisch erzeugen[Bearbeiten]

http://www.typo3tiger.de/blog/post/extbase-fal-filereference-im-controller-erzeugen.html

Dynamisch bedeutet hier, dass nach dem Upload dynamisch eine FAL FileReference zu einem Objekt im Controller erzeugt wird. Somit kann man die neue Datei auch gleich einem vorhandenen oder neu erstellten Objekt zuordnen.

Allgemeines Vorgehen:

  • Erweitern der Klasse FileReference
  • TCA Konfiguration vervollständigen
  • Parameter an TCA übergeben (Methode getFileFieldTCAConfig mit dem Parameter foreign_match_fields

Achtung: Grund für so manche Verwirrung ist, dass TYPO3 zwei unterschiedliche FileReference-Klassen liefert: Das Extbase Model \TYPO3\CMS\Extbase\Domain\Model\FileReference sowie das Core Model \TYPO3\CMS\Core\Resource\FileReference


Extbase Model (FileReference)

namespace Vendor\Ext\Domain\Model;

/**
 * Class FileReference
 */
class FileReference extends \TYPO3\CMS\Extbase\Domain\Model\FileReference {

    /**
     * uid of a sys_file
     *
     * @var integer
     */
    protected $originalFileIdentifier;

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

    /**
     * setFile
     *
     * @param \TYPO3\CMS\Core\Resource\File $falFile
     * @return void
     */
    public function setFile(\TYPO3\CMS\Core\Resource\File $falFile) {
        $this->originalFileIdentifier = (int)$falFile->getUid();
    }

}

Extbase Model (Person)

namespace Vendor\Ext\Domain\Model;

/**
 * Class Person
 */
class Person extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {

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

    /**
     * image
     *
     * @var \TYPO3\CMS\Extbase\Domain\Model\FileReference
     */
    protected $image = NULL;

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

    /**
     * Sets the title
     *
     * @param string $title
     * @return void
     */
    public function setTitle($title) {
        $this->title = $title;
    }

    /**
     * Returns the image
     *
     * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
     */
    public function getImage() {
        return $this->image;
    }

    /**
     * Sets the image
     *
     * @param \TYPO3\CMS\Extbase\Domain\Model\FileReference $image
     * @return void
     */
    public function setImage(\TYPO3\CMS\Extbase\Domain\Model\FileReference $image) {
        $this->image = $image;
    }

}

Extbase Controller (Person)

namespace Vendor\Ext\Domain\Model;

/**
 * Class PersonController
 */
class PersonController  extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {

   /**
    * personRepository
    *
    * @var \Vendor\Ext\Domain\Repository\PersonRepository
    * @inject
    */
    protected $personRepository;

   /**
    * pesistenceManager - not neccessary since 6.2
    *
    * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
    * @inject
    */
    protected $persistenceManager;

    /**
     * Action addImage
     *
     * @param \Vendor\Ext\Domain\Model\Person $person
     * @return void
     */
    public function AddImageAction(\Vendor\Ext\Domain\Model\Person $person) {

        $storageRepository = $this->objectManager->get('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
        $storage = $storageRepository->findByUid('1');
        $targetFolder = $storage->createFolder('path/to/target_folder');
        $originalFilePath = '/tmp/tempfile.png';
        $newFileName = 'myfile123.png';

        if (file_exists($originalFilePath)) {
            $movedNewFile = $storage->addFile($originalFilePath, $targetFolder, $newFileName);
            $newFileReference = $this->objectManager->get('Vendor\\Ext\\Domain\\Model\\FileReference');
            $newFileReference->setFile($movedNewFile);
            $person->setImage($newFileReference);
        }

        $this->personRepository->update($person);
        // Not neccessary since 6.2
        $this->persistenceManager->persistAll();

    }

}

TCA Konfiguration (Person)

'image' => array(
	'exclude' => 1,
	'label' => 'LLL:EXT:ext/Resources/Private/Language/locallang_db.xlf:tx_ext_domain_model_person.image',
	'config' => \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getFileFieldTCAConfig(
		'image',
		array(
			'maxitems' => 6,
			'foreign_match_fields' => array(
				'fieldname' => 'image',
				'tablenames' => 'tx_ext_domain_model_person',
				'table_local' => 'sys_file',
			),
		),
		$GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext']
	),
),

Typoscript Konfiguration

config.tx_extbase {
	persistence {
		classes {
			Vendor\Ext\Domain\Model\FileReference {
				mapping {
					tableName = sys_file_reference
					columns {
						uid_local.mapOnProperty = originalFileIdentifier
					}
				}
			}
		}
		objects {
			TYPO3\CMS\Extbase\Domain\Model\FileReference.className = Vendor\Ext\Domain\Model\FileReference
		}
		updateReferenceIndex = 1
	}
}

Upload Beispiel - Reusable Code[Bearbeiten]

Um wiederverwendbar für alle Controller zu sein, darf der Code nicht im Controller sein. Dann wirds aber komplizierter. Hier ein Beispiel wie es geht:

https://github.com/helhum/upload_example
http://insight.helhum.io/post/85015526410/file-upload-using-extbase-and-fal-in-typo3-62
http://ab-softlab.tumblr.com/post/119838114044/fileupload-in-frontend-using-typo3-6x-or-7-fal (Step by Step Anleitung)

http://wiki.zone30.info/wikizone/images/8/8b/Upload_example-master.zip