0

I am using a theme and trying to import blocks has this warning popup:

Warning: Invalid argument supplied for foreach() in /home/pluslivi/public_html/app/code/Rokanthemes/RokanBase/Controller/Adminhtml/Rokanbase/Importblock.php on line 67

When I go to the file to take a look at the error line -

foreach($data['root']['blocks']['item'] as $_item) {
            $exist = false;
            $collection = $this->_objectManager->create('Magento\Cms\Model\Block')->getCollection();
            $collection->addFieldToFilter('identifier', $_item['identifier']);
            if($collection->getSize())
                $exist = true;
            $block = $this->_objectManager->create('Magento\Cms\Model\Block');
            if($overwrite) {
                if($exist) {
                    $conflictingOldItems[] = $_item['identifier'];
                    $block->load($_item['identifier']);
                }
            } else {
                if($exist) {
                    continue;
                }
            }
            $conflictingOldItems[] = $_item['identifier'];
            $_item['store_id'] = array(0);
            $block->addData($_item)->save();
            $i++;
        }

Can someone help me out with this?
Any help would be greatly appreciated.

  • print_r($data); before your foreach loop. It will help you. – Suman Singh Feb 10 at 3:26
0

Check $data['root']['blocks']['item'] is array or not before looping. Try following way:


if (isset($data['root']['blocks']['item']) && is_array($data['root']['blocks']['item'])) {
    foreach($data['root']['blocks']['item'] as $_item) {
        $exist = false;
        $collection = $this->_objectManager->create('Magento\Cms\Model\Block')->getCollection();
        $collection->addFieldToFilter('identifier', $_item['identifier']);
        if($collection->getSize())
            $exist = true;
        $block = $this->_objectManager->create('Magento\Cms\Model\Block');
        if($overwrite) {
            if($exist) {
                $conflictingOldItems[] = $_item['identifier'];
                $block->load($_item['identifier']);
            }
        } else {
            if($exist) {
                continue;
            }
        }
        $conflictingOldItems[] = $_item['identifier'];
        $_item['store_id'] = array(0);
        $block->addData($_item)->save();
        $i++;
    }
}
  • Thank you so much! Tried it your way it and worked perfectly. – Onyekachi Amadi Feb 10 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.