In your observer, check if the conditions for moving the block are met. if so, get the layout and manually add a layout update within the observer.
Here's an example that uses an observer on layout_load_before
:
namespace Company\Module\Observer;
class LayoutLoadBefore implements \Magento\Framework\Event\ObserverInterface
{
/**
* add custom layout updates
*
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$move = false;
// add your checks here
if ($move) {
/** @var \Magento\Framework\View\LayoutInterface $layout */
$layout = $observer->getData('layout');
$layout->getUpdate()->addUpdate('<move element="blah" destination="to" before="-" />');
}
return $this;
}
}
this event conveniently provides access to the layout. if you are stuck with a specific event, there are other ways to access the current layout to add the update.