Skip to content

Commit

Permalink
Initial commit for 1.1.0
Browse files Browse the repository at this point in the history
Improved primary_category single variables,
{field_name:primary_category_id} is replaced with
{field_name:primary_category:id} and support added for all category
fields; id, name, url_title, description, image and parent_id.
  • Loading branch information
Nathan Pitman committed Sep 9, 2014
1 parent f4f512d commit 2bedca3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 124 deletions.
27 changes: 18 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Install
2. Move third\_party/nf\_categories\_field to expressionengine/third\_party
3. Move themes/third\_party/nf\_categories\_field to expressionengine/themes
4. Install the field type from the ExpressionEngine control panel
5. Create a new custom field and select the 'Categories' field type

Field Settings
--------------
Expand Down Expand Up @@ -47,22 +48,28 @@ Obviously if you set your field to 'Sync with Native Categories' then you can us

### Single Variables

When displaying the field on the front end, you can use the following single variables:
When displaying the field on the front end, you can use the following single variable:

- `{field_name}`

This will either display a single category ID or a piped list (e.g. `1|3|17`).
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`).

- `{field_name:primary_category_id}`
- `{field_name:primary_category_name}`
- `{field_name:primary_category_url_title}`
- `{field_name:primary_category_parent_id}`
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 ID and/or name or URL title of the primary category and the ID of it's parent (or itself if it has no parent) using these tags.
- `{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}`

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

- `{field_name:primary_category}`

### Variable Pair

You can also use a variable pair:
You can also use a field_name variable pair:

<pre>{field_name}
<option val="{category_id}">{category_name} ({category_url_title})</option>
Expand All @@ -84,7 +91,9 @@ You also have access to one parameter:
Change Log
----------

See Releases - https://github.com/ninefour/categories_field.ft.ee_addon/releases
For release notes and instructions on upgrading from one version to another please check the releases page:

https://github.com/ninefour/categories_field.ft.ee_addon/releases

Thanks
------
Expand Down
2 changes: 1 addition & 1 deletion system/third_party/nf_categories_field/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
if ( ! defined('NF_CF_NAME'))
{
define('NF_CF_NAME', 'Categories Field');
define('NF_CF_VERSION', '1.0.9');
define('NF_CF_VERSION', '1.1.0');
define('NF_CF_DOCS', 'http://github.com/ninefour/categories_field.ft.ee_addon');
}

Expand Down
193 changes: 79 additions & 114 deletions system/third_party/nf_categories_field/ft.nf_categories_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Get config file
require(PATH_THIRD.'nf_categories_field/config.php');


/**
* Categories Field Fieldtype
*
Expand Down Expand Up @@ -326,11 +325,7 @@ private function _display_field($data, $cell = FALSE)
$selected_primary = NULL;
$selected_primary_label = "";

/*echo('<pre>');
print_r($this->settings);
exit;*/

// If validation on the publish form fires we get an array
// If validation on the publish form fires we get an array back
if (is_array($data)) {

$selected = in_array($row[0], $data) ? 1 : 0;
Expand Down Expand Up @@ -364,7 +359,6 @@ private function _display_field($data, $cell = FALSE)
// Primary Category?
if (isset($this->settings['primary_cat']) AND $this->settings['primary_cat']) {
$selected_primary = ($row[0] == $selected_primary) ? 1 : 0;
//echo($selected_primary);
if ($selected_primary) {
$selected_primary_label = '<span class="label">Primary Category</span>';
}
Expand Down Expand Up @@ -558,100 +552,106 @@ function replace_tag($data, $params = array(), $tagdata = FALSE) {
return $parsed;
}

// {field_name:primary_category_id}
function replace_primary_category_id($data, $params = array(), $tagdata = FALSE)
// {field_name:modifier} AND/OR {field_name:modifier:attribute}
function replace_tag_catchall($data, $params = array(), $tagdata = FALSE, $modifier)
{

// Establish Settings
$settings = (isset($this->settings['nf_categories_field'])) ? $this->settings['nf_categories_field'] : $this->settings;
$settings = $this->_default_settings($settings);

$primary_cat_id = FALSE;
// array_filter removes empty nodes, array_values re-indexes
$categories = array_values(array_filter(explode($settings['delimiter'], $data)));
// Get modifier parts
$parts = explode(":", $modifier);

if ($categories AND substr( $categories[0], 0, 1 ) === "p") {
$primary_cat_id = ltrim($categories[0],'p');
}

return $primary_cat_id;
}

// {field_name:primary_category_name}
function replace_primary_category_name($data, $params = array(), $tagdata = FALSE)
{
// If part 1 is {field_name:primary_category...
if ($parts[0]=="primary_category") {

// Establish Settings
$settings = (isset($this->settings['nf_categories_field'])) ? $this->settings['nf_categories_field'] : $this->settings;
$settings = $this->_default_settings($settings);

$primary_cat_name = FALSE;
// array_filter removes empty nodes, array_values re-indexes
$categories = array_values(array_filter(explode($settings['delimiter'], $data)));

if (substr( $categories[0], 0, 1 ) === "p") {
$primary_cat_id = ltrim($categories[0],'p');
$primary_cat = $this->_get_category_data(array($primary_cat_id));
$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 ($primary_cat[0]) {
$primary_cat_name = $primary_cat[0]['category_name'];
}
}
// array_filter removes empty nodes, array_values re-indexes
$categories = array_values(array_filter(explode($settings['delimiter'], $data)));

return $primary_cat_name;
}
if ($categories AND substr( $categories[0], 0, 1 ) === "p") {

// {field_name:primary_category_url_title}
function replace_primary_category_url_title($data, $params = array(), $tagdata = FALSE)
{
$primary_category_id = ltrim($categories[0],'p');

// Establish Settings
$settings = (isset($this->settings['nf_categories_field'])) ? $this->settings['nf_categories_field'] : $this->settings;
$settings = $this->_default_settings($settings);
// If there is an attribute request {field_name:primary_category:something}
if (isset($parts[1])) {

$primary_cat_url_title = FALSE;
// array_filter removes empty nodes, array_values re-indexes
$categories = array_values(array_filter(explode($settings['delimiter'], $data)));
// Get extra category data
$primary_category = $this->_get_category_data(array($primary_category_id));

if (substr( $categories[0], 0, 1 ) === "p") {
$primary_cat_id = ltrim($categories[0],'p');
$primary_cat = $this->_get_category_data(array($primary_cat_id));
if (!is_array($primary_category[0])) {
return FALSE;
} else {

if ($primary_cat[0]) {
$primary_cat_url_title = $primary_cat[0]['category_url_title'];
}
}
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'];
}
return $primary_category_parent_id;
break;

return $primary_cat_url_title;
}
}

// {field_name:primary_category_parent_id}
function replace_primary_category_parent_id($data, $params = array(), $tagdata = FALSE)
{
}

// Establish Settings
$settings = (isset($this->settings['nf_categories_field'])) ? $this->settings['nf_categories_field'] : $this->settings;
$settings = $this->_default_settings($settings);
// Just return the default without any attribute preference
} else {

$primary_cat_name = FALSE;
// array_filter removes empty nodes, array_values re-indexes
$categories = array_values(array_filter(explode($settings['delimiter'], $data)));
if ($categories AND substr( $categories[0], 0, 1 ) === "p") {
$primary_category_id = ltrim($categories[0],'p');
}

if (substr( $categories[0], 0, 1 ) === "p") {
$primary_cat_id = ltrim($categories[0],'p');
$primary_cat = $this->_get_category_data(array($primary_cat_id));
// {field_name:primary_category}
return $primary_category_id;

if ($primary_cat[0]) {
// If there's a parent return it's ID, else return this categories ID
if ($primary_cat[0]['category_parent_id']==0) {
return $primary_cat[0]['category_id'];
} else {
return $primary_cat[0]['category_parent_id'];
}

}

}

return;
}

/**
Expand Down Expand Up @@ -705,57 +705,21 @@ public function display_field($field_data)
return $this->_display_field($field_data);
}

/**
* Displays the field in matrix
*
* @param string
* @return string
*/
public function display_cell($cell_data)
{
return $this->_display_field($cell_data, TRUE);
}

/**
* Displays the field in Low Variables
*
* @param string
* @return string
*/
public function display_var_field($var_data)
{
return $this->_display_field($var_data);
}

/**
* Displays the field in Grid
*
* @param string
* @return string
*/
public function grid_display_field($grid_data)
{
return $this->_display_field($grid_data);
}

// --------------------------------------------------------------------

/**
* Allow in Grid
* Allowed Content Types
*
* @param string
* @return bool
*/
public function accepts_content_type($name)
{
return in_array($name, array(
'channel',
'grid',
'low_variables'
'channel'
));
}


/**
* Include CSS theme to CP header
*
Expand All @@ -775,6 +739,7 @@ public function _include_theme_css($file, &$r = FALSE) {
}
}
}

/**
* Include JS theme to CP header
*
Expand Down

0 comments on commit 2bedca3

Please sign in to comment.