We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Gypsy

Development and Programming

Brandon Kelly's avatar
Brandon Kelly
257 posts
16 years ago
Brandon Kelly's avatar Brandon Kelly

A few days ago, slapshotw and I were discussing / collaboratively venting about the way EE handles custom fields + field groups + weblogs. There’s a lot of repetition involved: It’s not rare for a site to have the same field duplicated throughout multiple field groups, and multiple near-identical field groups thanks to little differences.

But those days are gone now, with a little help from my new extension, Gypsy!

In a nutshell, Gypsy frees your custom fields from their field groups, enabling you to assign them directly to your weblogs.

Get it now! brandon-kelly.com/apps/gypsy

Usage You can turn any of your custom fields into “gypsies” from the Edit Custom Field page. Find the preference labelled “Is this a gypsy field?” and set it to Yes. Then choose which weblogs you want your field to appear in from the multi-select box that appears below.

Enjoy!

       
Chad Crowell's avatar
Chad Crowell
242 posts
16 years ago
Chad Crowell's avatar Chad Crowell

I feel like I just took some LSD. This is so OUT THERE! Zany!

       
Nathan Pitman's avatar
Nathan Pitman
531 posts
16 years ago
Nathan Pitman's avatar Nathan Pitman

So in theory you could create a single custom field group and then add a ton of custom fields which are ‘gypsies’ and then assign them to the relevant weblogs, negating the need for multiple custom field groups at all?

If so sounds like a really powerful extension and I can’t wait to give it a shot! 😊

       
Ryan M.'s avatar
Ryan M.
1,511 posts
16 years ago
Ryan M.'s avatar Ryan M.

I played with the beta for a bit yesterday (thanks for that!) and was pretty impressed.

I will have to test this a bit to see about best way to implement. Right now you can do one of two things: make a Gypsy field in a particular weblog’s custom field group and share it with other weblogs, or make a dedicated “Gypsy” custom field group that isn’t assigned to any weblog, and store all your Gypsy fields there (like Nathan just said above…he posted as I was writing this).

Only other thing to watch out for is the field order. If your gypsy field is “100” - that may show up first in one weblog Pub/Edit page, or 10th in another. If you are as anal as I am about what order your fields are in, you will have to adjust non-gypsy fields flow around them.

Much needed extension. Great work!

       
Brandon Kelly's avatar
Brandon Kelly
257 posts
16 years ago
Brandon Kelly's avatar Brandon Kelly

Quick note – I have not tested this with PHP4. I suspect it will work just fine, but if you have PHP4 please verify!

       
Nathan Pitman's avatar
Nathan Pitman
531 posts
16 years ago
Nathan Pitman's avatar Nathan Pitman

I guess the only solution to that would be to somehow introduce a set of secondary entry fields which allowed you to set the order of that gypsy field relative to each assigned weblog… could get messy but I’m sure Brandon would come up with a simple but brilliantly clever solution. 😊

       
Brandon Kelly's avatar
Brandon Kelly
257 posts
16 years ago
Brandon Kelly's avatar Brandon Kelly
I guess the only solution to that would be to somehow introduce a set of secondary entry fields which allowed you to set the order of that gypsy field relative to each assigned weblog… could get messy but I’m sure Brandon would come up with a simple but brilliantly clever solution. 😊

My current stance on field order is: it works as-is. If you set a Gypsy field to order #100, you can then edit your other fields’ order #s to work around the Gypsy field.

       
Nathan Pitman's avatar
Nathan Pitman
531 posts
16 years ago
Nathan Pitman's avatar Nathan Pitman

…and in reality how often is an admin likely to fiddle with field order once the initial site setup is done… so yes there’s really no need for a complicated solution. 😊

       
Ryan M.'s avatar
Ryan M.
1,511 posts
16 years ago
Ryan M.'s avatar Ryan M.

Another way to have approached this would have been to allow a weblog to have multiple field groups, but this is ultimately more flexible.

       
Brandon Kelly's avatar
Brandon Kelly
257 posts
16 years ago
Brandon Kelly's avatar Brandon Kelly
Another way to have approached this would have been to allow a weblog to have multiple field groups, but this is ultimately more flexible.

