Skip to content

Instantly share code, notes, and snippets.

@htuscher
Last active November 17, 2015 16:05
Show Gist options
  • Select an option

  • Save htuscher/cd57e7b68e8fe6642165 to your computer and use it in GitHub Desktop.

Select an option

Save htuscher/cd57e7b68e8fe6642165 to your computer and use it in GitHub Desktop.
TYPO3 Solr Extbase repository hook
<?php
$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher');
$signalSlotDispatcher->connect(
'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend',
'afterInsertObject',
'Vendor\\Extension\\Solr\\ProductObserver',
'afterCreateProduct'
);
$signalSlotDispatcher->connect(
'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend',
'afterUpdateObject',
'Vendor\\Extension\\Solr\\ProductObserver',
'afterUpdateProduct'
);
$signalSlotDispatcher->connect(
'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend',
'afterRemoveObject',
'Vendor\\Extension\\Solr\\ProductObserver',
'afterRemoveProduct'
);
<?php
/***************************************************************
* Copyright notice
*
* (c) 2015 Hans Höchtl <[email protected]>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
namespace Vendor\Extension\Solr;
use Vendor\Extension\Domain\Model\Product;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
class ProductObserver
{
static protected $tableName = 'tx_vendor_domain_model_product';
/**
* @var \Tx_Solr_IndexQueue_Queue
*/
protected $indexQueue;
/**
* Constructor
*/
public function __construct() {
$this->indexQueue = GeneralUtility::makeInstance('Tx_Solr_IndexQueue_Queue');
}
/**
* @param DomainObjectInterface $object
*/
public function afterCreateProduct($object) {
if ($object instanceof Product) {
$this->indexQueue->updateItem(self::$tableName, $object->getUid());
}
}
/**
* @param DomainObjectInterface $object
*/
public function afterUpdateProduct($object) {
if ($object instanceof Product) {
if ($object->isVisible()) {
$this->indexQueue->updateItem(self::$tableName, $object->getUid());
} else {
$this->removeFromIndexAndQueue(self::$tableName, $object->getUid());
}
}
}
/**
* @param DomainObjectInterface $object
*/
public function afterRemoveProduct($object) {
if ($object instanceof Product) {
$this->removeFromIndexAndQueue(self::$tableName, $object->getUid());
}
}
/**
* Removes record from the index queue and from the solr index
*
* @param string $recordTable Name of table where the record lives
* @param int $recordUid Id of record
*/
protected function removeFromIndexAndQueue($recordTable, $recordUid) {
/** @var \Tx_Solr_GarbageCollector $garbageCollector */
$garbageCollector = GeneralUtility::makeInstance('Tx_Solr_GarbageCollector');
$garbageCollector->collectGarbage($recordTable, $recordUid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment