$alias = $resource->cleanAlias($pagetitle);
$alias = $resource->cleanAlias($resource->get('pagetitle'));
$resource->cleanAlias($resource->get('pagetitle'));
<?php $alias = $resource->cleanAlias($resource->get('pagetitle')); $alias = mb_substr($alias, 0, 50); // To Do: get value from friendly_alias_max_length $modx->event->output($alias);
Have you tried using $resource->set('alias', $alias); in the code? This would seem to save the new value to the $resource object's alias property prior to saving to the database.Thank you for answer ))
I am doing something similar (where I instead add longtitle and pagetitle together to get an alias) and it works on both New and Update documents.
you should be able to get the friendly_alias_max_length value by using $modx->getOption('friendly_alias_max_length')
<?php $alias = $resource->cleanAlias($resource->get('pagetitle')); $alias = mb_substr($alias, 0, 50); $resource->set('alias', $alias); $resource->save();
if ($resource->save() == false) { $validator = $resource->getValidator(); if ($validator->hasMessages()) { foreach ($validator->getMessages() as $message) { $this->addFieldError($message['field'],$this->modx->lexicon($message['message'])); } } }
What is the issue?Thank you for help! Module is fully functional and work just fine.
If there is another alias with the same string, the MODx processor will throw an error which may not be in your log and it won't save the info to the db until the alias error is fixed. You will need to rewrite the save() function as below:
if ($resource->save() == false) { $validator = $resource->getValidator(); if ($validator->hasMessages()) { foreach ($validator->getMessages() as $message) { $this->addFieldError($message['field'],$this->modx->lexicon($message['message'])); } } }
This should log any validation errors to your error log.
// Trim alias length $alias = $resource->cleanAlias($resource->get('pagetitle')); $alias = mb_substr($alias, 0, 50); // here you can change quantity of characters $resource->set('alias', $alias); $resource->save(); if ($resource->save() == false) { $validator = $resource->getValidator(); if ($validator->hasMessages()) { foreach ($validator->getMessages() as $message) { $this->addFieldError($message['field'],$this->modx->lexicon($message['message'])); } } }