Skip to content

Instantly share code, notes, and snippets.

@mdcpepper
Last active August 29, 2015 14:01
Show Gist options
  • Save mdcpepper/68c9580f4abc2fc11b8b to your computer and use it in GitHub Desktop.
Save mdcpepper/68c9580f4abc2fc11b8b to your computer and use it in GitHub Desktop.
{# news/_entry.twig #}
{% block breadcrumbs %}
{{ macro.breadcrumbs(entry, { parent: craft.entries.id(45).first() }) }}
{% endblock %}
{%- macro breadcrumbs(entry, options = {}) -%}
{# defaults #}
{%- set showHome = (options.home is defined) ? options.home : true -%}
{%- set showCurrent = (options.current is defined) ? options.current : false -%}
{%- set parent = (options.parent is defined) ? options.parent : null -%}
{%- set threshold = (options.threshold is defined) ? options.threshold : 2 -%}
{%- set classes = (options.class is defined) ? options.class : 'nav breadcrumb' -%}
{# where do we want to start the breadcrumb ancestors from? the current entry or some override #}
{%- set startPoint = (parent) ? parent : entry.parent %}
{# we're not actually outputting the links as we go along, we put them in an array. At the end
we only loop through and output them if there's more than the threshold #}
{%- set links = [] %}
{# add a link to the homepage if we want one #}
{% if showHome %}
{%- set link -%}<li>{{- link_to('entry:2', 'Home') -}}</li>{%- endset -%}
{%- set links = links|merge([link]) -%}
{% endif %}
{% if startPoint %}
{# structures and categories are the only thing that have ancestors #}
{%- if 'structure' == startPoint.section.type or 'Category' == startPoint.elementType -%}
{%- for crumb in startPoint.getAncestors() -%}
{%- set link -%}<li><a href="{{ entry_or_redirected(crumb).url }}">{{ crumb.menuLabel|default(crumb.title) }}</a></li>{%- endset -%}
{%- set links = links|merge([link]) -%}
{%- endfor -%}
{%- endif -%}
{# now the actual entry/start point #}
{%- set link -%}<li><a href="{{ entry_or_redirected(startPoint).url }}">{{ startPoint.menuLabel|default(startPoint.title) }}</a></li>{%- endset -%}
{%- set links = links|merge([link]) -%}
{% endif %}
{# include a link to the current page aswell maybe #}
{% if showCurrent %}
{%- set link -%}<li><a href="{{ entry_or_redirected(entry).url }}">{{ entry.menuLabel|default(entry.title) }}</a></li>{%- endset -%}
{%- set links = links|merge([link]) -%}
{% endif %}
{# sometimes you end up just linking to the homepage which looks silly on its own so make sure we're at the threshold #}
{%- if links|length >= threshold -%}
<ul class="{{ classes }}">
{%- for link in links -%}
{{- link -}}
{%- endfor -%}
</ul>
{%- endif -%}
{% endmacro -%}
<?php
// return the first-child of a redirected-entry if it has one, or just itself
public function entry_or_redirected($entry)
{
if('first-child-redirect' == $entry->pageTemplate)
{
$children = $entry-getChildren();
if(empty($children)) return $entry;
return $children[0];
}
return $entry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment