Hey all,
I'm looking for a plugin to change the default behavior of the auto-generated Alias field. I'd like it to auto-generate from the Longtitle field instead of the Pagetitle, but keep all the other default functionality, ie. Auto-fill if blank but allow for manual change, duplicate detection, character cleaning, etc...
I thought I'd try my hand at PHP (not fluent) and wrote the following:
<?php
if ($resource->get('alias') == ''){
$resource->set('alias',$resource->cleanAlias($resource->get('longtitle')));
$resource->save();
}
return;
However, this does not work as planned. Thoughts?
Front End Designer + Developer for Eternity
That looks right to me, though I would change the if to:
if (empty($resource->get('alias'))) {
}
Try setting the automatic_alias System Setting to "No", since you are handling that yourself.
What System Event is your plugin attached to?
<?php
if (empty($resource->get('alias'))) {
$resource->set('alias',$resource->cleanAlias($resource->get('longtitle')));
$resource->save();
}
return;
I changed the if to your suggestion, however that locks the site up in "forever saving" loop.
I have the plugin on the OnBeforeDocFormSave thinking I could beat the empty Alias validation... however an empty Alias prevents me from saving instead of taking the Longtitle as it should.
Front End Designer + Developer for Eternity
discuss.answer
Sorry, my bad. You can't use empty() inside an if statement like that unless you have PHP 5.5. It should be:
$a = $resource->get('alias');
if (empty($a)) {
If that doesn't work, it's kind of hacky, but you might be able to turn automatic_alias back on, then in a plugin connected to OnDocFormSave, detect aliases auto-generated from the pagetitle, and convert them. The down side would be that the user could never get the pagetitle version even if they wanted it.
(untested)
$savedAlias = $resource->get('alias');
$pagetitle = $resource->get('pagetitle');
if ($savedAlias === $resource->cleanAlias($pagetitle)) {
$resource->set('alias', $resource->cleanAlias($resource->get('longtitle')));
$resource->save();
}
[ed. note: BobRay last edited this post 10 years, 2 months ago.]
That second "untested" version works great! Thank You!
Front End Designer + Developer for Eternity
Thanks Jako.
However, Bob's works like a charm.
Your first example doesn't allow for a manual Alias to be entered and override the Longtitle auto-fill.
You second example doesn't work due to the Alias' validator. It won't let the resource save or the plugin run if the Alias is blank on Save.
Front End Designer + Developer for Eternity
My version has its flaws too.
If you enter a manual alias that happens to match the auto-generated one from the pagetitle, it will be converted to one based on the longtitle.
-
☆ A M B ☆
- 1,031 Posts
Sorry Bob, I obviously forgot that the generation of the default alias runs before the plugin and your alias generation runs only when the alias was generated from the pagetitle.
No problem, Jako, I'm misreading code around here all the time.