Project

General

Profile

Actions

Feature #61722

closed

Make it possible to translate records with extbase

Added by Frank Nägler over 9 years ago. Updated almost 9 years ago.

Status:
Rejected
Priority:
Should have
Assignee:
Category:
-
Target version:
Start date:
2014-09-18
Due date:
% Done:

0%

Estimated time:
PHP Version:
Tags:
Complexity:
Sprint Focus:

Description

Currently it is not possible to translate records with extbase.
It would be nice to have a translate method in all repositories which can copy a record an set the correct language fields.

Actions #1

Updated by Gerrit Code Review over 9 years ago

  • Status changed from New to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/32866

Actions #2

Updated by Frank Nägler over 9 years ago

  • Status changed from Under Review to Rejected

rejected, because it is possible, see comments on gerrit.

Actions #3

Updated by Mathias Brodala over 9 years ago

Shortened code example for documentation:

<?php
namespace Vendor\MyExtension\Domain\Model;

/**
 * Add this to your TypoScript setup:
 *
 * config.tx_extbase.persistence.classes {
 *   Vendor\MyExtension\Domain\Model\Foo.mapping {
 *     columns {
 *       l10n_parent.mapOnProperty = translationParent
 *     }
 *   }
 * }
 *
 * Usage:
 *
 * $fooTranslation = $objectManager->get($targetType);
 * $fooTranslation->_setProperty('_languageUid', $languageUid);
 * $fooTranslation->setTranslationParent($foo);
 * // This allows for immediate access to translation during this request
 * // and easy persistence simply by persisting the original object
 * $foo->addTranslation($fooTranslation);
 * // Set translated property values ...
 * $fooTranslation->setBar('My translation');
 * // Add/update the original object
 * $fooRepository->update($foo);
 */
class Foo extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {

  /**
   * @var \Vendor\MyExtension\Domain\Model\Foo $translationParent
   */
  protected $translationParent;

  // Getter/Setter ...

  /**
   * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Vendor\MyExtension\Domain\Model\Foo>
   */
  protected $translations;

  // Getter/Setter/Add/Remove ...

  /**
   * Sets up this object
   */
  public function __construct() {

    $this->translations = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
  }
}
Actions #4

Updated by Artus Kolanowski almost 9 years ago

Because I can't and won't change a domain model for this in many cases I wrote a transparent solution. So it might be helpful for anybody:

<?php
/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\ColumnMap;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * Provides services to translate domain objects
 */
class TranslationService implements SingletonInterface {

    /**
     * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
     * @inject
     */
    protected $dataMapper;

    /**
     * Translates a domain object
     *
     * @param DomainObjectInterface $origin
     * @param DomainObjectInterface $translation
     * @param int $language
     * @throws \Exception
     * @return void
     */
    public function translate(DomainObjectInterface $origin, DomainObjectInterface $translation, $language) {
            if (get_class($origin) !== get_class($translation)) {
            throw new \Exception('Origin and translation must be the same type.', 1432499926);
        }

        $dataMap = $this->dataMapper->getDataMap(get_class($origin));

        if (!$dataMap->getTranslationOriginColumnName()) {
            throw new \Exception('The type is not translatable.', 1432500079);
        }

        $propertyName = GeneralUtility::underscoredToLowerCamelCase($dataMap->getTranslationOriginColumnName());

        if ($translation->_setProperty($propertyName, $origin) === FALSE) {
            $columnMap = $dataMap->getColumnMap($propertyName);
            $columnMap->setTypeOfRelation(ColumnMap::RELATION_HAS_ONE);
            $columnMap->setType($dataMap->getClassName());
            $columnMap->setChildTableName($dataMap->getTableName());

            $translation->{$propertyName} = $origin;
        }

        $translation->_setProperty('_languageUid', $language);
    }
}

To use this you need just to call TranslationService::translate() before you save the translation.

Actions

Also available in: Atom PDF