modules/custom/bdp/bdp.module
<?php
/**
* @file
* BDP generation.
*
* BDP view mode and template for nodes
*/
require_once dirname(__FILE__) . '/bdp.field.inc';
/**
* Implements hook_help().
*/
function bdp_help($path, $arg) {
// Show a warning on the 'BDP' view mode sub-tab of 'Manage Display' pages
// that re-ordering fields has no affect on the actual BDP templates used.
if (preg_match('/.*\/manage\/.*\/display\/bdp/', $path)) {
drupal_set_message(t('The ordering of these elements is hard-coded in the template files of the BDP module. Re-ordering here will not change any output in BDP.'), 'warning');
}
}
/**
* Implements hook_entity_info_alter().
*/
function bdp_entity_info_alter(array &$info) {
// Add an 'BDP' view mode to all possible entity types.
foreach (array_keys($info) as $entity_type) {
if (!empty($info[$entity_type]['view modes'])) {
$info[$entity_type]['view modes']['bdp'] = array(
'label' => t('BDP'),
'custom settings' => FALSE,
);
}
}
}
/**
* Implements hook_menu().
*/
function bdp_menu() {
$items = array();
$items['node/%node/bdp'] = array(
'page callback' => 'bdp_output_node',
'page arguments' => array(1),
'access callback' => 'node_access',
'access arguments' => array('view', 1),
'type' => MENU_CALLBACK,
'file' => 'bdp.pages.inc',
);
$items['admin/config/services/bdp'] = array(
'title' => 'BDP',
'description' => 'Configure BDP settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('bdp_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'bdp.admin.inc',
);
return $items;
}
/**
* Implements hook_entity_view().
*
* When an entity is being rendered with the 'bdp' view mode, convert the theme
* and template being used to use our own BDP theme/template.
*
* @see bdp_preprocess_bdp()
*/
function bdp_entity_view($entity, $type, $view_mode, $langcode) {
if ($view_mode == 'bdp') {
list(, , $bundle) = entity_extract_ids($type, $entity);
$entity->content += array(
'#entity' => $entity,
'#bundle' => $bundle,
'#theme' => "bdp",
);
}
}
/**
* Implements hook_theme().
*/
function bdp_theme($existing, $type, $theme, $path) {
$info['bdp'] = array(
'render element' => 'elements',
'template' => 'bdp',
'path' => $path . '/templates',
);
// Add support for our additional BDP templates.
$info += drupal_find_theme_templates($info, '.tpl.php', drupal_get_path('module', 'bdp') . '/templates');
$info['bdp_tag'] = array(
'render element' => 'element',
);
$info['bdp_tags'] = array(
'render element' => 'element',
);
// A theme wrapper for format_xml_elements().
$info['bdp_elements'] = array(
'variables' => array('bdp' => array()),
);
return $info;
}
function theme_bdp_elements($variables) {
return format_xml_elements($variables['bdp']);
}
function theme_bdp_tag($variables) {
$element = &$variables['element'];
if (isset($element['#value'])) {
$element['#value'] = check_plain($element['#value']);
}
elseif ($children = element_children($element)) {
$element['#value'] = '';
foreach ($children as $key) {
$element[$key] += array('#theme' => 'bdp_tag');
$element['#value'] .= drupal_render($element[$key]);
}
}
return theme_html_tag($variables);
}
function theme_bdp_tags($variables) {
$output = '';
$element = &$variables['element'];
foreach (element_children($element) as $key) {
$sub_element = array('element' => array());
$sub_element['element']['#tag'] = $element['#tag'];
if (isset($element['#attributes'])) {
$sub_element['element']['#attributes'] = $element['#attributes'];
}
$sub_element['element']['#value'] = drupal_render($element[$key]);
$output .= theme_html_tag($sub_element);
}
return $output;
}
/**
* Implements hook_node_view().
*/
function bdp_node_view($node, $view_mode, $langcode) {
if ($view_mode != 'bdp') {
return;
}
if ($node->type == 'data_set' || $node->type == 'data_source') {
$methods = array();
if ($items = field_get_items('node', $node, 'field_methods')) {
$method_instance = field_info_instance('node', 'field_methods', $node->type);
$instrumentation_instance = field_info_instance('node', 'field_instrumentation', $node->type);
$instrumentation_values = field_get_items('node', $node, 'field_instrumentation');
foreach ($items as $delta => $item) {
$methods['methdesc'] = strip_tags(_text_sanitize($method_instance, $langcode, $item, 'value'));
if (!empty($instrumentation_values[$delta])) {
$methods['methdesc'] .= ' Instrumentation:' . strip_tags(_text_sanitize($instrumentation_instance, $langcode, $instrumentation_values[$delta], 'value'));
}
}
}
if ($qa_items = field_get_items('node', $node, 'field_quality_assurance')) {
$qa_instance = field_info_instance('node', 'field_quality_assurance', $node->type);
$methods['methdesc'] .= ' Quality Control: ' . strip_tags(_text_sanitize($qa_instance, $langcode, $qa_items[0], 'value'));
}
if (!empty($methods)) {
$node->content['methods'] = array(
'#theme' => 'bdp_elements',
'#bdp' => $methods,
);
}
}
switch ($node->type) {
case 'data_set':
// Join all taxonomy fields into one content array to output in the
// template.
$node->content['keywordSets'] = array();
foreach (element_children($node->content) as $key) {
if (isset($node->content[$key]['#field_type'])
&& $node->content[$key]['#field_type'] == 'taxonomy_term_reference'
&& $node->content[$key]['#formatter'] == 'taxonomy_bdp_keywordset') {
$node->content['keywordSets'][] = $node->content[$key];
}
}
break;
}
}
/**
* Implements hook_date_format_types().
*/
function bdp_date_format_types() {
return array(
'bdp_yeardate' => t('BDP yearDate'),
);
}
/**
* Implements hook_date_formats().
*/
function bdp_date_formats() {
return array(
array(
'type' => 'bdp_yeardate',
'format' => 'Y-m-d',
'locales' => array(),
),
);
}
/**
* Implements hook_preprocess_HOOK() for bdp.tpl.php.
*/
function template_preprocess_bdp(&$variables, $hook) {
$variables['entity'] = $variables['elements']['#entity'];
$entity = $variables['entity'];
$entity_type = $variables['elements']['#entity_type'];
$bundle = $variables['elements']['#bundle'];
$label = entity_label($entity_type, $entity);
$uri = entity_uri($entity_type, $entity);
$variables['url'] = url($uri['path'], array('absolute' => TRUE) + $uri['options']);
$variables['label'] = check_plain($label);
$variables['language'] = check_plain($GLOBALS[LANGUAGE_TYPE_CONTENT]->name);
$variables['pubPlace'] = check_plain(variable_get('site_name', 'Drupal'));
$variables['station'] = check_plain(variable_get('station_acronym', 'STATION'));
$variables['data_policies'] = check_plain(strip_tags(variable_get('bdp_data_policies', '')));
if ($entity_type == 'node' && $variables['elements']['#bundle'] == 'data_set') {
$variables['pubDate'] = format_date($entity->created, 'bdp_yeardate');
}
// Helpful $content variable for templates.
$variables += array('content' => array());
foreach (element_children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
// Remove the field wrappers and classes so we just get native output.
if (isset($variables['content'][$key]['#theme']) && $variables['content'][$key]['#theme'] == 'field') {
unset($variables['content'][$key]['#theme']);
}
}
// Ensure that all the fields for this entity are available, even if empty.
foreach (field_info_instances($entity_type, $bundle) as $instance) {
if (!isset($variables['content'][$instance['field_name']])) {
$variables['content'][$instance['field_name']] = '';
}
}
// Add template suggestions to use, starting with the least preferred, and
// ending with the one to try first if it exists. The last one should be the
// most specific.
$variables['theme_hook_suggestions'][] = 'bdp';
$variables['theme_hook_suggestions'][] = "bdp__{$entity_type}__{$bundle}";
}
function bdp_debug($message, array $variables = array()) {
if (variable_get('bdp_debugging')) {
drupal_set_message(format_string('BDP DEBUG: ' . $message, $variables));
watchdog('bdp', $message, $variables, WATCHDOG_DEBUG);
}
}