[TYPO3-mvc] Get Domain Object with forced default language

Franz Koch typo3.RemoveForMessage at elements-net.de
Thu Dec 30 15:36:41 CET 2010


Hey,

> I work on an extbase extension for a website with multiple languages.
> Some of the fields of my model are the same in every of the 5 languages
> and the editors don't want to have to put the same image into the 4
> translations of a record.
>
> My idea was to create a fallback mechanism. If a foreign language record
> has no image, get the image from default language.
>
> Example: Default language is English, one of the foreign languages is
> German. I have a product with an image field, but it's only filled in
> the default language version. In German it's empty and should be filled
> automatically from the default language.

have you tried configuring language fallback for that field via TCA/TS? 
AFAIK extbase is using language overlay functionality from TYPO3, so 
this should work up to some point.

> But obviously i need to have a domain model object in default language.
> For that i built a repository method to get the default language object
> for a given foreign language object:
>
> /**
> * Finds a default language product for the given product
> *
> * @param Tx_TechCatalog_Domain_Model_Product $product
> */
> public function
> findDefaultLanguageProduct(Tx_TechCatalog_Domain_Model_Product $product) {
>
> $query = $this->createQuery();
>
> //Disable Language restriction
> $querySettings =
> $this->objectManager->getObject('Tx_Extbase_Persistence_Typo3QuerySettings');
>
> $querySettings->setRespectSysLanguage(FALSE);
> $query->setQuerySettings($querySettings);
>
> $uidConstraint = $query->equals('uid',
> $product->_getProperty('t3_origuid'));
> $languageConstraint = $query->equals('sys_language_uid', 0);
>
> return
> $query->matching($uidConstraint)->matching($languageConstraint)->execute();
>
> }

The last line is wrong. You can only pass one "matching" constraint to 
the query - so in your case the last one is replacing your uid 
constraint. You have to concatenate them via a locigalAnd constraint. 
There's also some unnecessary overhead in your handling of the 
querySettings. Give this one a try:

  /**
  * Finds a default language product for the given product
  *
  * @param Tx_TechCatalog_Domain_Model_Product $product
  */
  public function
  findDefaultLanguageProduct(Tx_TechCatalog_Domain_Model_Product $product) {

  $query = $this->createQuery();

  //Disable Language restriction
  $query->getQuerySettings()->setRespectSysLanguage(FALSE);

   $constraints = $query->logicalAnd(
       $query->equals('uid', $product->_getProperty('t3_origuid')),
       $query->equals('sys_language_uid', 0)
   );

   return $query->matching($constraints)
       ->execute();
}

Please note that in general you're not supposed to use the _getProperty 
method. Better use 
Tx_Extbase_Reflection_ObjectAccess::getProperty($object, $nameOfProperty);

-- 
kind regards,
Franz Koch


More information about the TYPO3-project-typo3v4mvc mailing list