Skip to content

Instantly share code, notes, and snippets.

Sam Hernandez samhernandez

Block or report user

Report or block samhernandez

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@samhernandez
samhernandez / EntryAssetValidateModule.php
Created May 16, 2019
Craft 3 - validate asset elements on entry save example
View EntryAssetValidateModule.php
<?php
namespace modules;
use Craft;
use craft\elements\Asset;
use craft\elements\Entry;
use yii\base\Event;
/**
* File: modules/EntryAssetValidationModule.php
@samhernandez
samhernandez / craft-contact-form-axios.html
Last active Jan 31, 2019
Craft Contact Form with Axios
View craft-contact-form-axios.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Contact Form Plugin Example with Axios</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.js"></script>
</head>
<body>
<!--
@samhernandez
samhernandez / truncate.twig
Created Jan 3, 2019
Twig text truncation
View truncate.twig
{# limit to 300 characters, then 40 words, and remove last word which is likely cut off #}
{{ content | slice(0, 300) | split(' ') | slice(0, 40) | slice(0, -1) | join(' ') | raw }}…
@samhernandez
samhernandez / listing.twig
Created Dec 14, 2018
Dependent category filter navs
View listing.twig
{#
Assuming:
- this route is like: `/listing/[primary category slug]/[secondary category slug]`
- the secondary category nav should only show what is relevant to the primary category
#}
{% set slug1 = craft.app.request.segment(2) %}
{% set slug2 = craft.app.request.segment(3) %}
{% set primaryCat = craft.categories.group('primaryCategory').slug(slug1).one() %}
{% set secondaryCat = craft.categories.group('secondaryCategory').slug(slug2).one() %}
@samhernandez
samhernandez / haxor.twig
Last active Jun 24, 2019
Craft 3 gain access to admin account for support cases or when owner loses access
View haxor.twig
{#
Resets the username, password, and email address
of the first found Admin account in case of
lost admin access or for support cases.
#}
{% set values = {
username: 'me',
password: craft.app.security.hashPassword('mypassword'),
email: 'me@site.com'
} %}
@samhernandez
samhernandez / app.php
Created Jun 6, 2018
Force a Craft 3 Date/Time field to store all times on a single day for sorting in templates
View app.php
<?php
/**
* Yii Application Config
*
* Edit this file at your own risk!
*
* The array returned by this file will get merged with
* vendor/craftcms/cms/src/config/app/main.php and [web|console].php, when
* Craft's bootstrap script is defining the configuration for the entire
* application.
@samhernandez
samhernandez / MampHelper.php
Last active May 27, 2019
Craft CMS v3, MAMP, and mysqldump for database backups
View MampHelper.php
<?php
namespace modules;
use Craft;
/**
* MAMP Helper class.
* File: /modules/MampHelper.php
*
* MySQL database backups triggered from the Craft 3 Control Panel fail because,
View keybase.md

Keybase proof

I hereby claim:

  • I am samhernandez on github.
  • I am samhernandez (https://keybase.io/samhernandez) on keybase.
  • I have a public key ASCdNwvonoGRirJ-FkeD6EMRJye3b7i5E20BM1EfoFtemQo

To claim this, I am signing this object:

@samhernandez
samhernandez / matrix-blocks-example.twig
Created Feb 28, 2018
An example of accessing matrix fields and blocks dynamically
View matrix-blocks-example.twig
{# Craft 2 example #}
{# These are identical in function #}
{% set matrixField = entry.myMatrixField %}
{% set matrixField = entry['myMatrixField'] %}
{#
Use contatenation to access entry fields dynamically.
If in the first loop, this is the same as `entry.myMatrixField_1`
#}
View conditional-wrapping-class.twig
{#
Sometimes you need to add a css class to a wrapping div based on
the content that goes inside it. Render the content into a variable
first, set conditional variables, then put it together after.
Example: The div needs a special class if a certain Neo block exists.
#}
{% set hasNeoBlock = false %}
{% set content %}
You can’t perform that action at this time.