I want to edit Magento 2 core function:
/**
* Build category URL path
*
* @param \Magento\Catalog\Api\Data\CategoryInterface|\Magento\Framework\Model\AbstractModel $category
* @return string
*/
public function getUrlPath($category)
{
if (in_array($category->getParentId(), [Category::ROOT_CATEGORY_ID, Category::TREE_ROOT_ID])) {
return '';
}
$path = $category->getUrlPath();
if ($path !== null && !$category->dataHasChangedFor('url_key') && !$category->dataHasChangedFor('parent_id')) {
return $path;
}
$path = $category->getUrlKey();
if ($path === false) {
return $category->getUrlPath();
}
if ($this->isNeedToGenerateUrlPathForParent($category)) {
$parentPath = $this->getUrlPath(
$this->categoryRepository->get($category->getParentId(), $category->getStoreId())
);
$path = $parentPath === '' ? $path : $parentPath . '/' . $path;
}
return $path;
}
I am new to Magento 2 and I want to edit the aforementioned code. The problem is that, after I edit the core file and later on when Magento executes an update, my edited codes will be reset.
I searched on Google and found out that we can create plugins to edit these core functions, But I am getting confused. I can't find simple tutorials or reference about how to do it.
There are some answers on Stackexchange, which says to Add override preference in di.xml, then create Create block,model and controller file in your module. I am not able to understand these procedures? Can anyone please explain what is block,model and controller? please How should I use it with the above function.
Kindly guide me with the same. Any help will be appreciated.
Thank You