Adding a “Prepend” Option to URLs generated by the {exp:structure:nav} Tag

In mod.structure.php function nav() insert the highlighted line:

[php highlight=”81″ firstline=”76″]

// Miscellaneous parameters
$depth = $this->EE->TMPL->fetch_param(‘limit_depth’, 1);
$exclude = $this->EE->TMPL->fetch_param(‘exclude’, NULL);
$mode = $this->EE->TMPL->fetch_param(‘mode’, ‘sub’);
$prepend = $this->EE->TMPL->fetch_param(‘prepend’, NULL);

[/php]

And before the end of the function, replace the line setting $html with the following highlighted line:

[php highlight=”124″ firstline=”120″]

if ($entry_id === FALSE)
$entry_id = 0;

$html = $this->sql->generate_nav($entry_id, $current_id, $depth, $exclude, $mode, $prepend);

return $html;
}

[/php]

Now open sql.structure.php.

Locate the generate_nav() function (around line 479) and change the function definition to the first highlighted line below. Also update the function call to get_selective_data() (around line 489) to pass through that $prepend value as shown in the second line below.

[php highlight=”479,489″ linestart=”474″]

/**
* Get the HTML code for an unordered list of the tree
* @return string HTML code for an unordered list of the whole tree
*/
function generate_nav($entry_id, $current_id = FALSE, $depth = 1, $exclude = NULL, $mode, $prepend=null)
{

$html = ”;
$separator = $this->EE->config->item(‘word_separator’) != "dash" ? ‘_’ : ‘-‘;

// Fallback to entry_id if no current_id (e.g. sitemap usage)
if ($current_id === FALSE)
$current_id = $entry_id;

$pages = $this->get_selective_data($entry_id, $depth, $exclude, $current_id, $mode, $prepend);
$pages = $this->add_attributes($pages, $current_id, $mode);

[/php]

Now we update get_selective_data() (around line 139) to make use of the passed data:

[php linestart=”130″ highlight=”139″]
/**
* Get selective data on all Structure Channels
* The Godfather of all Structure queries
*
* "I’m gonna make you a query you can’t refuse."
*
* @return array
*/
function get_selective_data($parent_id = NULL, $depth = 1, $exclude = NULL, $current_id = NULL, $mode = NULL, $prepend = NULL)
{
$pages = $this->get_site_pages();

[/php]

Please take a moment and go back to thoroughly review the inline comment.

Finally, in the same get_selective_data() function find the code which sets “$data[$row[‘entry_id’]][‘uri’]” (around line 286) and replace it with the highlighted code:

[php linestart=”281″ highlight=”287″]

// build out the main data array
foreach ($page_data as $row)
{
$data[$row[‘entry_id’]] = $row;
//$data[$row[‘entry_id’]][‘uri’] = $this->EE->functions->create_page_url($pages[‘url’] .’/’. $prepend, $pages[‘uris’][$row[‘entry_id’]]);
$data[$row[‘entry_id’]][‘uri’] = $this->EE->functions->create_page_url($pages[‘url’] .’/’. $prepend, $pages[‘uris’][$row[‘entry_id’]]);
$data[$row[‘entry_id’]][‘slug’] = $pages[‘uris’][$row[‘entry_id’]];
$data[$row[‘entry_id’]][‘classes’] = array();
$data[$row[‘entry_id’]][‘ids’] = array();

[/php]

That does it. You should now be able to add an arbitrary string to the beginning of the path portion of a URL.

Example code:

[php htmlscript=”true” wraplines=”true”]

{exp:structure:nav mode="sub" lang="{site_lang}" start_from="/{segment_1}" }

[/php]

Note: {site_lang} is a snippet I’ve created in the EE back end that always echos the current site “stub”. Also, the “start_from” attribute isn’t required.

You can use the same principles in this post to prepend strings to the Structure breadcrumbs and other parts of Structure. Please be aware, however, if you modify the core code the Structure guys are not going to be able to help you with any problems you might encounter.

Leave a Reply