Skip to content

Instantly share code, notes, and snippets.

View kristianb21's full-sized avatar

Kristian kristianb21

View GitHub Profile
@kristianb21
kristianb21 / php-zip-files.txt
Created June 13, 2014 12:57
PHP: Zip files
$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
@kristianb21
kristianb21 / d7-php-views-fields.txt
Created June 20, 2014 19:15
D7 - views - PHP: printing views row
//used for views-view-fields.tpl.php
// Prepare views row, field by field
foreach ($fields as $id => $field):
switch ($id) {
case 'title':
$title = $field->content;
break;
case 'field_id':
$field_name = $field->content;
break;
$theme_path = drupal_get_path('theme',$GLOBALS['theme']);
@kristianb21
kristianb21 / d7-php-drupal-get-query.txt
Created July 2, 2014 14:17
D7-PHP-drupal-get-query
//returns an array of url query parameters
$query = drupal_get_query_parameters();
dpm($query);
@kristianb21
kristianb21 / D7-PHP-Link-Function.php
Last active August 29, 2015 14:03
D7-PHP-link-function
@kristianb21
kristianb21 / D7-PHP-user-has-role.txt
Created July 2, 2014 15:22
D7-PHP-user-has-role
//checks if user has role
global $user;
$user_check = array_intersect(array('moderator', 'administrator'), array_values($user->roles));
if (empty($check) ? FALSE : TRUE) {
return $output;
} else {
// $user is not admin
}
@kristianb21
kristianb21 / D7-PHP-block-load.txt
Last active August 29, 2015 14:03
D7-PHP-block-load
$output = '';
//load blocks then render the returned objects
$myblock = block_load('module', 'block_delta');
$block = _block_render_blocks(array($myblock)));
$block = _block_get_renderable_array($block);
$output = drupal_render($block);
print $output;
@kristianb21
kristianb21 / d7-php-field-rendering.php
Created August 1, 2014 20:12
D7-PHP-field rendering
Output field as is in drupal ui:
$output = field_view_field('node', $node, 'field_name');
Just want a single value, and none of the field markup:
$node = node_load($nid);
$field = field_get_items('node', $node, 'field_name');
$output = field_view_value('node', $node, 'field_name', $field[$delta]);
@kristianb21
kristianb21 / gist:e307ada9c5cf2978502a
Created August 14, 2014 14:41
d7-drush-disable model
<?php
/**
* @file
* Drupal site-specific configuration file.
*
* IMPORTANT NOTE:
* This file may have been set to read-only by the Drupal installation program.
* If you make changes to this file, be sure to protect it again after making
* your modifications. Failure to remove write permissions to this file is a
@kristianb21
kristianb21 / PHP-D7-rendering-fields
Created August 18, 2014 13:26
PHP:D7: Rendering Fields in tpl
<?php
// Reference http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way
//dpm($node);
// adding prefix and suffic html wraps
if (isset($content['field_urgent_check'])) {
$content['field_urgent_check']['#title'] = 'Urgent Job?';
$content['field_urgent_check']['#prefix'] = '<div class="node-data">';
$content['field_urgent_check']['#suffix'] = '</div>';
print render($content['field_urgent_check']);
}