Custom ['_Link']

13 posts by 4 authors in: Forums > CMS Builder
Last Post: April 27, 2010   (RSS)

By Perchpole - April 25, 2010

Hello, All -

Due to the way my latest site works I have to include a category reference in each link. Failure to do so means that certain category-specific functions (such as breadcrumbs and page headers) would cease to work.

As a result, the humble ['_link'] value has become all but redundant. This is a shame as it means (in my case) I have to hard code every link on the site.

Would it be possible to define the structure of "_link" on a site-by-site basis?

I would like to modify the structure so that it inserts "?category=x" into the default code:

xyz/pageDetail.php?category=x&article-1

I assume this would need to be done in the viewer_functions.php or possibly in the page template itself.

Either way, as the operability/flexibility of CMSB increases such modifications are going to become a must. Indeed, I hope an init.php - full of user-defined variables - is already on the drawing board!

Any thoughts?

:0)

Perchpole

Re: [Perchpole] Custom ['_Link']

By aev - April 25, 2010

I second that!

A way of creating dynamic/advanced [_link] values from within the CMSB admin would be a great addition to CMSB. The way it works today has always been totally useless for the sites we build..

-aev-

Re: [Jason] Custom ['_Link']

By Perchpole - April 26, 2010

Hi, Jason -

Sounds like an interesting solution but, sadly, it doesn't do what I need. Your approach returns the values of the selected fields. It does not permit me to define any additional text.

For it to work I would need to insert category=x into the url - so that it can be passed to the $_REQUEST['category']; code in the resulting page header.

:0/

Perch

Re: [Perchpole] Custom ['_Link']

By Jason - April 26, 2010

Hi,

You can still access those variables. For example, if I had 2 variables in the URL string (say category and num), the string may look like this: pageDetails.php?x-4
Where category=x and num=4.

You could access them by putting this code near the top of your page:
$variables=explode('-',$_SERVER['QUERY_STRING']);
$_REQUEST['category']=$variables[0];
$_REQUEST['num']=$variables[1];


I can now access those 2 variables anywhere below this in the code by using $_REQUEST['category'] or $_REQUEST['num']

Hopefully 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: [Perchpole] Custom ['_Link']

By Dave - April 26, 2010

Hi Perch,

We ourselves use the _link for simple sections, but switch over to custom manually created links anytime we need to do anything more advanced.

We've found this to be the fastest, because _link is always going to be what you expect, it's obvious when looking at the code when links have been customized for something more advanced, and the program doesn't limit what you can do for custom links.

You can access the seo "filename" part of the generated url in this variable though: $record['_filename']

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

Re: [Dave] Custom ['_Link']

By Perchpole - April 26, 2010


We ourselves use the _link for simple sections, but switch over to custom manually created links anytime we need to do anything more advanced.



Hi, Dave -

Yes, I apreciate that. I just wondered if the idea was within the realms of do-ability or not!

Still hoping for an init.php set-up though.

:0)

Perch

Re: [Perchpole] Custom ['_Link']

By Dave - April 26, 2010

>Still hoping for an init.php set-up though.

I'm not sure if we're both talking about the same thing, but if we want to have custom modifications that are specific to a site they way we do that is to create a custom plugin for a site named /cmsAdmin/plugins/sitename-custom.php

And if I wanted to change how _link worked I'd put it at the top of the page, something like this:

// load records
list($newsRecords, $newsMetaData) = getRecords(array(
'tableName' => 'news',
));

// create custom _link values
foreach (array_keys($newsRecords) as $index) {
$record = &$newsRecords[$index];

$record['_link'] = '';
$record['_link'] .= "?category=" . urlencode($record['category']);
$record['_link'] .= "&" .$record['_filename']. '-' . $record['num'];

unset($record);
}


Basically what that does is overwrite _link for all the records.

If you wanted to modify how _link worked site wide I could add a plugin filter hook for that value to CMSB. Then you could have a plugin that output custom _link values based on the table, record, etc.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Custom ['_Link']

By aev - April 27, 2010

Dave,

this is a great tip! I'll bookmark this one.

Just one thing.. what is the & in the line after the foreach statement:
$record = &$newsRecords[$index];

-aev-

Re: [aev] Custom ['_Link']

By Dave - April 27, 2010

Hi Aev,

The & is for "Assigning by reference":
http://php.net/manual/en/language.references.whatdo.php

Basically it means: Instead of copying this, make bother references point to the same data so when you modify one both get modified. By default when you say $a = $b in PHP you get a copy and not a pointer to the original.

This code is simple enough that it's not really required, but it can make it more readable. You could also say:

// create custom _link values
foreach (array_keys($newsRecords) as $index) {
$newsRecords[$index]['_link'] = '';
$newsRecords[$index]['_link'] .= "?category=" . urlencode($record['category']);
$newsRecords[$index]['_link'] .= "&" .$record['_filename']. '-' . $record['num'];
}


But you end up having to paste your variable name a few more times and it's a little less readable.

You could also drop the first line = '' and have this:

// create custom _link values
foreach (array_keys($newsRecords) as $index) {
$newsRecords[$index]['_link'] = "?category=" . urlencode($record['category']);
$newsRecords[$index]['_link'] .= "&" .$record['_filename']. '-' . $record['num'];
}


For code in the forum I just try to make it as foolproof and clear as possible... Which is sometimes a tricky balance. :)

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