/** * {@inheritdoc} */ protected function _beforeSave(AbstractModel $tag) { /** @var \Mirasvit\Blog\Model\Tag $tag */ if (!$tag->getData('url_key')) { $tag->setData('url_key', $this->filter->translitUrl($tag->getName())); } return parent::_beforeSave($tag); }
/** * Process page data before saving * * @param \Magento\Framework\Model\AbstractModel $object * @return $this * @throws \Magento\Framework\Model\Exception */ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object) { /* * For two attributes which represent timestamp data in DB * we should make converting such as: * If they are empty we need to convert them into DB * type NULL so in DB they will be empty and not some default value */ foreach (array('custom_theme_from', 'custom_theme_to') as $field) { $value = !$object->getData($field) ? null : $object->getData($field); $object->setData($field, $this->dateTime->formatDate($value)); } if (!$object->getData('identifier')) { $object->setData('identifier', $this->filter->translitUrl($object->getData('title'))); } if (!$this->getIsUniquePageToStores($object)) { throw new \Magento\Framework\Model\Exception(__('A page URL key for specified store already exists.')); } if (!$this->isValidPageIdentifier($object)) { throw new \Magento\Framework\Model\Exception(__('The page URL key contains capital letters or disallowed symbols.')); } if ($this->isNumericPageIdentifier($object)) { throw new \Magento\Framework\Model\Exception(__('The page URL key cannot be made of only numbers.')); } // modify create / update dates if ($object->isObjectNew() && !$object->hasCreationTime()) { $object->setCreationTime($this->_date->gmtDate()); } $object->setUpdateTime($this->_date->gmtDate()); return parent::_beforeSave($object); }
/** * Auto generate slug if empty * * @param \Magento\Framework\Model\AbstractModel $object * * @return bool */ protected function isSlugEmpty(\Magento\Framework\Model\AbstractModel $object) { if (empty($object->getData('slug'))) { $slug = $this->filter->translitUrl($object->getData('title')); $object->setData('slug', $slug); } return true; }
/** * Generate code from label * * @param string $label * @return string */ protected function generateCode($label) { $code = substr(preg_replace('/[^a-z_0-9]/', '_', $this->filterManager->translitUrl($label)), 0, 30); $validatorAttrCode = new \Zend_Validate_Regex(['pattern' => '/^[a-z][a-z_0-9]{0,29}[a-z0-9]$/']); if (!$validatorAttrCode->isValid($code)) { $code = 'attr_' . ($code ?: substr(md5(time()), 0, 8)); } return $code; }
/** * {@inheritdoc} */ public function normalize($string) { //@codingStandardsIgnoreStart $table = ['Š' => 'S', 'š' => 's', 'Đ' => 'Dj', 'đ' => 'dj', 'Ž' => 'Z', 'ž' => 'z', 'Č' => 'C', 'č' => 'c', 'Ć' => 'C', 'ć' => 'c', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'Ae', 'Å' => 'A', 'Æ' => 'A', 'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'Oe', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'Ue', 'Ý' => 'Y', 'Þ' => 'B', 'ß' => 'Ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'ae', 'å' => 'a', 'æ' => 'a', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'o', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'oe', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ý' => 'y', 'ý' => 'y', 'þ' => 'b', 'ÿ' => 'y', 'Ŕ' => 'R', 'ŕ' => 'r', 'ü' => 'ue', '/' => '', '&' => '', '(' => '', ')' => '']; //@codingStandardsIgnoreStop $string = strtr($string, $table); $string = $this->filter->translitUrl($string); return $string; }
/** * {@inheritdoc} */ protected function _beforeSave(DataObject $post) { /** @var \Mirasvit\Blog\Model\Post $post */ if (!$post->hasData('type')) { $post->setData('type', \Mirasvit\Blog\Model\Post::TYPE_POST); } if (!$post->getData('url_key')) { $post->setData('url_key', $this->filter->translitUrl($post->getName())); } $this->saveImage($post); return parent::_beforeSave($post); }
/** * {@inheritdoc} */ protected function _beforeSave(DataObject $category) { /** @var \Mirasvit\Blog\Model\Category $category */ parent::_beforeSave($category); if (!$category->getChildrenCount()) { $category->setChildrenCount(0); } if (!$category->getData('url_key')) { $category->setData('url_key', $this->filter->translitUrl($category->getName())); } if ($category->isObjectNew()) { if (!$category->hasParentId()) { $category->setParentId(1); } /** @var \Mirasvit\Blog\Model\Category $parent */ $parent = ObjectManager::getInstance()->create('Mirasvit\\Blog\\Model\\Category')->load($category->getParentId()); $category->setPath($parent->getPath()); if ($category->getPosition() === null) { $category->setPosition($this->getMaxPosition($category->getPath()) + 1); } $path = explode('/', $category->getPath()); $level = count($path) - ($category->getId() ? 1 : 0); $toUpdateChild = array_diff($path, [$category->getId()]); if (!$category->hasPosition()) { $category->setPosition($this->getMaxPosition(implode('/', $toUpdateChild)) + 1); } if (!$category->hasLevel()) { $category->setLevel($level); } if (!$category->getId() && $category->getPath()) { $category->setPath($category->getPath() . '/'); } $this->getConnection()->update($this->getEntityTable(), ['children_count' => new \Zend_Db_Expr('children_count+1')], ['entity_id IN(?)' => $toUpdateChild]); } return $this; }
/** * Format URL key from name or defined key * * @param string $str * @return string */ public function formatUrlKey($str) { return $this->filter->translitUrl($str); }
/** * Generate CMS page url key based on url_key entered by merchant or page title * * @param \Magento\Cms\Model\Page $cmsPage * @return string */ public function generateUrlKey($cmsPage) { $urlKey = $cmsPage->getIdentifier(); return $this->filterManager->translitUrl($urlKey === '' || $urlKey === null ? $cmsPage->getTitle() : $urlKey); }