Quantcast
Channel: Joomla 4 - Check if Alias is Unique - Joomla Stack Exchange
Viewing all articles
Browse latest Browse all 2

Answer by Sébastien Merour for Joomla 4 - Check if Alias is Unique

$
0
0

I finally "partially" solved this by moving a piece of code, and replacing the date by an increment :

    public function check()    {        try        {            parent::check();        }        catch (\Exception $e)        {            $this->setError($e->getMessage());            return false;        }                // Check the publish down date is not earlier than publish up.                if ($this->publish_down > $this->_db->getNullDate() && $this->publish_down < $this->publish_up) {                    $this->setError(Text::_('JGLOBAL_START_PUBLISH_AFTER_FINISH'));                    return false;                }                // Set publish_up, publish_down to null if not set                if (!$this->publish_up) {                    $this->publish_up = null;                }                if (!$this->publish_down) {                    $this->publish_down = null;                }    jimport('joomla.filter.output');    if (empty($this->alias)) {        $this->alias = $this->name;    }    $pk = $this->id;    if (empty($pk)) {        $pk = 0;    }            $originAlias = $this->alias;    $this->alias = EventHelper::stringURLSafe(trim($this->alias));         if (empty($this->alias)) {             $this->alias = $originAlias;             $this->alias = EventHelper::replace($this->alias);             $this->alias = JFilterOutput::stringURLSafe($this->alias);         };    $db = Factory::getDbo();    $query = $db->getQuery(true);          $query->select('COUNT(*)')        ->from('#__rwlevents')        ->where('`alias` = ' .$db->Quote($this->alias))        ->where('`id` <> '.$pk);    $db->setQuery($query);            $newalias = $this->alias;    $count = $db->loadResult();            $increment = $count + 1;            if (!empty($count) AND $count >= 1) {             $this->alias = EventHelper::replace($newalias);             $this->alias = JFilterOutput::stringURLSafe($newalias).'-'. $increment;    }    return true;}

The only issue I have now is that when I have more than 1 Dupliacte, the Alias become something like this :

concert-au-3-arena-2-2

concert-au-3-arena-2-2-2

concert-au-3-arena-2-2-2-2


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>