Question about Filename field translation

8 posts by 3 authors in: Forums > CMS Builder
Last Post: September 14, 2012   (RSS)

By Shore - September 12, 2012

Hello,

When CMSB transforms the Filename field to the URL slug, does it get saved to the database somewhere so that I could query for the string?

If not, is there a way to build/decrypt the Filename-translation so that I could lookup a record based off the "Title" field for example?

In addition to the ability to lookup by the 'num' at the end of the URL, I'd like to be able to lookup records based on the URL value that CMSB generates from the Filename field.

Thanks!

Re: [cmscott] Question about Filename field translation

By Jason - September 13, 2012

Hi,

The value of the "filename fields" field isn't stored in the database but in the schema file of that given section. There really isn't an easy way of doing this, as a section could have multiple values in filename fields and there isn't a way of knowing which one was used unless you're looking at the values of an individual record.

That being said, if you wanted to base links off of a title instead of a num, you could build up your links manually.

for example:

<a href = "articleDetail.php?title=<?php echo urlencode($record['title']);?>"> <?php echo $record['title'];?> </a>

Then in your detail page, your where clause could be based off of the value of $_REQUEST['title']

If you require this type of searching while still using the value of the _link field, we can certainly look at creating some custom code for you to do that. If you're interested in this, please email consulting@interactivetools.com and we can go over some options.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Question about Filename field translation

By Shore - September 13, 2012

Hi Jason,

The problem I'm running into is that some of my Title field values contain special characters like the % character. These characters get stripped to build the URL slug.

So when I try to do what you suggest, it can't find the record.

Example:

TITLE: 10% off Product A
URL: 10-off-product-a

If I replace the hyphens with blank spaces, then lookup the title, can't find the record because of the % character.

Any ideas?

Thanks!

Re: [cmscott] Question about Filename field translation

By Dave - September 13, 2012

Hi cmscott,

There's a function in /lib/viewer_functions.php called getFilenameFieldValue() that generates the 'slug', but the problem without using the record number is there is nothing to prevent duplicates, so if you have two records:

Hello-World-1
Hello-World-2

Based on the slug without a record number you couldn't get to the original record. Will that be a problem?

Or to approach it another way, if you can let me know what you're trying to accomplish I might be able to think of some other suggestions.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [cmscott] Question about Filename field translation

By Dave - September 14, 2012

Hi cmscott,

Ok, I think I got it.

Just a heads up that we are working on a non-free "Permalinks" plugin that we'll be releasing in the next few weeks that does a lot of this. But pending the actual release date, price, feature list of that, etc I'd carry on with what you are doing.

I've attached a sample plugin I quickly wrote which checks "on save" in the CMS if a section has a field called "slug" and if it does it updates it with that value. If you want to give that a try just upload it to /plugins/, activate it, add a field called "slug" and save a record. (Note make sure you're running 2.17).

The code uses our internal utility functions but should be pretty readable so you can use it for another purpose if needed. It is as follows:

<?php


addAction('record_postsave', 'autoFillSlug', null, 4);

// after a record is saved, run this code
function autoFillSlug($tableName, $isNewRecord, $oldRecord, $recordNum) {
if (!array_key_exists('slug', $_REQUEST)) { return; }

// get section schema and record
$schema = loadSchema($tableName);
$record = mysql_get($tableName, $recordNum);

// generate the slug
require_once SCRIPT_DIR . "/lib/viewer_functions.php";
$slug = getFilenameFieldValue($record, @$schema['_filenameFields']);
$slug .= $recordNum;

// update the record
mysql_update($tableName, $recordNum, null, array('slug' => $slug));
}

?>


Obviously, I can't help too much with custom code, but I hope this points you in the right direction. Let me know if that works for you and if you need anything else. Cheers!
Dave Edis - Senior Developer
interactivetools.com
Attachments:

autofillslug.php 1K

Re: [Dave] Question about Filename field translation

By Shore - September 14, 2012

Hi Dave,

Looks like that could work, I'll test it out later today and let you know how I make out.

Very helpful :)

Thanks!

Re: [cmscott] Question about Filename field translation

By Shore - September 14, 2012

Hi Dave,

Works like a charm! Thank you sir.

Cheers
[:)]