[TYPO3-mvc] Using a Repository inside a ViewHelper

Sebastian Michaelsen sebastian.gebhard at gmail.com
Tue May 24 12:07:39 CEST 2011


Hey,

I'm struggling to use a Repository inside a custom ViewHelper.

=== Why the heck would you want to do that? ===
I have an Extension dealing with "definitions" (like Glossary Entries). 
It provides a ViewHelper that checks some text if it has "defined" words 
in it and links them to their explanation on a Glossary page. It is 
designed to be used in other extensions. Like if you have an extbased 
blog you can use the GlossarylinksViewHelper to add links to your posts. 
(The actual replacement in the code below is not finished, but that's 
not my problem)
This is why I need to do the data things inside the ViewHelper, because 
the extension that is enriched with "definitions" is not aware of the 
glossary data.

=== What's the problem? ===
I can't figure out how to tell the DefinitionsRepository from which 
Folder it should read the records. By default it takes the 
plugin.tx_otherextension.persistence.storagePid value from the extension 
that it is used with.

E.g. I have a Blog with posts from pid 20 and I have Glossary Terms on 
pid 30. In one of the Blog Templates I use the GlossarylinksViewHelper, 
but the DefinitionsRepository will always try to read from pid 20.

Any hints for me how I can set the right Storage Folder? Or should I try 
a totally other approach?

Kind regards,
Sebastian

(Below my current GlossarylinksViewHelper Class:)

<?php

/**
  * Parses text and includes explanations for glossary words
  *
  * @author Michaelsen
  */
class Tx_Definitions_ViewHelpers_GlossarylinksViewHelper extends 
Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
	
	/**
	 * @var Tx_Definitions_Domain_Repository_DefinitionRepository
	 */
	protected $definitionRepository;
	
	/**
	 * @param Tx_Definitions_Domain_Repository_DefinitionRepository 
$definitionRepository
  	 * @return void
	 */
	public function 
injectDefinitionRepository(Tx_Definitions_Domain_Repository_DefinitionRepository 
$definitionRepository) {
		$this->definitionRepository = $definitionRepository;
	}
	
	/**
	 * @param string $targetPage TODO: Link to the Glossary page
	 * @param string $storagePage Page to read Glossary entries from
	 * @return string
	 */
	public function render($targetPage, $storagePage) {
		
		$content = $this->renderChildren();
		
		/**
		 * TODO: Read from $storagePage
		 */
		$definitions = $this->definitionRepository->findAll();
		
		foreach($definitions as $definition) {
			$content = str_replace($definition->getTitle(), '<span title="' . 
$definition->getExplanation() . '">' . $definition->getTitle() . 
'</span>', $content);
		}
		
		return $content;
		
	}
	
}


More information about the TYPO3-project-typo3v4mvc mailing list