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!
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! 😊
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!
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. 😊
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.
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.
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!)
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.