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 27, 2015
1 parent 03e6663 commit a08b878
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 69 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,19 @@ When displaying the field on the front end, you can use the following single var

This will either display a single category ID or a piped list if more than one category has been assigned (e.g. `1|3|17`).

If you have enabled the 'Primary Category Assignment' option then you can access the various attributes of the primary category and the ID of it's parent (or itself if it has no parent) by appending the 'primary_category' modifier to the single field_name variable tag:
If you have enabled the 'Primary Category Assignment' option then you can access the various attributes of the primary category and the ID of it's parent (or itself if it has no parent) by appending the 'primary' modifier to the single field_name variable tag:

- `{field_name:primary_category:id}`
- `{field_name:primary_category:parent_id}`
- `{field_name:primary_category:name}`
- `{field_name:primary_category:url_title}`
- `{field_name:primary_category:description}`
- `{field_name:primary_category:image}`
- `{field_name:primary:category_id}`
- `{field_name:primary:category_parent_id}`
- `{field_name:primary:category_name}`
- `{field_name:primary:category_url_title}`
- `{field_name:primary:category_description}`
- `{field_name:primary:category_image}`
- `{field_name:primary:category_custom_field_name}`

You can also return the primary category ID using the modifier with no attribute specified:

- `{field_name:primary_category}`
- `{field_name:primary}`

### Variable Pair

Expand Down
99 changes: 38 additions & 61 deletions system/third_party/nf_categories_field/ft.nf_categories_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,15 +628,8 @@ function replace_tag_catchall($data, $params = array(), $tagdata = FALSE, $modif

switch($parts[0]) {

// If part 1 is {field_name:primary_category...
case 'primary_category':

$primary_category_id = FALSE;
$primary_category_parent_id = FALSE;
$primary_category_name = FALSE;
$primary_category_url_title = FALSE;
$primary_category_description = FALSE;
$primary_category_image = FALSE;
// If part 1 is {field_name:primary...
case 'primary':

// array_filter removes empty nodes, array_values re-indexes
$categories = array_values(array_filter(explode($settings['delimiter'], $data)));
Expand All @@ -645,7 +638,7 @@ function replace_tag_catchall($data, $params = array(), $tagdata = FALSE, $modif

$primary_category_id = ltrim($categories[0],'p');

// If there is an attribute request {field_name:primary_category:something}
// If there is an attribute request {field_name:primary:something}
if (isset($parts[1])) {

// Get extra category data
Expand All @@ -655,70 +648,54 @@ function replace_tag_catchall($data, $params = array(), $tagdata = FALSE, $modif
return FALSE;
} else {

switch ($parts[1]) {

// {field_name:primary_category:id}
case 'id':
return $primary_category_id;
break;

// {field_name:primary_category:name}
case 'name':
$primary_category_name = $primary_category[0]['category_name'];
return $primary_category_name;
break;

// {field_name:primary_category:url_title}
case 'url_title':
$primary_category_url_title = $primary_category[0]['category_url_title'];
return $primary_category_url_title;
break;

// {field_name:primary_category:description}
case 'description':
$primary_category_description = $primary_category[0]['category_description'];
return $primary_category_description;
break;

// {field_name:primary_category:image}
case 'image':
$primary_category_image = $primary_category[0]['category_image'];
return $primary_category_image;
break;

// {field_name:primary_category:parent_id}
case 'parent_id':
// If there's a parent return it's ID, else return this categories ID
if ($primary_category[0]['category_parent_id']==0) {
$primary_category_parent_id = $primary_category[0]['category_id'];
} else {
$primary_category_parent_id = $primary_category[0]['category_parent_id'];
if (array_key_exists($parts[1], $primary_category[0])) {

switch ($parts[1]) {

// {field_name:primary:category_id}
case 'id':
return $primary_category_id;
break;

// {field_name:primary:category_parent_id}
case 'parent_id':
// If there's a parent return it's ID, else return this categories ID
if ($primary_category[0]['category_parent_id']==0) {
$primary_category_parent_id = $primary_category[0]['category_id'];
} else {
$primary_category_parent_id = $primary_category[0]['category_parent_id'];
}
return $primary_category_parent_id;
break;

// {field_name:primary:category_ ... }
default:
// This includes custom category fields
return $primary_category[0][$parts[1]];

}
return $primary_category_parent_id;
break;

}
} else {
return FALSE;
}

}

// Just return the default without any attribute preference
} else {

// {if {field_name:primary_category}} ...
return TRUE;
// {if {field_name:primary}} ...
if ($categories AND substr( $categories[0], 0, 1 ) === "p") {
return ltrim($categories[0],'p');
} else {
return FALSE;
}

}

} else {
return FALSE;
}

break;

default:
return FALSE;
break;

break;
}

}
Expand Down

0 comments on commit a08b878

Please sign in to comment.