Skip to content

Instantly share code, notes, and snippets.

View martsie's full-sized avatar

Marton Bodonyi martsie

View GitHub Profile
@martsie
martsie / my_module.module.php
Created January 25, 2017 11:07
Drupal 7 AJAX command fallback callback
<?php
/**
* Non-JavaScript callback that returns hello world.
*/
function MY_CUSTOM_MODULE_nojs_callback() {
return "Hello world";
}
@martsie
martsie / my_module.module.php
Created January 25, 2017 11:06
Ajax callback for Drupal AJAX commands article
<?php
/**
* AJAX callback that prints hello world underneath our link.
*/
function MY_CUSTOM_MODULE_ajax_callback() {
// Define a new array to hold our AJAX commands.
$ajax_commands = array();
// Create a new AJAX command that replaces the #page text with our own text.
$ajax_commands[] = ajax_command_after('#my-special-link', 'Hello world');
@martsie
martsie / my_module.module.php
Created January 25, 2017 11:05
Trigger page callback for AJAX commands article
<?php
function MY_CUSTOM_MODULE_trigger_page() {
// Load in Drupal core AJAX library.
drupal_add_library('system', 'drupal.ajax');
// Load in our custom JavaScript.
drupal_add_js(drupal_get_path('module', 'MY_CUSTOM_MODULE') . '/MY_CUSTOM_MODULE.js');
// Present the user with a link with the id 'my-special-link'.
return l('Run my AJAX commands', 'my-custom-path/nojs', array(
@martsie
martsie / my_module.module.php
Created January 25, 2017 11:04
hook_menu implementation for AJAX commands
<?php
/**
* Implements hook_menu().
*/
function MY_CUSTOM_MODULE_menu() {
// Returns AJAX commands if the user has JavaScript turned on.
$items['my-custom-path/ajax'] = array(
'title' => 'My custom ajax callback',
'page callback' => 'MY_CUSTOM_MODULE_ajax_callback',
'access arguments' => array('access content'),
@martsie
martsie / consistent-drupal-ajax-title.php
Created January 25, 2017 11:02
An example of a consistent Drupal 7 page title update command
<?php
// Change the page title.
$site_title = variable_get('site_name');
$page_title = t('My Great Page');
$ajax_commands[] = ajax_command_invoke(NULL, 'changePageTitle', array($page_title . ' - ' . $site_title));
@martsie
martsie / drupal-ajax.php
Created January 25, 2017 11:01
Example of Drupal 7 Ajax command PHP implementation
// Change the page title.
$ajax_commands[] = ajax_command_invoke(NULL, 'changePageTitle', array(t('My great page title')));
@martsie
martsie / jquery.changePagetitle.js
Created January 25, 2017 11:00
Example of Drupal 7 ajax callback command in jQuery scope
/**
* Changes the page title. Can be called from Drupal AJAX commands using ajax_command_invoke().
*/
(function ($) {
$.fn.changePageTitle = function(pageTitle) {
document.title = pageTitle;
};
})(jQuery);
@martsie
martsie / template.php
Created January 25, 2017 10:50
Overriding theme_item_list in Drupal 7
<?php
function my_awesome_theme_item_list($variables) {
$items = $variables['items'];
$title = $variables['title'];
$type = $variables['type'];
$attributes = $variables['attributes'];
// Only output the list container and title, if there are any list items.
// Check to see whether the block title exists before adding a header.
// Empty headers are not semantic and present accessibility challenges.
@martsie
martsie / template.php
Created January 25, 2017 10:49
Overriding breadcrumbs in Drupal 7
<?php
function my_awesome_theme_breadcrumb($variables) {
$breadcrumb = $variables['breadcrumb'];
if (!empty($breadcrumb)) {
// Provide a navigational heading to give context for breadcrumb links to
// screen-reader users. Make the heading invisible with .element-invisible.
$output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
// Add 'my-awesome-class' to the breadcrumb wrapper and change the separators from '»' to '::'.
@martsie
martsie / assertions.php
Created January 25, 2017 10:45
Example of Drupal SimpleTest assertions
<?php
// This will fail.
$this->assertTrue('foo' == 'bar', 'The two strings are equal to eachother.');
// This will pass.
$this->assertTrue('foo' == 'foo', 'The two strings are equal to eachother.');