using field:label in permalink? hack or feature request?

By kitsguru - March 21, 2019

I have a content_type list field that I want to use to build the permalink with using the label.

e.g. content_type list

1|meals
2|activity
3|special event

I want the permalink to look like:

meals/lunch
meals/dinner
activity/art-show
activity/bingo
special-event/st-paddys-day
special-event/mothers-day-festival

I tried using the permalink autopopulate field but it gives:

1-lunch

Any ideas on how I might set this up or can we make it a feature request to be able to use dynamic prefixes based on record content?

Jeff Shields

By kitsguru - April 17, 2019

Ok so I am doing a custom plugin for an events table using the sample code. However I am getting an error.

addAction('record_presave', 'permalink_custom_permalinks', 1, 3);

function permalink_custom_permalinks($tableName, $isNewRecord, $oldRecord)
{
    $schema = loadSchema($tableName);
    $prefix = @$schema['permalink']['defaultValue'];

    // create custom permalink on first save of record
    if ($tableName === 'events') {
        $date = date("Y-m-d", @$_REQUEST['start_date']);
        $_REQUEST['permalink'] = $prefix . $date .'/'. @$_REQUEST['title'];
        trigger_error($_REQUEST['permalink']);
    }
}

The title is "Elsie's Show", prefix is "events"

The output I am getting is:

event/1969-12-31/Elsie's Show

and the error is 

Notice: event/1969-12-31/Elsie's Show in /Volumes/J/yaaws4/html/www/cmsb/plugins/yaaApp/yaaApp.php on line 125
Permalink can only contain these chars (a-z0-9/-.)!

It appears that the permalink is not getting processed to replace the spaces with hyphens and apostrophe stripped out etc.

Jeff Shields

By daniel - April 17, 2019

Hi Jeff,

You're right - the value you're generating is going through the same error checking as if it was manually entered into the permalink field. You should be able to correct this by adding the following to the end of your if() block (where you're currently calling trigger_error()):

$_REQUEST['permalink'] = _permalink_generate_formatInputText( $_REQUEST['permalink'] );

Also, it looks like start_date is either empty or doesn't contain a timestamp, causing it to default to 1969-12-31. One quick thing you can try to fix that is add a strtotime() call to the date generation:

$date = date("Y-m-d", strtotime(@$_REQUEST['start_date']));

(strtotime() is a really handy function that can convert most string representations of dates into a timestamp: https://www.php.net/manual/en/function.strtotime.php)

Let me know if this helps sort out your issue!

Thanks,

Daniel
Technical Lead
interactivetools.com