The more elegant way would be to use the same code as the code that enables the flat mode when saving the configuration. It can be found under Magento/Catalog/Model/Indexer/Product/Flat/System/Config/Mode
:
public function processValue()
{
if ((bool)$this->getValue() != (bool)$this->getOldValue()) {
if ((bool)$this->getValue()) {
$this->indexerState->loadByIndexer(\Magento\Catalog\Model\Indexer\Product\Flat\Processor::INDEXER_ID);
$this->indexerState->setStatus(\Magento\Framework\Indexer\StateInterface::STATUS_INVALID);
$this->indexerState->save();
} else {
$this->_productFlatIndexerProcessor->getIndexer()->setScheduled(false);
}
}
}
So I'm pretty sure you could do something like that:
$this->_productFlatIndexerProcessor->getIndexer()->setScheduled(false);
Where $this->_productFlatIndexerProcessor
is an instance of \Magento\Catalog\Model\Indexer\Product\Flat\Processor
.
Possible alternative
However, that method does not save the config so when the system checks if flat is enabled via the config it still returns true.
A possible alternative (to be tested) would be to use a plugin on the isFlatEnabled
method from Magento\Catalog\Model\Indexer\Product\Flat\State
(the method is actually defined in the Magento\Catalog\Model\Indexer\AbstractFlatState
class).
Based on what you want to achieve you could setup an after plugin to force that method to return false under certain conditions.