Skip to content

Instantly share code, notes, and snippets.

View ChezFre's full-sized avatar

Chez Fre ChezFre

View GitHub Profile
@ChezFre
ChezFre / template.php
Created July 8, 2014 11:28
Change language name to language code in Drupal language block
<?php
function Autostock2014_language_switch_links_alter(&$links, $type, $path) {
foreach ($links as &$language) {
$language['title'] = $language['language']->language;
}
}
@ChezFre
ChezFre / node.tpl.php
Last active August 29, 2015 14:03
Print field value in Drupal template
/*****************
** BAD PRACTICE **
*****************/
<img src="<?php print file_create_url($node->field_image['und'][0]['uri']); ?>" />
/*************
** USE THIS **
*************/
@ChezFre
ChezFre / Drupal Views Export
Created July 20, 2014 15:44
Filter entity reference by current logged in user field
$view = new view();
$view->name = 'garages';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Garages';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
@ChezFre
ChezFre / config.js
Created July 21, 2014 18:49
CKeditor add html tags and attributes you don't want removed
// http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-extraAllowedContent
// /admin/config/content/ckeditor/edit/Full
config.allowedContent = true;
config.extraAllowedContent = 'span';
config.removeFormatAttributes = 'align,hspace,valign'; // Defaults to: 'class,style,lang,width,height,align,hspace,valign'
@ChezFre
ChezFre / node.tpl.php
Created August 4, 2014 08:01
Add flag to node template
// https://www.drupal.org/node/295383
<?php print flag_create_link('flag_machine_name', $node->nid); ?>
@ChezFre
ChezFre / steps
Created August 6, 2014 09:48
Show number of entities referencing this node in a view
Via http://drupal.stackexchange.com/questions/42352/display-number-of-times-content-is-referenced-in-content
This video series helped me an infinite amount: http://vimeo.com/33251051, http://vimeo.com/33252553.
Make sure Child has an entity reference to Parent.
Create a new view
In Filter Criteria, add a new Type (= Parent)
In Advanced>Other, set Aggregation to YES
In Advanced>Relationships, add a new relationship for "A bridge to the Content entity that is referencing Content via field_parent" where field_parent is your entity reference. Call this "Parents"
In Fields, add Content: Title and when prompted set its Aggregation type to Group results together
@ChezFre
ChezFre / template.php
Created September 2, 2014 11:20
Replace node id with other field from referenced node
function Berghoff2014_form_alter(&$form, &$form_state, $form_id) {
if( $form_id === "webform_client_form_33" ) {
/**
** Replace NID from Agent with actual email **
**/
//$node_id = $form['submitted']['agent']['#value'];
$node_id = arg(1);
@ChezFre
ChezFre / template.php
Created October 2, 2014 07:43
Unset all Drupal css and javascript files in your theme
function THEME_NAME_css_alter(&$css)
{
unset($css[drupal_get_path('module', 'system').'/system.theme.css']);
unset($css[drupal_get_path('module','system').'/system.base.css']);
unset($css[drupal_get_path('module', 'system').'/system.messages.css']);
unset($css[drupal_get_path('module', 'comment').'/comment.css']);
unset($css[drupal_get_path('module', 'field').'/theme/field.css']);
unset($css[drupal_get_path('module', 'mollom').'/mollom.css']);
unset($css[drupal_get_path('module', 'node').'/node.css']);
unset($css[drupal_get_path('module', 'search').'/search.css']);
@ChezFre
ChezFre / node.tpl.php
Last active August 29, 2015 14:07
Add attributes to rendered element in Drupal Template
<?php
$content['field_name'][0]['#item']['attributes']['attribute_name'] = 'value';
?>
@ChezFre
ChezFre / template.php
Created November 26, 2014 08:27
Prefill a webform based on a given id or value in the URL
<?php
function hook_form_alter(&$form, &$form_state, $form_id) {
if( $form_id === "webform_client_form_nn" ) { // Update with actual form id
$node_id = $form['submitted']['wagen']['#value']; // Define a field in webform that takes a get value as default
$node = node_load( $node_id );
// If a node has been specificied via its nid (cid in url), we prefill the request field with the name and url of the car