Documentation is not that good in this module. I have followed the few example files and I have developed some code that is not working as expected. Let's see if someone can help me.
I am developing a submodule of moneysuite affiliates suite (https://www.drupal.org/project/moneysuite) so that a webmaster can develop, let's say, a white label theme, and show this theme to users that come from some affiliate.
I have developed three files following the guide of themekey example. The idea is that a themekey rule can be constructed depending on the value of affiliateID of a user.
ms_affiliates_themekey.module
<?php
/**
* @file
* ThemeKey support for changing theme depending on affiliate,
* good for white label templating while maintaining original
* theme on website for non affiliate visitors
*/
/**
* Implementation of hook_themekey_properties()
*
* Two assign a value to a property during a page request
* you have three possibilities:
* 1. Provide a mapping function from one property
* to another and tell ThemeKey about it using this hook.
* 2. Implement hook_themekey_global().
* 3. Implement hook_themekey_paths().
*
* There's an example implementation of this hook,
* @see themekey_example_themekey_properties()
*
* @return
* An array of ThemeKey properties and mapping functions:
* array of ThemeKey property attributes:
* key: namespace:property
* value: array(
* description => Readable name of property (required)
* validator => Callback function to validate a rule starting with that property (optional)
* TODO: describe validator arguments and return value
* file => File that provides the validator function (optional)
* path => Alternative path relative to dupal's doc root to load the file (optional)
* static => true/false, static properties don't occur in properties drop down
* and have fixed operator and value (optional)
* page cache => Level of page caching support:
* - THEMEKEY_PAGECACHE_SUPPORTED
* - THEMEKEY_PAGECACHE_UNSUPPORTED
* - THEMEKEY_PAGECACHE_TIMEBASED
* Default is THEMEKEY_PAGECACHE_UNSUPPORTED (optional)
* )
* array of mapping functions
* key: none (indexed)
* value: array(
* src => Source property path (required)
* dst => Destination property path (required)
* callback => Mapping callback (required)
* file => File that provides the callback function (optional)
* path => Alternative path relative to dupal's doc root to load the file (optional)
* )
*/
function ms_affiliates_themekey_themekey_properties() {
// Attributes of properties
$attributes = array();
$attributes['ms_affiliates:affiliate_id'] = array(
'description' => t('The affiliate id a user came from'),
'validator' => 'ms_affiliates_themekey_validator_aid',
'file' => 'ms_affiliates_themekey_validators.inc',
'page cache' => THEMEKEY_PAGECACHE_SUPPORTED,
);
// Mapping functions
$maps = array();
$maps[] = array(
'src' => 'system:dummy',
'dst' => 'ms_affiliates:affiliate_id',
'callback' => 'ms_affiliates_themekey_map_aid',
'file' => 'ms_affiliates_themekey_mappers.inc',
);
return array('attributes' => $attributes, 'maps' => $maps);
}
ms_affiliates_themekey_validators.inc
<?php
/**
* @file
* Validator functions for the Affiliates Suite Themekey module.
*/
/*
* name: ms_affiliates_themekey_validator_aid
* Validates a Theme Switching Rule that depends on the affiliate ID of Moneysuite Affiliates Suite.
* Allowed Operators: "=", "!"
* Allowed values: string of digits (numbers)
*
*
* @param $rule
* A Theme Switching Rule as associative array:
* - property: ThemeKey property as string (e.g., "drupal:path")
* - wildcard: optional string, only used if property is "drupal:path:wildcard"
* - operator: ThemeKey operator as string ("=", "!", "<". "<=", ">", ">=", "~")
* - value: ThemeKey property value as string
*
* @return
* An associative array of errors:
* - property: translated error message as string
* describing a problem with the property
* - wildcard: translated error message as string
* describing a problem with the wildcard
* - operator: translated error message as string
* describing a problem with the operator
* - value: translated error message as string
* describing a problem with the value
* If no errors detected the array is empty.
*
*/
function ms_affiliates_themekey_validator_aid($rule){
$errors = array();
switch ($rule['operator']) {
case '*':
case '!*':
case '<':
case '<=':
case '>':
case '>=':
case '~':
case '!~':
$errors['operator'] = t('Possible operators are "=", "!"');
break;
case '=':
if ('0' === $rule['value']) {
$errors['value'] = t('Use a value different from "0" to compare to an affiliate id');
}
elseif (!ctype_digit($rule['value'])) {
$errors['value'] = t('Affiliate id value must be a number');
}
break;
case '!':
if ('0' !== $rule['value']) {
$errors['value'] = t('Use value "0" to identify all users with affiliation');
}
break;
default:
$errors['property']=t('Unknown error');
}
return $errors;
}
ms_affiliates_themekey_mappers.inc
<?php
/**
* @file
* Mapping functions for the Affiliates Suite Themekey module.
*/
/*
*
* name: ms_affiliates_themekey_map_aid
*
* ThemeKey mapping function to set a
* ThemeKey property's value (destination)
* with the aid of another ThemeKey property (source).
*
* src: system:dummy
* dst: ms_affiliates:affiliate_id
*
* @param $data
* string containing current value of ThemeKey property user:aid
*
* @return
* user affiliate id from Moneysuite Affilates Suite for current user
*/
function ms_affiliates_themekey_map_aid($dummy){
global $user;
watchdog('ms_affiliates_themekey','Affiliate detected with UID='.$user->uid.', and affiliate id='.ms_affiliates_get_affiliate($user->uid).' changing theme');
return ms_affiliates_get_affiliate($user->uid);
}
My problem is that watchdog doesn't show the message line in mappers function and theme is not being switched. Any help on how to proceed?
Comments
Comment #2
mkalkbrenner1. Why don't you open a sandbox project to upload your code. That's easier to handle than that pasted code.
2. You should leverage ThemeKey Debug instead of watchdog.
3. Is suggest not to map system:dummy to ms_affiliates:affiliate_id, but user:uid. This way you get $uid as parameter for function ms_affiliates_themekey_map_aid($uid).
4. ThemeKey works in a very early step of the bootstrap process. I'm not sure if function ms_affiliates_get_affiliate() is fully functional at this point.
Comment #3
Farreres commented@mkalkbrenner
1. moneysuite project already has an approved project, this code is experimental and I only want to upload it when it is more or less functional.
2. I will check ThemeKey Debug
3. I already tried user:uid but as I got no result I changed it randomly. Will use it now
4. It's annoying that ms_affiliates_get_affiliate() is maybe not fully functional, but I will give it a try. Perhaps it is. Otherwise I will check if there are other mechanisms like going into the cookie or maybe something in session.
Will come back with the results.
Comment #4
Farreres commentedThanks for your help. I managed to make it work by relying the session data on affiliates. Will push now to git.