Created
April 16, 2013 22:41
-
-
Save kevinchampion/5400279 to your computer and use it in GitHub Desktop.
Radioactivity with Panels contexts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_ctools_render_alter(). | |
*/ | |
function isu_panel_layouts_ctools_render_alter(&$info, &$page, &$context) { | |
if (!empty($context['contexts'])) { | |
// I don't know why this is necessary, but just looping over the contexts | |
// array is throwing an unknown error. | |
$context_keys = array_keys($context['contexts']); | |
foreach ($context_keys as $key) { | |
$radioactivity_fields = $radioactivity_profiles = array(); | |
// TODO: Get this from somewhere. | |
$lang = LANGUAGE_NONE; | |
$entity_type = $context['contexts'][$key]->get_keyword(); | |
$entity_info = entity_get_info($entity_type); | |
// Find out if this context represents an entity page replacement. In | |
// some cases it might be useful to only allow panels views contexts | |
// to register a radioactivity view if they are full page replacements | |
// for ordinary entity pages eg. node/%node. | |
$is_entity_replacement = radioactivity_is_entity_page_replacement($entity_info['path'], arg()); | |
$entity = $context['contexts'][$key]->data; | |
$bundle = $entity->type; | |
$fields = field_info_instances($entity_type, $bundle); | |
foreach ($fields as $field) { | |
if ($field['widget']['type'] == 'radioactivity_basic_widget') { | |
$radioactivity_profile = radioactivity_decay_profile_load($field['settings']['profile']); | |
dpm($is_entity_replacement); | |
if ($is_entity_replacement == TRUE) { | |
_radioactivity_update_energy( | |
$entity_type, | |
$bundle, | |
$field['field_name'], | |
$lang, | |
$entity->$entity_info['entity keys']['id'], | |
$radioactivity_profile->granularity, | |
time(), | |
FALSE | |
); | |
} | |
} | |
} | |
} | |
} | |
} | |
/** | |
* Helper to determine if this panel view is an override of an ordinary entity | |
* view page. | |
* | |
* @param $path | |
* The path attribute as retrieved from the entity info. | |
* @param $args | |
* The url arguments. | |
* | |
* @return | |
* Boolean whether it's a replacement page or not. | |
*/ | |
function radioactivity_is_entity_page_replacement($entity_path, $args) { | |
$replacement = TRUE; | |
// Mung $path to break it into path components. | |
$entity_path_args = explode('/', $entity_path); | |
foreach ($entity_path_args as $key => $entity_path_arg) { | |
// Check for unmatching path args. | |
if ($entity_path_arg != $args[$key]) { | |
// Make sure entity path arg isn't an entity load arg AND url arg isn't | |
// numeric. | |
if (drupal_substr($entity_path_arg, 0, 1) != '%' || !is_numeric($args[$key])) { | |
$replacement = FALSE; | |
} | |
} | |
} | |
return $replacement; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
field_language($entity_type, $entity, $field_name = NULL, $langcode = NULL)
http://api.drupal.org/api/drupal/modules%21field%21field.multilingual.inc/function/field_language/7
Returns language for a specified field, taking into account user language, maybe this will work for line 47.
Nice snippet.