Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateUrlPath.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
17 use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
18 
23 {
27  private $categoryUrlPathGenerator;
28 
32  private $categoryUrlRewriteGenerator;
33 
37  private $storeViewService;
38 
42  private $urlPersist;
43 
50  public function __construct(
51  CategoryUrlPathGenerator $categoryUrlPathGenerator,
52  CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator,
53  UrlPersistInterface $urlPersist,
54  StoreViewService $storeViewService
55  ) {
56  $this->categoryUrlPathGenerator = $categoryUrlPathGenerator;
57  $this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator;
58  $this->urlPersist = $urlPersist;
59  $this->storeViewService = $storeViewService;
60  }
61 
71  public function afterSave(
72  CategoryResource $subject,
73  CategoryResource $result,
75  ): CategoryResource {
76  $parentCategoryId = $category->getParentId();
77  if ($category->isObjectNew()
78  && !$category->isInRootCategoryList()
79  && !empty($parentCategoryId)
80  ) {
81  foreach ($category->getStoreIds() as $storeId) {
82  if (!$this->isGlobalScope((int)$storeId)
83  && $this->storeViewService->doesEntityHaveOverriddenUrlPathForStore(
84  $storeId,
85  $parentCategoryId,
87  )
88  ) {
89  $category->setStoreId($storeId);
90  $this->updateUrlPathForCategory($category, $subject);
91  $this->urlPersist->replace($this->categoryUrlRewriteGenerator->generate($category));
92  }
93  }
94  }
95 
96  return $result;
97  }
98 
105  private function isGlobalScope(int $storeId): bool
106  {
108  }
109 
117  private function updateUrlPathForCategory(Category $category, CategoryResource $categoryResource): void
118  {
119  $category->unsUrlPath();
120  $category->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($category));
121  $categoryResource->saveAttribute($category, 'url_path');
122  }
123 }
__construct(CategoryUrlPathGenerator $categoryUrlPathGenerator, CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator, UrlPersistInterface $urlPersist, StoreViewService $storeViewService)
afterSave(CategoryResource $subject, CategoryResource $result, AbstractModel $category)