TYPO3/Extend Aimeos

From Aimeos documentation

Other languages:
English 100% • ‎русский 3%

2017.x version

Create an extension

Aimeos and the Aimeos TYPO3 extension are very powerful but there are numerous features that are only available through additional extensions. Often, your project also requires special features that makes it different from other web sites build with Aimeos. But extending the Aimeos TYPO3 extension itself is a bad thing because you will loose the ability to update the extension. To solve this, the Aimeos TYPO3 extension allows you to integrate your own TYPO3 extension containing additional Aimeos extensions.

You can easily create a new extension that is already packaged inside a TYPO3 extension using the extension generator

Adding Aimeos extensions from inside your TYPO3 extension is very simple:

  • Create a new TYPO3 extension (easiest via the TYPO3 Extension Builder extension but don't use underscores if you aren't aware of how TYPO3 expects class names then)
  • Adapt the ext_emconf.php and add the Aimeos extension as dependency (replace the version numbers if your extension requires a different version of Aimeos)
  1. 'constraints' => array(
  2. 	'depends' => array(
  3. 		'aimeos' => '17.0.0-17.99.99',
  4. 	),
  5. ),
  • Adapt the ext_localconf.php. It must contain at least this lines
  1. <?php
  3. if ( ! defined( 'TYPO3_MODE' ) ) {
  4. 	die ( 'Access denied.' );
  5. }
  7. $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['extDirs']['1_'.$_EXTKEY] =
  8.   'EXT:' . $_EXTKEY . '/Resources/Extensions/';
  10. $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['confDirs']['1_'.$_EXTKEY] =
  11.   'EXT:' . $_EXTKEY . '/Resources/Private/Config/';
  13. ?>

The order of the extension and configuration directories is important as settings of files in later directories will overwrite previously declared ones! The directory names will be sorted by their key and the Aimeos extension always uses "0_aimeos". If you have several TYPO3 extensions that contains configuration settings, you can change the prefix to "2_", "3_" or similar to enforce the correct order.

  • Place your additional extensions in Resources/Extensions/ of your extension
  • Add required configuration for the Aimeos extensions in Resources/Private/Config/ of your TYPO3 extension (e.g. classes.php, client.php, controller.php, mshop.php, etc.)

Don't forget to install the new TYPO3 extension using the Extension Manager! Copying the extension in the typoconf/ext/ directory isn't sufficient as the configuration above won't be used. Always install your extension after the Aimeos TYPO3 extension. This should be normally enforced automatically if you've added Aimeos as dependency. Otherwise, your extension won't be found.

Use the Aimeos objects

If you need to instantiate the Aimeos controllers or managers directly in your TYPO3 extension and call their methods, you have to supply a context object. This object is a dependency injection container that offers access to configuration settings, database connections, session and content cache as well as translation facilities. You can get an instance in TYPO3 depending on your environment:

  1. // in a controller action
  2. use Aimeos\Aimeos\Controller\AbstractController;
  4. class YourController extends AbstractController
  5. {
  6.     public function indexAction() {
  7.         $context = $this->getContext();
  8.     }
  9. }
  11. // in a scheduler task
  12. $context = \Aimeos\Aimeos\Scheduler\Base::getContext( $localConfigArray );
  14. // anywhere else
  15. $config = \Aimeos\Aimeos\Base::getConfig( $localConfigArray );
  16. $context = \Aimeos\Aimeos\Base::getContext( $config );

In the MVC controller actions where the required parameters for site, language and currency are available as part of the request, a locale object is added to the context automatically. Everywhere else, you need to retrieve this values from somewhere else, e.g. the configuration. Then, you can use the bootstrap() method of the locale manager to retrieve the locale item yourself:

  1. $manager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
  2. $item = $manager->bootstrap( '<sitecode>', '<languageid>', '<currencyid>', true );
  3. $context->setLocale( $item );

The language and currency IDs are optional and the first matching locale item from the mshop_locale table in the database will be used. If there are no entries in the mshop_locale table because you want to manage the data in a custom administration interface, you can create an empty locale object yourself:

  1. $manager = \Aimeos\MShop\Factory::createManager( $context, 'locale' );
  2. $item = $manager->createItem();
  3. $item->setLanguageId( 'en' );
  4. $context->setLocale( $item );

It's a good idea to set at least the currently used language ID because then you are able to add the translation facilities for this language too and there won't be any exception if some code wants to translate a string:

  1. // use the appropriate way depending on your environment
  2. $i18n = \Aimeos\Aimeos\Base::getI18n( array( 'en' ) );
  3. $context->setI18n( $i18n );

Alternatively, if you don't want any translation, you can add a "Null" object instead. It returns the singular or plural string untranslated and for this decision it needs the language ID to determine the right singular/plural rule:

  1. $i18n = array( 'en' => new \Aimeos\MW\Translation\None( 'en' ) );
  2. $context->setI18n( $i18n );

Afterwards, you are able to create every object from the Aimeos core and save, retrieve or delete the stored data. You should never use the "new" operator to create a new objects because the implementation variant depends on the configuration and decorators are added automatically. Instead, use the Aimeos\MShop\Factory class or any more specific factory to create new objects, e.g.

  1. $manager = \Aimeos\MShop\Factory::createManager( $context, 'product' );
  2. $search = $manager->createSearch();
  3. $search->setConditions( $search->compare( '==', 'product.code', 'test' ) );
  5. foreach( $manager->searchItems( $search ) as $id => $item ) {
  6.     // print_r( $item );
  7. }