Magento 2.4.6 Deprecated Functionality: parse_url() Passing Null to Parameter #1

deprecatedmagento2magento2.4.6php8

I am using magento 2.4.6
One of my modules is adding meta tags in catalog. Not my code

case 'catalog_product_view':
                    $currentProduct = $this->_registry->registry('current_product');
                    if ($currentProduct && $currentProduct->getData('enable_index_follow')) {
                        $indexValue = $currentProduct->getData('index_value');
                        $followValue = $currentProduct->getData('follow_value');
                        $indexFollowValue = $this->_indexFollowBuilder->getIndexFollowValue($indexValue, $followValue);
                        $this->_pageConfig->setRobots($indexFollowValue);
                    }
                    if ($currentProduct && $currentProduct->getData('enable_canonical_url')) {
                        $canonicalUrl = $currentProduct->getData('canonical_url');
                        $urlOptions = parse_url($canonicalUrl);
                        if (!isset($urlOptions['scheme'])) {
                            $canonicalUrl = $baseUrl . $canonicalUrl;
                        }
                        $this->_pageConfig->addRemotePageAsset(
                            $canonicalUrl,
                            'canonical',
                            ['attributes' => ['rel' => 'canonical']]
                        );
                    }
                    break;

Line:

$urlOptions = parse_url($canonicalUrl);

throw error:

Deprecated Functionality: parse_url(): Passing null to parameter #1 ($url) of type string is deprecated

I can't fix it.

Any help please?

Best Answer

Instead of null you can use empty string.

chnage below code from

$canonicalUrl = $currentProduct->getData('canonical_url');

to

$canonicalUrl = $currentProduct->getData('canonical_url') ?? '';
Related Topic