Skip to content

Instantly share code, notes, and snippets.

@benjaminrau
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjaminrau/3642d8e6c4a3e24c4e29 to your computer and use it in GitHub Desktop.
Save benjaminrau/3642d8e6c4a3e24c4e29 to your computer and use it in GitHub Desktop.
ViewHelper to query dynamic repository
<?php
/***************************************************************
* Copyright notice
*
* (c) 2014 Benjamin Rau <rau@codearts.at>
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* ### QueryRepository ViewHelper
*
* Calls a repository to query objects
*
* @author Benjamin Rau <rau@codearts.at>
*/
class Tx_MyExt_ViewHelpers_QueryRepositoryViewHelper extends Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
/**
* @var \Tx_Extbase_Persistence_Repository
*/
protected $repository;
/**
* @param string $repositoryName
* @param mixed $storagePid
* @return mixed
*/
public function render($repositoryName, $storagePid = NULL) {
$this->repository = $this->initializeRepository($repositoryName);
if (NULL !== $storagePid) {
$this->setStoragePid($storagePid);
}
return $this->repository->findAll();
}
/**
* initialize Repository
*
* @param string $repositoryName
* @return \Tx_Extbase_Persistence_Repository $repository
*/
protected function initializeRepository($repositoryName) {
/* @var Tx_Extbase_Object_ObjectManager $objectManager */
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Tx_Extbase_Object_ObjectManager');
/* @var Tx_Extbase_Persistence_Repository $repository */
$repository = $objectManager->get($repositoryName);
/* @var \Tx_Extbase_Persistence_Typo3QuerySettings $querySettings */
$querySettings = $objectManager->get('Tx_Extbase_Persistence_Typo3QuerySettings');
$querySettings->setRespectStoragePage(FALSE);
$repository->setDefaultQuerySettings($querySettings);
return $repository;
}
/**
* Set the storage Pid
*
* @param mixed $storagePid
*/
protected function setStoragePid($storagePid) {
/* @var Tx_Extbase_Object_ObjectManager $objectManager */
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Tx_Extbase_Object_ObjectManager');
/* @var Tx_Extbase_Persistence_Typo3QuerySettings $querySettings */
$querySettings = $objectManager->get('Tx_Extbase_Persistence_Typo3QuerySettings');
$querySettings->setRespectStoragePage(TRUE);
if (TRUE === is_integer($storagePid)) {
$storagePid[] = $storagePid;
}
$querySettings->setStoragePageIds($storagePid);
$this->repository->setDefaultQuerySettings($querySettings);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment