Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProductUrlRewrite.php
Go to the documentation of this file.
1 <?php
7 
17 
22 {
23  const XML_PATH_SEO_SAVE_HISTORY = 'catalog/seo/save_rewrites_history';
24 
28  protected $locator;
29 
33  protected $scopeConfig;
34 
38  protected $arrayManager;
39 
45  public function __construct(
49  ) {
50  $this->locator = $locator;
51  $this->arrayManager = $arrayManager;
52  $this->scopeConfig = $scopeConfig;
53  }
54 
58  public function modifyMeta(array $meta)
59  {
60  if ($this->locator->getProduct()->getId()) {
61  $meta = $this->addUrlRewriteCheckbox($meta);
62  }
63 
64  return $meta;
65  }
66 
70  public function modifyData(array $data)
71  {
72  return $data;
73  }
74 
81  protected function addUrlRewriteCheckbox(array $meta)
82  {
83  $urlPath = $this->arrayManager->findPath(
85  $meta,
86  null,
87  'children'
88  );
89 
90  if ($urlPath) {
91  $containerPath = $this->arrayManager->slicePath($urlPath, 0, -2);
92  $urlKey = $this->locator->getProduct()->getData('url_key');
93  $saveRewritesHistory = $this->scopeConfig->isSetFlag(
94  self::XML_PATH_SEO_SAVE_HISTORY,
96  $this->locator->getProduct()->getStoreId()
97  );
98 
99  $meta = $this->arrayManager->merge($containerPath, $meta, [
100  'arguments' => [
101  'data' => [
102  'config' => [
103  'component' => 'Magento_Ui/js/form/components/group',
104  ],
105  ],
106  ],
107  ]);
108 
109  $checkbox['arguments']['data']['config'] = [
110  'componentType' => Field::NAME,
111  'formElement' => Checkbox::NAME,
112  'dataType' => Text::NAME,
113  'component' => 'Magento_Catalog/js/components/url-key-handle-changes',
114  'valueMap' => [
115  'false' => '',
116  'true' => $urlKey
117  ],
118  'imports' => [
119  'urlKey' => '${ $.provider }:data.product.' . ProductAttributeInterface::CODE_SEO_FIELD_URL_KEY,
120  'handleUseDefault' => '${ $.parentName }.url_key:isUseDefault',
121  'handleChanges' => '${ $.provider }:data.product.'
123  ],
124  'description' => __('Create Permanent Redirect for old URL'),
125  'dataScope' => 'url_key_create_redirect',
126  'value' => $saveRewritesHistory ? $urlKey : '',
127  'checked' => $saveRewritesHistory,
128  ];
129 
130  $meta = $this->arrayManager->merge(
131  $urlPath . '/arguments/data/config',
132  $meta,
133  ['valueUpdate' => 'keyup']
134  );
135  $meta = $this->arrayManager->merge(
136  $containerPath . '/children',
137  $meta,
138  ['url_key_create_redirect' => $checkbox]
139  );
140  $meta = $this->arrayManager->merge(
141  $containerPath . '/arguments/data/config',
142  $meta,
143  ['breakLine' => true]
144  );
145  }
146 
147  return $meta;
148  }
149 }
__construct(LocatorInterface $locator, ArrayManager $arrayManager, ScopeConfigInterface $scopeConfig)
__()
Definition: __.php:13