Skip to content

Instantly share code, notes, and snippets.

View samhernandez's full-sized avatar
🎈

Sam Hernandez samhernandez

🎈
  • Precocity LLC
  • Plano TX
  • X @sam_h
View GitHub Profile
@samhernandez
samhernandez / drupal-multilingual-files.conf
Created January 28, 2015 20:45
Redirect to files that exist without the first language segment in the url
# For Drupal multilingual sites
#
# When the first segment of the url path is a language like 'en', 'fr', 'es'
# paths to files break because Drupal adds them to the base url.
# This turns `http://site.com/es/style.css` into `http://site.com/style.css`
# if the latter is actually a file.
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^/?\w{2}/(.+)$ %{DOCUMENT_ROOT}/$1 [L]
@samhernandez
samhernandez / gist:5717071
Created June 5, 2013 20:34
Quick color hex value in JavaScript. Taken from the comments at http://www.paulirish.com/2009/random-hex-color-code-snippets/
var randomBgHex = '#' + ('000000' + Math.floor(Math.random()*16777215).toString(16)).slice(-6);
@samhernandez
samhernandez / drupal_swap_form_error_message.php
Last active December 20, 2015 09:29
Swap Drupal form error messages
<?php
// Drupal 7.22
// If there's an easier way please let me know
/**
* Swap out a form error message.
*
* This must be done with the actual message comparison since error keys
* are not stored in drupal messages.
*
@samhernandez
samhernandez / drupal-users-by-role-id.php
Last active December 20, 2015 13:59
Get an array of users for a given role id.
/**
* Get an array of users for a given role id.
* @param int $rid - The role id
* @return array
*/
function users_by_role_id($rid)
{
$users = array();
$uids = array();
$query = db_query("SELECT uid FROM users_roles WHERE rid = :role_id", array(':role_id' => $rid));
@samhernandez
samhernandez / drupal-render-entity-field.php
Created August 2, 2013 21:37
Sometimes you want to render a node or user field in a template. Argue about modifying stuff in hook_preprocess() all you want. Don't see the difference between using this and render() in a template.
/**
* Returns the simple, cleaned, translated value for a node or user field
* without extra Drupal markup.
*
* Examples:
* <?php print render_entity_field('user', $user, 'field_first_name'); ?>
* <?php print render_entity_field('node', $node, 'field_my_custom_field'); ?>
*
* @see http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way
* @param string $type 'node', 'user'
@samhernandez
samhernandez / drupal-render-entity-field.php
Created August 2, 2013 21:37
Sometimes you want to render a node or user field in a template. Argue about modifying stuff in hook_preprocess() all you want. Don't see the difference between using this and render() in a template.
/**
* Returns the simple, cleaned, translated value for a node or user field
* without extra Drupal markup.
*
* Examples:
* <?php print render_entity_field('user', $user, 'field_first_name'); ?>
* <?php print render_entity_field('node', $node, 'field_my_custom_field'); ?>
*
* @see http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way
* @param string $type 'node', 'user'
@samhernandez
samhernandez / drupal-render-entity-field.php
Created August 2, 2013 21:37
Sometimes you want to render a node or user field in a template. Argue about modifying stuff in hook_preprocess() all you want. Don't see the difference between using this and render() in a template.
/**
* Returns the simple, cleaned, translated value for a node or user field
* without extra Drupal markup.
*
* Examples:
* <?php print render_entity_field('user', $user, 'field_first_name'); ?>
* <?php print render_entity_field('node', $node, 'field_my_custom_field'); ?>
*
* @see http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way
* @param string $type 'node', 'user'
@samhernandez
samhernandez / iterate.twig
Last active April 7, 2017 19:59
A macro to iterate through Neo and Matrix blocks
{#
Don't call this method directly from templates. Rather, call the `neo` or `matrix`
methods to keep things future-proof.
Block templates may be named (from most generic to most specific)
{ block type }_{ entry field handle }_{ entry section handle}_{ entry type}.twig
{ block type }_{ entry field handle }_{ entry section handle}.twig
{ block type }_{ entry field handle }.twig
{ block type }.twig
@samhernandez
samhernandez / docker-container-name.sh
Last active April 10, 2017 19:32
Get the container name from a compose file
# Suppose there is service defined in the docker-compose.yml file
# named `mysql` but it does not have `container_name` defined so
# we don't know what the container name is right now
CONTAINER_NAME=$(docker-compose -f docker-compose.yml ps | grep mysql | awk '{ print $1 }')
echo "Restarting mysql container"
docker stop $CONTAINER_NAME
docker start $CONTAINER_NAME
@samhernandez
samhernandez / edit-this.twig
Created September 3, 2014 16:38
Craft CMS edit entry link
{% if entry is defined and currentUser and currentUser.can('editEntries:' ~ entry.id) %}
<a href="{{ entry.cpEditUrl }}" class="edit-this section-{{ entry.sectionId }}">Edit this</a>
{% endif %}