I use the same name for multiple products, so when I create a product page with the same name I got an error because I used the name already.
Is it possible to add an auto-increment integer at the end of an url?
Something like:
www.website.nl/product
www.website.nl/product-01
www.website.nl/product-02
www.website.nl/product-03
Thanks Lucas,
Do you maybe know some example plugins that can do such action?
this needs some testing,
but something like that as a plugin for onBeforeDocFormSave could do it:
<?php
// Get the current event
$e = &$modx->event;
$r = &$e->params['resource'];
// set new alias
$alias = $r->get('alias');
$id = $r->get('id');
$pagetitle = $r->get('pagetitle');
//here you need to check for existing aliases and create a new incremented one
//this needs some testing
$c = $modx->newQuery('modResource', array('alias' => $alias, 'id:!=' => $id));
if ($collection = $modx->getCollection('modResource', $c)) {
foreach ($collection as $key => $object) {
$existing[] = $object->get('alias');
}
$c = $modx->newQuery('modResource', array('pagetitle' => $pagetitle, 'id:!=' => $id));
if ($collection = $modx->getCollection('modResource', $c)) {
foreach ($collection as $key => $object) {
$existing[] = $object->get('alias');
}
}
$loop = true;
$i = 0;
while ($loop) {
if ($i == 0) {
$suffix = '';
} else {
$suffix = $i < 10 ? '-0' . $i : '-' . $i;
}
$newalias = $pagetitle . $suffix;
$newalias = $r->cleanAlias($newalias);
$loop = in_array($newalias, $existing);
$i++;
}
$r->set('alias', $newalias);
}
Thanks Bruno17 for your example will try it right now!
just edited this row:
$newalias = $r->cleanAlias($newalias);
Thanks Bruno17,
I tried with the first code and one with the correction but it give the (Please correct the errors in your form before submitting) error on the alias field.
set automatic-alias system-setting to yes and try again
Thanks Bruno17, I tried that but still it give the error, attached my settings.
which version of revo? I’m on 2.1.2 and seems to work.