Skip to content

Instantly share code, notes, and snippets.

@croxton
Created March 20, 2012 13:44
Show Gist options
  • Save croxton/2135739 to your computer and use it in GitHub Desktop.
Save croxton/2135739 to your computer and use it in GitHub Desktop.
Assets custom field for Taxonomy
This will add an 'Assets' fieldtype to Taxonomy.
Caveats:
* only images will be visible in the Assets sheets
* only one image can be selected
* shows all file upload locations that the user's member group can access
* stores the actual image url NOT the Assets path eg {filedir_1}my_image.jpg.
Iain - you might want to implement Assets path {filedir} parsing for your Taxonomy module
tags and then store the Assets url instead (which is in files[0].path). Then users can move
the image in Assets and it won't break the link...
mcp.taxonomy.php
1. Add this just below the opening class Taxonomy_mcp {
private static $_assets_init = FALSE;
2. Add this add around line 598, below unset($entries_select_dropdown)...
// Assets field
foreach($vars['tree_settings']['fields'] as $custom_field)
{
if ($custom_field['type'] == 'assets')
{
// Make sure that Assets is installed and include dependencies
if (array_key_exists('assets', $this->EE->addons->get_installed()))
{
if ( ! self::$_assets_init)
{
require_once PATH_THIRD.'assets/helper.php';
$assets_helper = new Assets_helper;
$assets_helper->include_sheet_resources();
self::$_assets_init = true;
}
$assets_js = '
$input = $(\'input[name="custom_fields['.$custom_field["name"].']"]\');
$preview = $input.next("a").next(".preview");
$delete = $preview.find(".delete-preview");
$delete.css("display", "block");
var sheet'.$custom_field["order"].' = new Assets.Sheet({
// optional settings (these are the default values):
multiSelect: false,
filedirs: "all", // or array of filedir IDs
kinds: ["image"], // string "any", or array of file kinds e.g. ["image", "flash"]
// onSelect callback (required):
onSelect: function(files) {
// files is an array of file paths and URLs, e.g.:
// [
// { path: "{filedir_1}filename1.jpg", url: "/images/uploads/filename1.jpg" }
// { path: "{filedir_1}filename2.jpg", url: "/images/uploads/filename2.jpg" }
// ]
$input.attr("value", files[0].url);
$preview.find("img").attr("src", files[0].url);
$preview.css("display", "block");
}
});
$input.next("a").click(function(){
sheet'.$custom_field["order"].'.show();
return false;
});
$delete.click(function(e) {
$preview.css("display", "none");
$input.attr("value", "");
return false;
});
';
$this->EE->cp->add_to_foot('<script type="text/javascript">'.$assets_js.'</script>');
}
}
}
--------------------------------
views/manage_node.php
3. Add this at around line 139 inside the switch/case block...
case($custom_field['type'] == 'assets'):
if ( ! empty($value))
{
$thumb_html = '<div class="preview">';
}
else
{
$thumb_html = '<div class="preview" style="display: none;">';
}
$thumb_html .= '<img src="'.$value.'" alt="" style="width: 160px; border: 5px solid white; margin-top: 10px;">';
$thumb_html .= '<a href="#" class="delete-preview">delete</a>';
$thumb_html .= '</div>';
$this->table->add_row(
$custom_field['label'].':',
form_input('custom_fields['.$custom_field['name'].']', $value, 'id='.$custom_field['name'].', style="width: 60%;"')
.'&nbsp;&nbsp;<a href="#" class="submit">select</a>'
.$thumb_html
);
break;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment