Skip to content

Instantly share code, notes, and snippets.

View martsie's full-sized avatar

Marton Bodonyi martsie

View GitHub Profile
@martsie
martsie / permission_check.php
Created January 25, 2017 10:17
A simple implementation of a custom permission check in hook_init in Drupal 7
<?php
/**
* Implements hook_init().
*
* Runs every time a page is requested on the site.
*/
function my_custom_module_init() {
if (user_access('my custom permission')) {
drupal_set_message(t('User has my custom permission'));
}
@martsie
martsie / hook_permission.php
Created January 25, 2017 10:15
Example implementation of Drupal 7 hook_permission
<?php
/**
* Implements hook_permission().
*
* Defines a new permission called 'my custom permission'.
*/
function my_custom_module_permission() {
return array(
'my custom permission' => array(
'title' => t('My custom permission'),
@martsie
martsie / reset-module-schema.sh
Created January 25, 2017 10:11
Resetting a Drupal 7 module schema via drush
drush sql-query "UPDATE system SET schema_version = 7001 WHERE name = 'my_custom_module';"
@martsie
martsie / oop-drupal-7-user-metadata-wrapper-hooks.php
Created January 25, 2017 09:49
Entity Metadata Wrapper hooks for Drupal 7 user data abstraction
<?php
/**
* Implements hook_entity_property_info_alter().
*/
function my_custom_module_entity_property_info_alter(&$info) {
$properties = &$info['user']['properties'];
$properties['employee_number'] = array(
'label' => t("Employee number"),
'description' => t("The employee number of this user."),
@martsie
martsie / oop-drupal-7-user-metadata-example.php
Created January 25, 2017 09:48
Entity Metadata Wrapper implementation of User Data abstraction in Drupal 7
<?php
global $user;
$account = entity_metadata_wrapper('user', $user);
$account->employee_number->set(60000);
entity_save('user', $account);
$employee_number = $account->employee_number->value();
@martsie
martsie / oop-drupal-7-user-abstraction.php
Created January 25, 2017 09:47
Drupal 7 OOP getter and setter for user data
<?php
class EmployeeDetails {
/**
* The associated user account.
*/
protected $account;
/**
* The key for accessing data.
*/
@martsie
martsie / oop-drupal-7-user.php
Last active January 25, 2017 09:47
Nicer OOP implementation of storing and accessing structured data in Drupal 7 user object
<?php
// Set the employee salary of a user.
$account = user_load(1);
$employee_details = new EmployeeDetails($account);
$employee_details->setEmployeeSalary(65000);
user_save($account);
// Retrieve the employee salary for a given user.
$account = user_load(1);
$employee_details = new EmployeeDetails($account);
@martsie
martsie / drupal7-unsafe-user-data.php
Created January 25, 2017 09:11
An unsafe implementation of storing structured data in the Drupal 7 User data property
<?php
$user->data['employee'] = array(
'employee_number' => 123456,
'employee_details' => array(
'salary' => 65000,
'access_level' => 'A',
'start_date' => 1425439660,
),
);
user_save($user);
@martsie
martsie / drupal7-user-data.php
Created January 25, 2017 09:09
Basic Drupal 7 User Data usage
<?php
// Add some data to the current user.
global $user;
$user->data['foo'] = 'bar';
user_save($user);
// Access some data from the current user.
global $user;
$user_data = $user->data['foo'];
@martsie
martsie / coffee-script-qunit-multiple-async-test.coffee
Created January 25, 2017 08:48
QUnit test in CoffeeScript with multiple async assertions
QUnit.module 'Waiter class'
test 'Waiter waits for 1 second twice', (assert) ->
done1 = do assert.async
done2 = do assert.async
assert.expect 2
waiter = new Waiter()