src/Observers/UrlKeyObserver.php
protected function makeUrlKeyUnique($urlKey)
    {

        // initialize the entity type ID
        $entityType = $this->getEntityType();
        $entityTypeId = (integer) $entityType[MemberNames::ENTITY_TYPE_ID];

        // initialize the store view ID, use the admin store view if no store view has
        // been set, because the default url_key value has been set in admin store view
        $storeId = $this->getSubject()->getRowStoreId(StoreViewCodes::ADMIN);

        // initialize the counter
        $counter = 0;

        // initialize the counters
        $matchingCounters = array();
        $notMatchingCounters = array();

        // pre-initialze the URL key to query for
        $value = $urlKey;

        do {
            // try to load the attribute
            $productVarcharAttribute = $this->getProductBunchProcessor()
                                            ->loadProductVarcharAttributeByAttributeCodeAndEntityTypeIdAndStoreIdAndValue(
                                                MemberNames::URL_KEY,
                                                $entityTypeId,
                                                $storeId,
                                                $value
                                            );

            // try to load the product's URL key
            if ($productVarcharAttribute) {
                // this IS the URL key of the passed entity
                if ($this->isUrlKeyOf($productVarcharAttribute)) {
                    $matchingCounters[] = $counter;
                } else {
                    $notMatchingCounters[] = $counter;
                }

                // prepare the next URL key to query for
                $value = sprintf('%s-%d', $urlKey, ++$counter);
            }
        } while ($productVarcharAttribute);

        // sort the array ascending according to the counter
        asort($matchingCounters);
        asort($notMatchingCounters);

        // this IS the URL key of the passed entity => we've an UPDATE
        if (sizeof($matchingCounters) > 0) {
            // load highest counter
            $counter = end($matchingCounters);
            // if the counter is > 0, we've to append it to the new URL key
            if ($counter > 0) {
                $urlKey = sprintf('%s-%d', $urlKey, $counter);
            }
        } elseif (sizeof($notMatchingCounters) > 0) {
            // create a new URL key by raising the counter
            $newCounter = end($notMatchingCounters);
            $urlKey = sprintf('%s-%d', $urlKey, ++$newCounter);
        }

        // return the passed URL key, if NOT
        return $urlKey;
    }
protected function makeUrlKeyUnique($urlKey)
    {

        // initialize the entity type ID
        $entityType = $this->getEntityType();
        $entityTypeId = (integer) $entityType[MemberNames::ENTITY_TYPE_ID];

        // initialize the store view ID, use the admin store view if no store view has
        // been set, because the default url_key value has been set in admin store view
        $storeId = $this->getSubject()->getRowStoreId(StoreViewCodes::ADMIN);

        // initialize the counter
        $counter = 0;

        // initialize the counters
        $matchingCounters = array();
        $notMatchingCounters = array();

        // pre-initialze the URL key to query for
        $value = $urlKey;

        do {
            // try to load the attribute
            $productVarcharAttribute = $this->getProductBunchProcessor()
                                            ->loadProductVarcharAttributeByAttributeCodeAndEntityTypeIdAndStoreIdAndValue(
                                                MemberNames::URL_KEY,
                                                $entityTypeId,
                                                $storeId,
                                                $value
                                            );

            // try to load the product's URL key
            if ($productVarcharAttribute) {
                // this IS the URL key of the passed entity
                if ($this->isUrlKeyOf($productVarcharAttribute)) {
                    $matchingCounters[] = $counter;
                } else {
                    $notMatchingCounters[] = $counter;
                }

                // prepare the next URL key to query for
                $value = sprintf('%s-%d', $urlKey, ++$counter);
            }
        } while ($productVarcharAttribute);

        // sort the array ascending according to the counter
        asort($matchingCounters);
        asort($notMatchingCounters);

        // this IS the URL key of the passed entity => we've an UPDATE
        if (sizeof($matchingCounters) > 0) {
            // load highest counter
            $counter = end($matchingCounters);
            // if the counter is > 0, we've to append it to the new URL key
            if ($counter > 0) {
                $urlKey = sprintf('%s-%d', $urlKey, $counter);
            }
        } elseif (sizeof($notMatchingCounters) > 0) {
            // create a new URL key by raising the counter
            $newCounter = end($notMatchingCounters);
            $urlKey = sprintf('%s-%d', $urlKey, ++$newCounter);
        }

        // return the passed URL key, if NOT
        return $urlKey;
    }
protected function makeUrlKeyUnique ( $ urlKey ) { // initialize the entity type ID $ entityType = $ this -> getEntityType ( ) ; $ entityTypeId = ( integer ) $ entityType [ MemberNames :: ENTITY_TYPE_ID ] ; // initialize the store view ID, use the admin store view if no store view has // been set, because the default url_key value has been set in admin store view $ storeId = $ this -> getSubject ( ) -> getRowStoreId ( StoreViewCodes :: ADMIN ) ; // initialize the counter $ counter = 0 ; // initialize the counters $ matchingCounters = array ( ) ; $ notMatchingCounters = array ( ) ; // pre-initialze the URL key to query for $ value = $ urlKey ; do { // try to load the attribute $ productVarcharAttribute = $ this -> getProductBunchProcessor ( ) -> loadProductVarcharAttributeByAttributeCodeAndEntityTypeIdAndStoreIdAndValue ( MemberNames :: URL_KEY , $ entityTypeId , $ storeId , $ value ) ; // try to load the product's URL key if ( $ productVarcharAttribute ) { // this IS the URL key of the passed entity if ( $ this -> isUrlKeyOf ( $ productVarcharAttribute ) ) { $ matchingCounters [ ] = $ counter ; } else { $ notMatchingCounters [ ] = $ counter ; } // prepare the next URL key to query for $ value = sprintf ( '%s-%d' , $ urlKey , ++ $ counter ) ; } } while ( $ productVarcharAttribute ) ; // sort the array ascending according to the counter asort ( $ matchingCounters ) ; asort ( $ notMatchingCounters ) ; // this IS the URL key of the passed entity => we've an UPDATE if ( sizeof ( $ matchingCounters ) > 0 ) { // load highest counter $ counter = end ( $ matchingCounters ) ; // if the counter is > 0, we've to append it to the new URL key if ( $ counter > 0 ) { $ urlKey = sprintf ( '%s-%d' , $ urlKey , $ counter ) ; } } elseif ( sizeof ( $ notMatchingCounters ) > 0 ) { // create a new URL key by raising the counter $ newCounter = end ( $ notMatchingCounters ) ; $ urlKey = sprintf ( '%s-%d' , $ urlKey , ++ $ newCounter ) ; } // return the passed URL key, if NOT return $ urlKey ; }
Make's the passed URL key unique by adding the next number to the end.

@param string $urlKey The URL key to make unique

@return string The unique URL key
Make s the passed URL key unique by adding the next number to the end .