Benutzer: Gast
Eintrag: PageModel() getMedia()

PageModel() getMedia()

von: marcel
Tags: Typo3, Extbase
Kreise: Typo3

Example to get the image-files from the media-Field in an Extbase Page-Domain-Model.

Classes/Domain/Model/Page.php

Code: PHP
<?php
/* Typo3 Source 6.2.5 . Should work with previous versions working with namespaces */
namespace VENDOR\ExtName\Domain\Model;

class Page extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
	
	/**
	 * fileRepository
	 *
	 * @var \TYPO3\CMS\Core\Resource\FileRepository
	 * @inject
	 */
	protected $fileRepository;
	
	/**
	 * Field Media
	 * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference>
	 */
	protected $media;
	
	/**
	 * First found image in field media
	 * @var array
	 */
	protected $_firstMediaImage = array();
	
	/**
	 * All Images in Field media.
	 * Variables starting with _ are only in temporary use.
	 * I don't know if its faster to find the FileReferences only once than ask the Repository more often and iterate over all found files again and again.
	 * Once on checking, if there are files: <f:if condition="{page.mediaImages -> f:count()}"></f:if>
	 * And once on iterating over the images: <f:for each="{page.mediaImages} as="image"></f:for>
	 * Here are only getters and I store the results in this variables. Then in the getter I just return the stored result (if there is one).
	 * @var array
	 */
	protected $_mediaImages = array();
	
	/**
	 * @var array
	 */
	protected $_imageMimeTypes = array(
		'image/jpeg',
		'image/jpg',
		'image/png',
		'image/gif'
	);
	
	/**
	 * Returns the media
	 *
	 * @return \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\FileReference> $media
	 */
	public function getMedia() {
		return $this->media;
	}
	
	/**
	 * The first Image found in Field media.
	 * @return array
	 */
	public function getFirstMediaImage() {
		// try to return the image from a previous call of this method
		if (is_array($this->_firstMediaImage) && count($this->_firstMediaImage)) {
		  return $this->_firstMediaImage;
		}
		$fileObjects = $this->fileRepository->findByRelation( 'pages', 'media', ($this->_localizedUid) ? $this->_localizedUid : $this->getUid() );
		foreach ( $fileObjects as $fileObject) {
			$originalFile = $fileObject->getOriginalFile()->getProperties();
			if (in_array( $originalFile['mime_type'], $this->_imageMimeTypes )) {
				$this->_firstMediaImage = array (
						'reference' => $fileObject->getReferenceProperties(),
						'original' => $fileObject->getOriginalFile()->getProperties() 
				);
				return $this->_firstMediaImage;
			}
		}
		//\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump( $this->_firstMediaImage, 'first media image = ' );
		return null;
	}
	
	/**
	 * All Image-Files in Field media.
	 * @return array
	 */
	public function getMediaImages() {
		// try to return the images from a previous call of this method
		if (is_array($this->_mediaImages) && count($this->_mediaImages)) {
		  return $this->_mediaImages;
		}
		
		$fileObjects = $this->fileRepository->findByRelation( 'pages', 'media', ($this->_localizedUid) ? $this->_localizedUid : $this->getUid() );
		$mediaImages = array();
		foreach ( $fileObjects as $fileObject) {
			$originalFile = $fileObject->getOriginalFile()->getProperties();
			if (in_array( $originalFile['mime_type'], $this->_imageMimeTypes )) {
				$mediaImages[] = array (
						'reference' => $fileObject->getReferenceProperties(),
						'original' => $fileObject->getOriginalFile()->getProperties()
				);
			}
		}
		$this->_mediaImages = $mediaImages;
		//\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump( $_mediaImages, 'All media images =' );
		return $this->_mediaImages;
	}
}
?>

Resources/Private/Templates/Controller/Action.html

Code: XML XHTML XSLT HTML
<!-- Example Fluid-Call of the images of field media -->
<f:if condition="{page.mediaImages -> f:count()}">
	<f:for each="{page.mediaImages}" as="image">
		<f:image src="{image.reference.uid}" alt="" treatIdAsReference="true" />
	</f:for>
</f:if>

ext_typoscript_setup.txt

Code: TypoScript (TS)
# don't forget to map the Model to the right table:
config.tx_extbase {
	persistence {
		classes {
			VENDOR\ExtName\Domain\Model\Page {
				mapping {
					tableName = pages
				}
			}
		}
	}
}

The example worked for me. When transferred from the program to this page perhaps small errors have crept in. If you find some, please inform me and I’ll remove them.

Es gibt noch keine Kommentare.