Skip to content

Instantly share code, notes, and snippets.

@mmikkel
mmikkel / gist:450306a143310d935e76
Created August 21, 2015 08:52
Craft: Add entry author's category field to entry listing
public function modifyEntryTableAttributes( &$attributes, $source ) {
// Note: the number "3" is the relevant section's ID
if ($source == 'section:3')
{
// Add a dummy field key – this could be called anything but avoid conflicts
$attributes['myEntryAuthorCategory'] = Craft::t('Author category');
}
}
public function getEntryTableAttributeHtml(EntryModel $entry, $attribute)
@mmikkel
mmikkel / gist:61a8c8b3db728cdfdbbd
Last active August 29, 2015 14:22
Output sub nav from related nav item, from the top level entry
{# Get the related nav item for the current entry #}
{% set navItem = craft.entries.section('navigation').relatedTo(entry).first() %}
{% if navItem %}
{# Get the top level page for the nav item #}
{% set topPage = navItem.getAncestors().level(1).first()|default(navItem) %}
{# Build a "pages" array w/ the top page and its descendents (child entries) #}
{% set pages = craft.entries.id([topPage.id]|merge(topPage.getDescendants().ids()|default([]))) %}
<nav>
<ul>
{% nav page in pages %}
@mmikkel
mmikkel / gist:0937f634f788e78de5f5
Created April 20, 2015 14:06
TinyMCE button for custom shortcode
<?php
// [myCustomShortcode type="tip"]Dette er et godt tips[/myCustomShortcode]
function clientName_myCustomShortcode_func( $atts, $content = null ) {
$args = shortcode_atts( array(
'type' => 'tip',
), $atts );
if ( ! $content ) {
return '';
}
@mmikkel
mmikkel / gist:c6a89a975c0e48f9d560
Created March 3, 2015 00:05
Find entries w/ category relation in Matrix block (Craft CMS)
{# Fetch the category "myCategory" (and any child categories as well) #}
{% set categories = craft.categories.slug( 'myCategory' ) %}
{# Search for entries in section "test", that has the category "myCategory" (or any of its child categories) selected in the field "category" of the Matrix block "testMatrix" #}
{% set entries = craft.entries.section( 'test' ).relatedTo({
targetElement: categories,
field: 'testMatrix.category'
}) %}
{# Output the entries #}
@mmikkel
mmikkel / gist:292f692746c2dcedcc16
Created February 19, 2015 12:16
composer.json for themes running Timber (working as of February 19th 2015)
{
"require": {
"composer/installers": "~1",
"jarednova/timber": "dev-master"
},
"repositories": [
{
"type" : "package",
"package" : {
"name" : "jarednova/php-router",
@mmikkel
mmikkel / class.MyPost.php
Created November 11, 2014 10:50
TimberPost extension w/ post slug support
<?php
class MyPost extends TimberPost {
public function __construct( $post_id = null, $post_type = 'post' ) {
if ( is_string( $post_id ) ) {
// If $post_id is a string, we'll consider it a slug and try to get the ID
$args = array(