Skip to content

Instantly share code, notes, and snippets.

@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);
{% if article is defined %}
<li>
{{article.id}} - {{article.title}}
</li>
{% endif %}
@piotrpog
piotrpog / footer_creator.twig
Last active November 18, 2019 17:48
Email footer creator made with Craft CMS.
@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 / 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)){
<form class="js-contact-form">
<fieldset class="js-contact-form-fieldset">
{{ csrfInput() }}
<div class="field">
{{tag('label', {
for: 'form-email',
class: 'label',
text: 'Your Email'|t('contact-form'),
}) }}
{%- 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 / svg_map.twig
Last active August 5, 2020 03:21
SVG map-based navigation for Craft CMS. More info: http://localhost/_pog/craft-snippets-craft/web/articles/creating-map-based-navigation-in-craft-cms
{# settings #}
{% set map = include('usa.svg') %}
{% set mapLinks = entry.mapLinks.all() %}
{# logic #}
{% set map = map|retconRemove('style') %}
{% set regionsUsed = [] %}
{% for link in mapLinks %}
{% if link.linkRegion is not empty and link.linkEntry.exists() and link.linkRegion not in regionsUsed %}
{% set regionsUsed = regionsUsed|merge([link.linkRegion]) %}
@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 %}
@piotrpog
piotrpog / link_list_macro.twig
Created June 29, 2020 08:04
Twig macro generating links from array of entries