For sure. I considered at least three different approaches to Gypsy, but settled on this because it had the best flexibility-to-difficulty ratio.

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

Wow… this looks sweet. Good work Brandon! Can’t wait to try it out

       
Casey Reid's avatar
Casey Reid
82 posts
16 years ago
Casey Reid's avatar Casey Reid

Lots of familiar commenters on this thread thus far. Hello fellow Twitterers 😊 Absolutely fantastic idea Brandon. Can’t wait to try this out.

       
Linda A's avatar
Linda A
647 posts
16 years ago
Linda A's avatar Linda A

This sounds great. 😊

I don’t suppose its possible to extend it with some kind of migration utility for existing sites that already have fields repeated over several custom field groups?

       
Brandon Kelly's avatar
Brandon Kelly
257 posts
16 years ago
Brandon Kelly's avatar Brandon Kelly
I don’t suppose its possible to extend it with some kind of migration utility for existing sites that already have fields repeated over several custom field groups?

Here’s a little script I just wrote that will copy all of your field data from one field to another:

<?php


// -------------------
//  Configuration
// -------------------


// Old Field Names
$old_fields = array(
    'old_field_1',
    'old_field_2',
    'old_field_3'
    // etc.
);

// Gypsy Field Name
$gypsy_field = 'gypsy_field';

// Database Server
$db_server = 'localhost';

// Database User Name
$db_username = 'username';

// Database User Password
$db_password = 'password';

// Database Name
$db_name = 'my_website';


// -------------------
//  Script
// -------------------


// Connect to the database
$db_link = mysql_connect($db_server, $db_username, $db_password)
    OR exit('Could not connect to MySQL.');
mysql_select_db($db_name, $db_link)
    OR exit('Could not connect to the database.');

// Get the Gypsy field's ID
$gypsy_field_result = mysql_query("SELECT field_id
                                   FROM exp_weblog_fields
                                   WHERE field_name = '{$gypsy_field}'", $db_link) OR exit(mysql_error());
if ( ! mysql_num_rows($gypsy_field_result))
    exit("No field exists with the name \"{$gypsy_field}\"");
$gypsy_field_id = 'field_id_'.mysql_result($gypsy_field_result, 0);

// Get the old fields' IDs
$old_fields_result = mysql_query("SELECT field_id
                                  FROM exp_weblog_fields
                                  WHERE field_name IN ('".implode(',', $old_fields)."')", $db_link) OR exit(mysql_error());
if ( ! mysql_num_rows($old_fields_result))
    exit('No fields exist with the names "'.implode('", "', $old_fields).'"');

// Update Gypsy field's data with content from each old field
while($row = mysql_fetch_row($old_fields_result)) {
    $old_field_id = "field_id_{$row[0]}";
    if ($old_field_id == $gypsy_field_id)
        continue;
    
    $data_result = mysql_query("SELECT entry_id, {$old_field_id} data
                                FROM exp_weblog_data
                                WHERE {$old_field_id} != ''", $db_link) OR exit(mysql_error());
    if ( ! mysql_num_rows($data_result))
        continue;
    while($data_row = mysql_fetch_assoc($data_result)) {
        mysql_query("UPDATE exp_weblog_data
                     SET {$gypsy_field_id} = '".addslashes($data_row['data'])."'
                     WHERE entry_id = '{$data_row['entry_id']}'", $db_link) OR exit(mysql_error());
    }
}

echo "Success!";

?>

First go into your CP and create your new gypsy field. Then save that script as a PHP file, change the Configuration variables at the top, upload it to your website, and access it from your browser. (And when you’re done migrating, delete the old fields AND DELETE THAT SCRIPT!)

       
Linda A's avatar
Linda A
647 posts
16 years ago
Linda A's avatar Linda A

Oh, wow, fabulous. Thank you so much. 😊

       
1 2 3 Last

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.