Skip to content

Instantly share code, notes, and snippets.

@piotrpog
piotrpog / attributes_table.twig
Created June 25, 2020 16:49
Attributes table automatically generated from fields grouped within matrix field. More info: http://craftsnippets.com/articles/creating-attributes-table-from-entry-fields-in-craft-cms
{% apply spaceless %}
{# lightswitch field values #}
{% if lighswitchOn is not defined %}
{% set lighswitchOn = 'yes' %}
{% endif %}
{% if lighswitchOff is not defined %}
{% set lighswitchOff = 'no' %}
{% endif %}
{%- macro transformOrPlaceholder(image, settings, attributes = {}) -%}
{% if image is not null %}
{% set src = image.getUrl(settings) %}
{% set attributes = attributes|merge({src: src}) %}
{{tag('img', attributes)}}
{% elseif settings.width is defined or settings.height is defined %}
{% set width = settings.width ?? settings.height %}
{% set height = settings.height ?? settings.width %}
{% set src = 'https://placehold.co/'~width~'x'~height %}
{% set attributes = attributes|merge({src: src}) %}
@piotrpog
piotrpog / gist:9fcc6b6bb5b838d64ed5075003d2f820
Created December 10, 2019 22:23
Example mofule using incognito field - "favourite category". More info: http://craftsnippets.com/articles/using-incognito-field-plugin-for-craft-cms
// place this code in module variable file
public $userFieldHandle = 'visitedarticles';
public $categoryFieldHandle = 'articleCategory';
public function saveArticleVisit($article){
$user = Craft::$app->getUser()->getIdentity();
if(!is_null($user)){
@piotrpog
piotrpog / gist:6da736ead280bd9989173cad05e6ebef
Created November 21, 2019 19:51
Craft CMS log only warning and errors
'components' => [
'log' => function() {
$config = craft\helpers\App::logConfig();
if ($config) {
$config['targets'][0]['includeUserIp'] = false;
$config['targets'][0]['logFile'] = '@storage/logs/web.log';
$config['targets'][0]['enableRotation'] = true;
$config['targets'][0]['maxFileSize'] = 10240;
$config['targets'][0]['maxLogFiles'] = 5;
$config['targets'][0]['levels'] = ['error', 'warning'];
@piotrpog
piotrpog / footer_creator.twig
Last active November 18, 2019 17:48
Email footer creator made with Craft CMS.
@piotrpog
piotrpog / gist:e63877567d82d423d34951e37b29a6ad
Last active November 8, 2019 17:34
Send email in Craft module
use craft\mail\Message;
$settings = Craft::$app->systemSettings->getSettings('email');
$message = new Message();
$message->setFrom([$settings['fromEmail'] => $settings['fromName']]);
$message->setTo('xx@x.x');
$message->setHtmlBody('test body');
$message->setSubject('test subject');
Craft::$app->mailer->send($message);
@piotrpog
piotrpog / js_var.twig
Created October 13, 2019 23:19
Twig macro for passing Twig variables into Javascript. More info: http://craftsnippets.com/articles/using-javascript-in-twig-templates-with-craft-cms
{% macro jsVar(variable, jsVariableName) %}
{# v1 #}
var {{jsVariableName}} = {{variable|json_encode|raw}};
{% endmacro %}
{# v1 #}
{# settings #}
{% set faviconFileName = '/static/images/favicon.jpg' %}
{% set sizesIcon = [192, 48, 32, 16] %}
{% set sizesAppleTouch = [180] %}
{# output #}
{% if craft.app.plugins.isPluginEnabled('imager') and craft.imager.transformImage(faviconFileName, {}, null, {suppressExceptions : true}) %}
{% for faviconSize in sizesIcon %}
{% set faviconTransform = {
width: faviconSize,
@piotrpog
piotrpog / favicons.twig
Last active September 30, 2019 00:06
Twig component generating favicons from files uploaded trough control panel. More info: http://craftsnippets.com/articles/adding-favicons-to-craft-cms-website
{# requires global with handle 'favicon' containing asset field with handle 'faviconFile' #}
{# v2 #}
{% cache globally %}
{% set sizesIcon = [192, 48, 32, 16] %}
{% set sizesAppleTouch = [180] %}
{% if favicon is defined and favicon['faviconFile'] is defined and favicon.faviconFile.exists() and favicon.faviconFile.one().kind == 'image' %}
{# link icon #}
{% for faviconSize in sizesIcon %}
{% set icon = favicon.faviconFile.one() %}
{% set shorterEdge = icon.width > icon.height ? icon.height : icon.width %}
{% if craft.app.urlManager.matchedElement and craft.app.urlManager.matchedElement.uri == '__home__' %}
{% set seoTitle = craft.app.urlManager.matchedElement.title %}
{% elseif craft.app.urlManager.matchedElement %}
{% set seoTitle = craft.app.urlManager.matchedElement.title ~ ' - ' ~ siteName %}
{% else %}
{% set seoTitle = siteName %}
{% endif %}
<title>{{ seoTitle }}</title>