Skip to content

Instantly share code, notes, and snippets.

View martsie's full-sized avatar

Marton Bodonyi martsie

View GitHub Profile
@martsie
martsie / DrupalWebTest.php
Created January 25, 2017 10:43
Example Drupal Web Test Class
<?php
/**
* @file
* Test for the home page of this website.
*/
class SiteTestingHomePageTest extends SiteTesting {
/**
* Stores the user created for this test.
*/
@martsie
martsie / SiteTesting.php
Created January 25, 2017 10:42
Example of an abstract Drupal Web Test Case
<?php
/**
* @file
* Common testing class for this Drupal site.
*/
abstract class SiteTesting extends DrupalWebTestCase {
/**
* Overrides default set up handler to prevent database sand-boxing.
*/
@martsie
martsie / hook-views-api.php
Last active January 25, 2017 10:29
Basic implementation of hook_views_api()
<?php
/**
* Implements hook_views_api().
*/
function my_custom_module_views_api() {
return array(
'api' => 3,
);
}
@martsie
martsie / drupal-hook-views-data.php
Created January 25, 2017 10:27
Basic implementation of Drupal 7 hook_views_data()
<?php
/**
* Implements hook_views_data().
*/
function my_custom_module_views_data() {
$data['my_custom_module']['table']['group'] = t('My custom module');
$data['my_custom_module']['table']['join'] = array(
// Exist in all views.
'#global' => array(),
);
@martsie
martsie / good-drupal-7-views-field.php
Created January 25, 2017 10:26
Basic implementation of custom Drupal 7 views field handler
<?php
/**
* @file
* Custom views handler definition.
*
* Place this code in
* /sites/all/[custom_module_name]/includes/views_handler_my_custom_field.inc
*/
/**
@martsie
martsie / bad-drupal-7-views-field.php
Created January 25, 2017 10:25
Bad implementation of custom views fields in Drupal 7
<?php
/**
* DO NOT USE THIS CODE
*
* Substituting custom field content in views hooks is a great way
* to make enemies with future developers.
*/
function my_confusing_module_views_pre_render(&$view) {
// This is messy and hard to understand for site builders and future developers.
$view->result[0]->custom_field_999 = t('Foo');
@martsie
martsie / permission_menu.php
Created January 25, 2017 10:22
Page access controls in Drupal 7
<?php
/**
* Implements hook_menu().
*
* Defines custom pages on the site.
*/
function my_custom_module_menu() {
$items['my-custom-page'] = array(
'title' => 'My custom page',
'page callback' => 'my_custom_module_custom_page',
@martsie
martsie / permission_node_denied.php
Created January 25, 2017 10:21
Example of denying access to a Drupal 7 node
<?php
/**
* Implements hook_node_access().
*
* Runs every time a user tries to view a node on the website.
*/
function my_custom_module_node_access($node, $op, $account) {
if ($node->type == 'page' && $op == 'view' && !user_access('my custom permission')) {
return NODE_ACCESS_DENY;
}
@martsie
martsie / permission_denied.php
Created January 25, 2017 10:19
Example of denying a page load based on Drupal 7 permissions
<?php
/**
* Implements hook_init().
*
* Runs every time a page is requested on the site.
*/
function my_custom_module_init() {
// Return a 403 page and prevent further operations if the user does not have the right permissions.
if (!user_access('access content')) {
drupal_access_denied();
@martsie
martsie / core_permissions.php
Created January 25, 2017 10:18
A simple implementation of a core permission check 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('access content')) {
drupal_set_message(t('User has Drupal core access content permission'));
}