Skip to content

Commit

Permalink
New life to this Field!
Browse files Browse the repository at this point in the history
Improved update for 2.3 and split redirect functionalities into a separate extension. This is so simple now :)
  • Loading branch information
Marco Sampellegrini committed Dec 14, 2012
1 parent 4743f17 commit 791f23e
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 395 deletions.
27 changes: 4 additions & 23 deletions README.markdown
Expand Up @@ -6,32 +6,13 @@ You could build, for example, your own in-site url shortener service having urls

* Author: [Marco Sampellegrini](http://github.com/alpacaaa)
* Release date: 30th March 2012
* Version: 0.4
* Version: 0.5

This is based on the [Entry URL Field](http://github.com/nickdunn/entry_url_field/) by Nick Dunn.


## Use

Let's say you have an url structure like `post/category-handle/post-id`.
You don't want to rely on services like bit.ly but that url is just too long. Time to give this a try then!

The first thing you'd want to do is to add the field to your `Post` section.

There's a `Url` field which should reflect your url structure so, in this case,
we're going to populate it with a value of `post/{entry/category/item/@handle}/{entry/@id}`.

It's pretty straightforward, you just use xpath expressions as you normally do when building `@href` links.
That's a relative url, so `$root` will be prepended automatically.


## Filtering

Ok, this is cool so far, but you need a way to specify if and how the redirect should happen.
This is done through datasource filtering.

Symphony 2.3.1 supports page params in index page, which is exactly what you need for this extension to work.
Simply add a parameter to your index page (let's call it `r`). Now create a datasource which filters by
this field with `{$r}` as value and attach it to the index page. Whenever a page isn't matched (you don't have
page handles like `55u`, don't you? :D), Symphony will fallback to the index page and execute the datasource.
If a match is found, it will automatically redirect to the url specified in the entry. Cool stuff.
This field just stores an unique alpha-numeric short identifier for each of your entry.
It used to perform redirect too, but I felt that would have required an extension of its own because there are
quite a few use cases that have to be adressed.
23 changes: 0 additions & 23 deletions extension.driver.php
Expand Up @@ -2,16 +2,6 @@

class extension_shorten extends Extension
{
public function getSubscribedDelegates()
{
return array(
array (
'page' =>'/frontend/',
'delegate' => 'EventFinalSaveFilter',
'callback' => 'cleanup'
)
);
}

public function uninstall()
{
Expand All @@ -24,23 +14,10 @@ public function install()
"CREATE TABLE `tbl_fields_shorten` (
`id` int(11) unsigned NOT NULL auto_increment,
`field_id` int(11) unsigned NOT NULL,
`redirect` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`hide` ENUM('yes', 'no') DEFAULT 'no',
PRIMARY KEY (`id`),
KEY `field_id` (`field_id`)
) ENGINE=MyISAM;"
);
}

public function cleanup($context)
{
$section = SectionManager::fetch($context['entry']->get('section_id'));

$field = $section->fetchFields('shorten');
if (!$field) return;

$field = current($field);
$entry_id = $context['entry']->get('id');
$field->update($entry_id);
}
}
1 change: 1 addition & 0 deletions extension.meta.xml
Expand Up @@ -13,6 +13,7 @@
</author>
</authors>
<releases>
<release version="0.5" date="2012-12-13" min="2.3" />
<release version="0.4" date="2012-03-30" min="2.3" />
<release version="0.3" date="2012-02-14" min="2.2.5" />
<release version="0.2" date="2010-02-09" min="2.1.2" />
Expand Down

0 comments on commit 791f23e

Please sign in to comment.