Skip to content

Commit

Permalink
Adding support for custom category fields as per #3 and #25
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Pitman committed Mar 26, 2015
1 parent 02c2c13 commit 03e6663
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ You have access to several variables inside the variable pair:
- `{category_url_title}`
- `{category_description}`
- `{category_image}`
- `{category_custom_field_name}`

You also have access to these parameters:

Expand Down
35 changes: 28 additions & 7 deletions system/third_party/nf_categories_field/ft.nf_categories_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* Categories Field Fieldtype
*
* @package nf_categories_field
* @author Nathan Pitman <[email protected]>
* @copyright Copyright (c) 2014, Nine Four Ltd
* @author Nathan Pitman, <[email protected]>
* @copyright Copyright (c) 2015, NAthan Pitman
*/
class Nf_categories_field_ft extends EE_Fieldtype {

Expand Down Expand Up @@ -731,27 +731,48 @@ function replace_tag_catchall($data, $params = array(), $tagdata = FALSE, $modif
* @return Array Array of data read for the parser containing
* category IDs, names, and url_titles
*/
private function _get_category_data($cat_ids, $limit=NULL, $group_id=NULL)
private function _get_category_data($cat_ids, $limit=NULL, $group_id=NULL, $with_custom_fields=TRUE)
{
// Pull in category data and map it
ee()->load->model('category_model');

// Get the standard field data (including the custom field data)
ee()->db->from('categories c');
ee()->db->join('category_field_data cfd', 'cfd.cat_id=c.cat_id', 'left');
ee()->db->limit($limit);
$category_query = ee()->db->where_in('cat_id', $cat_ids)
->get('categories')
->result_array();
ee()->db->where_in('c.cat_id', $cat_ids);
$category_query = ee()->db->get()->result_array();

// Get the custom field names
ee()->db->from('category_fields cf');
if ($group_id) {
ee()->db->where('cf.group_id', $group_id);
}
$custom_fields_query = ee()->db->get()->result_array();

// Create the array for parsing
$parse = array();
$i = 0;
foreach ($category_query as $category_row)
{
$parse[] = array(

// Standard category fields
$parse[$i] = array(
'category_id' => $category_row['cat_id'],
'category_parent_id' => $category_row['parent_id'],
'category_name' => $category_row['cat_name'],
'category_url_title' => $category_row['cat_url_title'],
'category_description' => $category_row['cat_description'],
'category_image' => $category_row['cat_image']
);

// Add custom fields
foreach($custom_fields_query AS $custom_field) {
$custom_field_name = "field_id_".$custom_field['field_id'];
$parse[$i][$custom_field['field_name']] = $category_row[$custom_field_name];
}

$i++;
}

return $parse;
Expand Down

0 comments on commit 03e6663

Please sign in to comment.