當前位置: 首頁>>代碼示例>>PHP>>正文


PHP BaseElementModel::getUrlFormat方法代碼示例

本文整理匯總了PHP中BaseElementModel::getUrlFormat方法的典型用法代碼示例。如果您正苦於以下問題:PHP BaseElementModel::getUrlFormat方法的具體用法?PHP BaseElementModel::getUrlFormat怎麽用?PHP BaseElementModel::getUrlFormat使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在BaseElementModel的用法示例。


在下文中一共展示了BaseElementModel::getUrlFormat方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: prepareAncestorsForElement

 /**
  * @param BaseElementModel $element
  * @param $data
  *
  * @return null|CategoryModel
  */
 private function prepareAncestorsForElement(BaseElementModel $element, $data)
 {
     $parentCategory = null;
     $data = $this->freshenString($data);
     // Don't connect empty fields
     if (!empty($data)) {
         // This we append before the slugified path
         $categoryUrl = str_replace('{slug}', '', $element->getUrlFormat());
         // Find matching element by URI (dirty, not all categories have URI's)
         $criteria = craft()->elements->getCriteria(ElementType::Category);
         $criteria->groupId = $element->groupId;
         $criteria->uri = $categoryUrl . craft()->import->slugify($data);
         $criteria->limit = 1;
         $parentCategory = $criteria->first();
     }
     return $parentCategory;
 }
開發者ID:transomdesign,項目名稱:transom-craft-starter,代碼行數:23,代碼來源:Import_CategoryService.php

示例2: setUniqueUri

 /**
  * Sets the URI on an element using a given URL format, tweaking its slug if necessary to ensure it's unique.
  *
  * @param BaseElementModel $element
  *
  * @throws Exception
  */
 public static function setUniqueUri(BaseElementModel $element)
 {
     $urlFormat = $element->getUrlFormat();
     // No URL format, no URI.
     if (!$urlFormat) {
         $element->uri = null;
         return;
     }
     // No slug, or a URL format with no {slug}, just parse the URL format and get on with our lives
     if (!$element->slug || !static::doesUrlFormatHaveSlugTag($urlFormat)) {
         $element->uri = craft()->templates->renderObjectTemplate($urlFormat, $element);
         return;
     }
     $uniqueUriConditions = array('and', 'locale = :locale', 'uri = :uri');
     $uniqueUriParams = array(':locale' => $element->locale);
     if ($element->id) {
         $uniqueUriConditions[] = 'elementId != :elementId';
         $uniqueUriParams[':elementId'] = $element->id;
     }
     $slugWordSeparator = craft()->config->get('slugWordSeparator');
     $maxSlugIncrement = craft()->config->get('maxSlugIncrement');
     for ($i = 0; $i < $maxSlugIncrement; $i++) {
         $testSlug = $element->slug;
         if ($i > 0) {
             $testSlug .= $slugWordSeparator . $i;
         }
         $originalSlug = $element->slug;
         $element->slug = $testSlug;
         $testUri = craft()->templates->renderObjectTemplate($urlFormat, $element);
         // Make sure we're not over our max length.
         if (strlen($testUri) > 255) {
             // See how much over we are.
             $overage = strlen($testUri) - 255;
             // Do we have anything left to chop off?
             if (strlen($overage) > strlen($element->slug) - strlen($slugWordSeparator . $i)) {
                 // Chop off the overage amount from the slug
                 $testSlug = $element->slug;
                 $testSlug = substr($testSlug, 0, strlen($testSlug) - $overage);
                 // Update the slug
                 $element->slug = $testSlug;
                 // Let's try this again.
                 $i -= 1;
                 continue;
             } else {
                 // We're screwed, blow things up.
                 throw new Exception(Craft::t('The maximum length of a URI is 255 characters.'));
             }
         }
         $uniqueUriParams[':uri'] = $testUri;
         $totalElements = craft()->db->createCommand()->select('count(id)')->from('elements_i18n')->where($uniqueUriConditions, $uniqueUriParams)->queryScalar();
         if ($totalElements == 0) {
             // OMG!
             $element->slug = $testSlug;
             $element->uri = $testUri;
             return;
         } else {
             $element->slug = $originalSlug;
         }
     }
     throw new Exception(Craft::t('Could not find a unique URI for this element.'));
 }
開發者ID:jmstan,項目名稱:craft-website,代碼行數:68,代碼來源:ElementHelper.php


注:本文中的BaseElementModel::getUrlFormat方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。