[TYPO3-mvc] prefilter for e.G. category=2 in a search/list view upon page load

Franz Koch typo3.RemoveForMessage at elements-net.de
Fri Apr 8 02:25:30 CEST 2011


Hey,

> I have a searchform and a resulting listview (like in sjroffers) and I want
> to prefilter for a particular category upon page load. I don't want to just
> preselect a value in the form as in<f:form:select value=2  .../>  in the
> template but actually load the results for category=2.
> I don't seem to find a solution. Since there is no setUid() function in the
> Model and I also cannot override it, I seem to be locked out in whatever I
> try in the Repository or Controller. Is there any solution apart from a
> reload of the page with js with the initial category preselected?

because of your're refering to sjroffers I suppose you're also working 
with some demand object. If so, this might be what you're looking for:

----------
class Tx_YourExt_Domain_Controller_SomeController {
# ...
# here some injector methods for instantiating the categoryRepository 
and your main item repository
# ...
	/**
	 * List view Action. Is returning a list of your objects
	 *
	 * @param Tx_YourExt_Domain_Model_Demand $demand The search demand used 
to filter the objects in the list
	 * @return string The rendered view
	 */
	public function listAction( Tx_YourExt_Domain_Model_Demand $demand = 
NULL) {
		# ...
		// create a demand if none has been passed
		if ($demand === NULL) {
			$demand = $this->objectManager->create('Tx_YourExt_Domain_Model_Demand');
			
			// add default filtering options to the demand
			if ( isset($this->settings['list']['filter']['defaultCategory']) && 
!empty($this->settings['list']['filter']['defaultCategory']) ) {
				$demand->setCategory( $this->categoryRepository->findOneByUid( 
intval($this->settings['list']['filter']['defaultCategory']) ) );
			}
		}

		$this->view->assign('listItems', 
$this->someRepository->findDemanded($demand));
		$this->view->assign('categories', $this->categoryRepository->findAll());
	}
}
---------

So basically just prefill your demand object as needed. If your demand 
object only allows ONE category to be set and you don't have to interact 
with that demanded category directly from the demandObject itself, you 
could also consider to simply switch the property type of the demand to 
"integer" and not to a categoryObject which would allow a simple 
$demand->setCategory(3). As we're working object oriented passing a real 
object which ensures that it exists is better - but it's up to you and 
your demand to the demand object :).

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list