Skip to content

Instantly share code, notes, and snippets.

@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(
@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 / 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: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: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: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 / elementSelectChange.js
Created September 24, 2015 08:28
Poll for changes to element selects in Craft CMS
$(function () {
// Bind a greedy event handler
$('body').on('elementselectchange', '.elementselect', onElementSelectChange);
// Poll to evalute elementselects every 10ms
setInterval(evaluateElementSelects, 10);
function evaluateElementSelects ()
{
@mmikkel
mmikkel / Installing Imagick with PHP 7.md
Last active September 26, 2016 23:13 — forked from sjelfull/Installing Imagick with PHP 7.md
Installing Image Magick for PHP 7 on a Server Pilot managed server
apt-get install pkg-config libmagickwand-dev -y
cd ~/downloads
wget http://pecl.php.net/get/imagick-3.4.0RC2.tgz
tar xvzf imagick-3.4.0RC2.tgz
cd imagick-3.4.0RC2
/opt/sp/php7.0/bin/phpize
./configure
make install
rm -rf /tmp/imagick-3.4.0RC2*
public function getFieldValues(BaseElementModel $entry, $fields = null)
{
foreach ($entry->fieldLayout->fields as $fieldLayoutField) {
$fieldHandle = $fieldLayoutField->field->handle;
if ($fields === null || in_array($fieldHandle, $fields)) {
$field[$fieldHandle] = $entry->getFieldValue($fieldHandle);
}
}
return $field;
@mmikkel
mmikkel / gist:1fa89933dc9dfa3f3037
Created March 20, 2016 19:45
Adding a custom filter to gulp-nunjucks-render
var manageEnvironment = function(environment) {
environment.addFilter('custom', function(str) {
return 'Olav, du må vakne. Indianarane kjem.';
});
}
var htmlTask = function() {
return gulp.src(paths.src)
.pipe(data(getData))