Skip to content

Instantly share code, notes, and snippets.

View mdcpepper's full-sized avatar

Mike Pepper mdcpepper

View GitHub Profile

Keybase proof

I hereby claim:

  • I am mdcpepper on github.
  • I am mdcpepper (https://keybase.io/mdcpepper) on keybase.
  • I have a public key ASB9BczfIfBT3yuX8mlOSoLWSBw_8wdWpEnEKtHfg5b5NAo

To claim this, I am signing this object:

<?php
namespace modules;
use Craft;
use craft\elements\Asset;
use yii\base\Event;
/**
* Custom module class.
*
@mdcpepper
mdcpepper / YourPlugin.php
Created February 16, 2018 10:26
Move Entries to the top of a structure when they're first saved
class YourPlugin extends BasePlugin
{
public function init()
{
craft()->on('entries.onSaveEntry', function(Event $event)
{
$isNewEntry = $event->params['isNewEntry'];
if (!$isNewEntry)
{
{% extends "_layout" %}
{# Get all the categories to display #}
{% set allCategoriesById = entry.linkCategory.indexBy('id') %}
{# Get all the pages related to those categories #}
{% set allCategoryPages = craft.entries.section('pages').relatedTo(allCategoriesById).with(['pagesCategory']) %}
{# Group the pages by the categories ID's #}
{% set pagesByCategoryId = allCategoryPages|group('pagesCategory[0].id') %}
<?php
/**
* Get the content of a field for use in BaseElementModel::setContentFromPost()
*
* @param BaseElementModel $sourceElement The element in the source locale
* @param BaseElementModel $targetElement The element in the target locale
* @param FieldModel $field The field to migrate
*/
private function _getMigratedFieldContent(BaseElementModel $sourceElement, BaseElementModel $targetElement = null, FieldModel $field, $force = false)
<?php
namespace Craft;
class UserCountsPlugin extends BasePlugin
{
public function getName()
{
return Craft::t('User Counts');
}
@mdcpepper
mdcpepper / _cart-debug.twig
Created October 21, 2016 14:25
Craft Commerce debugging template
{% if craft.config.get('devMode') %}
{% set size = 30 %}
<pre style="width:80%;margin:0 auto">
Line Items:
---------------------------------------------------
{% for lineItem in cart.lineItems %}
{{ lineItem.purchasable.title }} x {{ lineItem.qty }} {{ dump(lineItem.options) }}
{% endfor %}
@mdcpepper
mdcpepper / UrlFormatsVariable.php
Created July 8, 2016 10:39
Using template variables to determine a URL Format
<?php
namespace Craft;
class UrlFormatsVariable
{
// Use in the URL Format:
//
// {{ craft.urlFormats.treatments(object) }}
public function treatments($entry)
{
<?php
namespace Craft;
class EntryTypeSourcesPlugin extends BasePlugin
{
public function getName()
{
return Craft::t('Entry Type Sources');
}