Skip to content

Instantly share code, notes, and snippets.

@croxton
Created March 14, 2012 12:54
Show Gist options
  • Save croxton/2036269 to your computer and use it in GitHub Desktop.
Save croxton/2036269 to your computer and use it in GitHub Desktop.
{exp:taxonomy:nav template_path="{segment_1}/{segment_2}"}
// --------------------------------------------------------------------
/**
* Returns the first node_id for a given template path
* @param string template_path
* @return int node_id,
* but if no rows returned, false
*/
function get_node_id_from_template_path($template_path)
{
$template_path= explode('/', trim($template_path, '/'));
// note: if there is no second segment, default to 'index'
list($template_group, $template) = $template_path + Array( 1 => 'index' );
// Is the first segment the name of a template group?
$this->EE->db->select('group_id');
$this->EE->db->where('group_name', $template_group);
$this->EE->db->where('site_id', $this->EE->config->item('site_id'));
$query = $this->EE->db->get('template_groups');
// Template group found!
if ($query->num_rows() == 1)
{
// Set the group_id so we can use it in the next query
$group_id = $query->row('group_id');
// Is the second segment the name of a valid template?
$this->EE->db->select('template_id');
$this->EE->db->where('group_id', $group_id);
$this->EE->db->where('template_name', $template);
$this->EE->db->where('site_id', $this->EE->config->item('site_id'));
$query = $this->EE->db->get('templates');
// We have a template id!
if ($query->num_rows() == 1)
{
$template_id = $query->row('template_id');
// now we can grab the Taxonomy node
$this->EE->db->select('node_id');
$this->EE->db->order_by($this->left_col, 'asc');
$this->EE->db->limit(1); // we only want the first node found - in case multiple nodes point to same template path
$this->EE->db->where('template_path', $template_id);
$node = $this->EE->db->get($this->tree_table)->row_array();
if($node)
{
return $node['node_id'];
}
else
{
return false;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment