ds.module in Display Suite 7
Same filename and directory in other branches
Display Suite core functions.
File
ds.moduleView source
<?php
/**
* @file
* Display Suite core functions.
*/
/**
* Constants for field types.
*/
define('DS_FIELD_TYPE_THEME', 1);
define('DS_FIELD_TYPE_FUNCTION', 2);
define('DS_FIELD_TYPE_PREPROCESS', 3);
define('DS_FIELD_TYPE_IGNORE', 4);
define('DS_FIELD_TYPE_CODE', 5);
define('DS_FIELD_TYPE_BLOCK', 6);
define('DS_FIELD_TYPE_CTOOLS', 7);
/**
* Constants for block fields rendering.
*/
define('DS_BLOCK_TEMPLATE', 1);
define('DS_BLOCK_TITLE_CONTENT', 2);
define('DS_BLOCK_CONTENT', 3);
/**
* Implements hook_permission().
*/
function ds_permission() {
return array(
'admin_view_modes' => array(
'title' => t('Administer view modes'),
'description' => t('Create custom view modes for every entity.'),
),
'admin_fields' => array(
'title' => t('Administer fields'),
'description' => t('Create fields for every entity.'),
),
'admin_display_suite' => array(
'title' => t('Administer Display Suite'),
'description' => t('General permission for Display Suite settings pages.'),
),
);
}
/**
* Implements hook_hook_info().
*/
function ds_hook_info() {
$hooks['ds_fields_info'] = array(
'group' => 'ds_fields_info',
);
return $hooks;
}
/**
* Implements hook_menu().
*/
function ds_menu() {
$items = array();
// Start page of Display Suite.
$items['admin/structure/ds'] = array(
'title' => 'Display suite',
'description' => 'Manage configuration for Display suite like fields, view modes etc.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array(
'admin_display_suite',
),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
// Emergency page
$items['admin/structure/ds/emergency'] = array(
'title' => 'Emergency',
'description' => 'In case you have errors via Display suite, visit this page.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_emergency',
),
'access arguments' => array(
'admin_display_suite',
),
'file' => 'ds.layout.inc',
'weight' => 100,
);
// Custom styles.
$items['admin/structure/ds/styles'] = array(
'title' => 'Styles',
'description' => 'Define classes which you can use as classes on regions and fields (if available).',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_styles_form',
),
'file' => 'ds.styles.inc',
'access arguments' => array(
'admin_display_suite',
),
);
// Layout overview.
$items['admin/structure/ds/layout'] = array(
'title' => 'Layout',
'description' => 'Displays a list of content which can be styled.',
'page callback' => 'ds_layout_list',
'file' => 'ds.layout.inc',
'access arguments' => array(
'admin_display_suite',
),
);
// Change layout.
$items['admin/structure/ds/change-layout'] = array(
'title' => 'Change layout',
'description' => 'Act on layout change to move fields elsewhere',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_field_ui_layout_change',
),
'access arguments' => array(
'admin_display_suite',
),
'file' => 'ds.field_ui.inc',
'type' => MENU_VISIBLE_IN_BREADCRUMB,
);
// Revert layout.
$items['admin/structure/ds/revert-layout'] = array(
'title' => 'Revert layout',
'description' => 'Revert layout and field settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_revert_layout_field_settings_form',
),
'file' => 'ds.field_ui.inc',
'access arguments' => array(
'admin_display_suite',
),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
);
// View modes.
$items['admin/structure/ds/view_modes'] = array(
'title' => 'View modes',
'description' => 'Manage view modes for all content.',
'page callback' => 'ds_view_modes_list',
'file' => 'ds.view_modes.inc',
'access arguments' => array(
'admin_view_modes',
),
);
$items['admin/structure/ds/view_modes/manage'] = array(
'title' => 'Add a view mode',
'description' => 'Manage a view mode',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_edit_view_mode_form',
),
'file' => 'ds.view_modes.inc',
'access arguments' => array(
'admin_view_modes',
),
'type' => MENU_LOCAL_ACTION,
);
$items['admin/structure/ds/view_modes/delete'] = array(
'title' => 'Delete a view mode',
'description' => 'Delete a custom view mode.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_delete_view_mode_confirm',
),
'file' => 'ds.view_modes.inc',
'access arguments' => array(
'admin_view_modes',
),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
);
$items['admin/structure/ds/view_modes/revert'] = array(
'title' => 'Revert a view mode',
'description' => 'Revert a view mode.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_revert_view_mode_confirm',
),
'file' => 'ds.view_modes.inc',
'access arguments' => array(
'admin_view_modes',
),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
);
// Fields.
$items['admin/structure/ds/fields'] = array(
'title' => 'Fields',
'description' => 'Manage fields for all content.',
'page callback' => 'ds_custom_fields_list',
'file' => 'ds.fields.inc',
'access arguments' => array(
'admin_fields',
),
);
$items['admin/structure/ds/fields/manage'] = array(
'title' => 'Fields edit',
'page callback' => 'ds_custom_manage',
'file' => 'ds.fields.inc',
'access arguments' => array(
'admin_fields',
),
'type' => MENU_CALLBACK,
);
$items['admin/structure/ds/fields/manage_custom'] = array(
'title' => 'Add a code field',
'description' => 'Manage a field',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_edit_custom_field_form',
),
'file' => 'ds.fields.inc',
'access arguments' => array(
'admin_fields',
),
'type' => MENU_LOCAL_ACTION,
'weight' => 0,
);
$items['admin/structure/ds/fields/manage_ctools'] = array(
'title' => 'Add a dynamic field',
'description' => 'Manage a dynamic field',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_edit_ctools_field_form',
),
'file' => 'ds.fields.inc',
'access arguments' => array(
'admin_fields',
),
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
);
$items['admin/structure/ds/fields/manage_ctools/content'] = array(
'title' => 'Ctools field content',
'page callback' => 'ds_ctools_content',
'file' => 'ds.field_ui.inc',
'access arguments' => array(
'admin_display_suite',
),
'type' => MENU_CALLBACK,
);
$items['admin/structure/ds/fields/manage_preprocess'] = array(
'title' => 'Add a preprocess field',
'description' => 'Manage a preprocess field',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_edit_preprocess_field_form',
),
'file' => 'ds.fields.inc',
'access arguments' => array(
'admin_fields',
),
'type' => MENU_LOCAL_ACTION,
'weight' => 3,
);
$items['admin/structure/ds/fields/delete'] = array(
'title' => 'Delete a field',
'description' => 'Delete a field.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_delete_field_confirm',
),
'file' => 'ds.fields.inc',
'access arguments' => array(
'admin_fields',
),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
);
$items['admin/structure/ds/fields/revert'] = array(
'title' => 'Revert a custom field',
'description' => 'Revert a custom field.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_revert_field_confirm',
),
'file' => 'ds.fields.inc',
'access arguments' => array(
'admin_fields',
),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
);
// Block can be disabled.
if (module_exists('block')) {
$items['admin/structure/ds/fields/manage_block'] = array(
'title' => 'Add a block field',
'description' => 'Manage a block field',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'ds_edit_block_field_form',
),
'file' => 'ds.fields.inc',
'access arguments' => array(
'admin_fields',
),
'type' => MENU_LOCAL_ACTION,
'weight' => 2,
);
}
// CTools Modal add field.
$items['admin/structure/ds/%ctools_js/add_field/%'] = array(
'title' => 'Add field',
'page callback' => 'ds_ajax_add_field',
'page arguments' => array(
3,
5,
),
'access arguments' => array(
'admin_fields',
),
'file' => 'ds.field_ui.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_theme().
*/
function ds_theme() {
module_load_include('inc', 'ds', 'ds.registry');
return _ds_theme();
}
/**
* Implements hook_ds_layout_info().
*/
function ds_ds_layout_info() {
module_load_include('inc', 'ds', 'ds.registry');
return _ds_ds_layout_info();
}
/**
* Implements hook_ctools_plugin_api().
*/
function ds_ctools_plugin_api($owner, $api) {
if ($owner == 'ds' && ($api == 'ds' || $api == 'plugins')) {
return array(
'version' => 1,
);
}
}
/**
* Implements hook_ctools_plugin_directory().
*/
function ds_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'content_types') {
return 'plugins/' . $plugin_type;
}
}
/**
* Implements hook_views_api().
*/
function ds_views_api() {
return array(
'api' => 3,
);
}
/**
* Implements hook_node_type_update().
*/
function ds_node_type_update($info) {
if (!empty($info->old_type) && $info->old_type != $info->type) {
module_load_include('inc', 'ds', 'ds.registry');
_ds_entity_type_update('node', $info, 'update');
}
}
/**
* Implements hook_node_type_delete().
*/
function ds_node_type_delete($info) {
module_load_include('inc', 'ds', 'ds.registry');
_ds_entity_type_update('node', $info, 'delete');
}
/**
* Implements hook_theme_registry_alter().
*/
function ds_theme_registry_alter(&$theme_registry) {
module_load_include('inc', 'ds', 'ds.registry');
_ds_theme_registry_alter($theme_registry);
}
/**
* Implements hook_entity_info_alter().
*/
function ds_entity_info_alter(&$entity_info) {
module_load_include('inc', 'ds', 'ds.registry');
_ds_entity_info_alter($entity_info);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ds_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
form_load_include($form_state, 'inc', 'ds', 'ds.field_ui');
// Also load admin on behalf of DS extras when enabled.
if (module_exists('ds_extras')) {
form_load_include($form_state, 'inc', 'ds_extras', 'ds_extras.admin');
}
ds_field_ui_fields_layouts($form, $form_state);
}
/**
* Implements hook_module_implements_alter().
*/
function ds_module_implements_alter(&$implementations, $hook) {
// node_field_display_module_alter() disables all labels on all fields
// when the view mode is 'search_index'. If you set display modes for
// this view mode by hand, then the hook isn't needed. Since this
// may be called hundreds of times on some pages, it's worth disabling it.
// See http://drupal.org/node/834278
// This code is also in Performance hacks module, but it's not bad to
// disable this too in Display Suite by default.
if ($hook == 'field_display_node_alter') {
unset($implementations['node']);
}
}
/**
* Implements hook_features_api().
*/
function ds_features_api() {
module_load_include('inc', 'ds', 'ds.registry');
return _ds_features_api();
}
/**
* Implements hook_filter_info().
*/
function ds_filter_info() {
$filters['ds_code'] = array(
'title' => t('Display Suite evaluator'),
'description' => t('This filter will only work in the Display Suite text format, machine name is <em>ds_code</em>. No other filters can be enabled either.'),
'process callback' => 'ds_php_eval',
'tips callback' => 'ds_filter_tips',
'cache' => FALSE,
);
return $filters;
}
/**
* Tips callback for Display Suite php filter.
*/
function ds_filter_tips($filter, $format, $long = FALSE) {
global $base_url;
if ($long) {
$output = '<h4>' . t('Using custom code with Display Suite') . '</h4>';
$output .= t('Include <?php ?> tags when using PHP. The $entity object is available.');
return $output;
}
else {
return t('You may post Display Suite code. You should include <?php ?> tags when using PHP. The $entity object is available.');
}
}
/**
* Function to check if we go on with Display suite.
*/
function ds_kill_switch() {
if (variable_get('ds_disable', FALSE)) {
return TRUE;
}
return FALSE;
}
/**
* Get entity view modes.
*
* @param $entity_type
* The name of the entity type.
*/
function ds_entity_view_modes($entity_type = NULL) {
if (!empty($entity_type)) {
switch ($entity_type) {
// For taxonomy terms the base table and the entity type are different
case 'taxonomy_term_data':
$entity_info = entity_get_info('taxonomy_term');
break;
default:
$entity_info = entity_get_info($entity_type);
break;
}
return $entity_info['view modes'];
}
}
/**
* Get Display suite layouts.
*/
function ds_get_layout_info() {
static $layouts = FALSE;
if (!$layouts) {
$errors = array();
$layouts = module_invoke_all('ds_layout_info');
// Give modules a chance to alter the layouts information.
drupal_alter('ds_layout_info', $layouts);
// Check that there is no 'content' region, but ignore panel layouts.
// Because when entities are rendered, the field items are stored into a
// 'content' key so fields would be overwritten before they're all moved.
foreach ($layouts as $key => $info) {
if (isset($info['panels'])) {
continue;
}
if (isset($info['regions']['content'])) {
$errors[] = $key;
}
}
if (!empty($errors)) {
drupal_set_message(t('Following layouts have a "content" region key which is invalid: %layouts.', array(
'%layouts' => implode(', ', $errors),
)), 'error');
}
}
return $layouts;
}
/**
* Get a layout for a given entity.
*
* @param $entity_type
* The name of the entity.
* @param $bundle
* The name of the bundle.
* @param $view_mode
* The name of the view mode.
* @param $fallback
* Whether to fallback to default or not.
*
* @return $layout
* Array of layout variables for the regions.
*/
function ds_get_layout($entity_type, $bundle, $view_mode, $fallback = TRUE) {
static $layouts = array();
$layout_key = $entity_type . '_' . $bundle . '_' . $view_mode;
if (!isset($layouts[$layout_key])) {
$entity_info = entity_get_info();
$overriden = TRUE;
if ($view_mode != 'form') {
$view_mode_settings = field_view_mode_settings($entity_type, $bundle);
$overriden = !empty($view_mode_settings[$view_mode]['custom_settings']) ? TRUE : FALSE;
}
// Check if a layout is configured.
if (isset($entity_info[$entity_type]['bundles'][$bundle]['layouts'][$view_mode]) && ($overriden || $view_mode == 'default')) {
$layouts[$layout_key] = $entity_info[$entity_type]['bundles'][$bundle]['layouts'][$view_mode];
$layouts[$layout_key]['view_mode'] = $view_mode;
}
// In case $view_mode is not found, check if we have a default layout,
// but only if the view mode settings aren't overridden for this view mode.
if ($view_mode != 'default' && !$overriden && $fallback && isset($entity_info[$entity_type]['bundles'][$bundle]['layouts']['default'])) {
$layouts[$layout_key] = $entity_info[$entity_type]['bundles'][$bundle]['layouts']['default'];
$layouts[$layout_key]['view_mode'] = 'default';
}
// Register the false return value as well.
if (!isset($layouts[$layout_key])) {
$layouts[$layout_key] = FALSE;
}
}
return $layouts[$layout_key];
}
/**
* Get all fields.
*
* @param $entity_type
* The name of the entity.
* @param $cache
* Whether we need to get the fields from cache or not.
* @return
* Collection of fields.
*/
function ds_get_fields($entity_type, $cache = TRUE) {
global $language;
static $static_fields, $fields_cached = array();
static $loaded = FALSE;
// Get fields from cache.
if (!$loaded) {
$loaded = TRUE;
if ($cache && ($cached_fields = cache_get('ds_fields:' . $language->language))) {
$fields_cached = $static_fields = $cached_fields->data;
}
}
if (!isset($static_fields[$entity_type])) {
// Do we have them cached or not ?
if (!isset($fields_cached[$entity_type])) {
// Get all fields for this entity type.
$fields = array();
foreach (module_implements('ds_fields_info') as $module) {
$function = $module . '_ds_fields_info';
$all_fields = $function($entity_type);
if (!empty($all_fields)) {
foreach ($all_fields as $key => $field_results) {
if ($key === $entity_type) {
// Add module key to field definition.
foreach ($field_results as $f => $d) {
$field_results[$f]['module'] = $module;
}
$fields = array_merge($field_results, $fields);
}
}
}
}
// Give modules a change to alter fields.
drupal_alter('ds_fields_info', $fields, $entity_type);
$fields_cached[$entity_type] = $fields;
// Cache the fields.
if ($cache) {
cache_set('ds_fields:' . $language->language, $fields_cached, 'cache');
}
}
else {
$fields = $fields_cached[$entity_type];
}
// Store the fields statically.
$static_fields[$entity_type] = $fields;
}
return isset($static_fields[$entity_type]) ? $static_fields[$entity_type] : array();
}
/**
* Get the field settings.
*
* @param $entity_type
* The name of the entity.
* @param $bundle
* The name of bundle (ie, page or story for node types, profile for users)
* @param $view_mode
* The name of view mode.
*/
function ds_get_field_settings($entity_type, $bundle, $view_mode, $default = TRUE) {
static $field_settings = NULL;
if (!isset($field_settings)) {
if ($cache = cache_get('ds_field_settings')) {
$field_settings = $cache->data;
}
else {
ctools_include('export');
$ds_field_settings = ctools_export_crud_load_all('ds_field_settings');
foreach ($ds_field_settings as $layout => $layout_settings) {
if (!isset($layout_settings->settings)) {
continue;
}
foreach ($layout_settings->settings as $field => $settings) {
$field_settings[$layout_settings->entity_type][$layout_settings->bundle][$layout_settings->view_mode][$field] = $settings;
}
}
cache_set('ds_field_settings', $field_settings, 'cache');
}
}
return isset($field_settings[$entity_type][$bundle][$view_mode]) ? $field_settings[$entity_type][$bundle][$view_mode] : (isset($field_settings[$entity_type][$bundle]['default']) && $default ? $field_settings[$entity_type][$bundle]['default'] : array());
}
/**
* Get the value for a Display Suite field.
*
* @param $key
* The key of the field.
* @param $field
* The configuration of a DS field.
* @param $entity
* The current entity.
* @param $entity_type
* The name of the entity.
* @param $bundle
* The name of the bundle.
* @param $view_mode
* The name of the view mode.
* @param $build
* The current built of the entity.
* @return $markup
* The markup of the field used for output.
*/
function ds_get_field_value($key, $field, $entity, $entity_type, $bundle, $view_mode, $build = array()) {
$field['field_name'] = $key;
$field['entity'] = $entity;
$field['entity_type'] = $entity_type;
$field['bundle'] = $bundle;
$field['view_mode'] = $view_mode;
$field['build'] = $build;
// Special case for ds_views which can handle custom fields now.
if ($field['field_type'] != DS_FIELD_TYPE_PREPROCESS && $entity_type == 'ds_views') {
$entity->preprocess_fields[] = $key;
}
switch ($field['field_type']) {
case DS_FIELD_TYPE_PREPROCESS:
$entity->preprocess_fields[] = $key;
break;
case DS_FIELD_TYPE_FUNCTION:
if (isset($field['file'])) {
include_once $field['file'];
}
return $field['function']($field);
case DS_FIELD_TYPE_THEME:
$format = isset($field['formatter']) ? $field['formatter'] : key($field['properties']['formatters']);
return theme($format, $field);
case DS_FIELD_TYPE_CODE:
return ds_render_code_field($field);
case DS_FIELD_TYPE_CTOOLS:
return ds_render_ctools_field($field);
case DS_FIELD_TYPE_BLOCK:
return ds_render_block_field($field);
}
}
/**
* Implements hook_field_attach_view_alter().
*/
function ds_field_attach_view_alter(&$build, $context) {
static $loaded_css = array();
// Global kill switch. In some edge cases, a view might
// be inserted into the view of an entity, in which the
// same entity is available as well. This is simply not
// possible, so you can temporarily disable DS completely
// by setting this variable, either from code or via
// the UI through admin/structure/ds/
if (ds_kill_switch()) {
return;
}
// If views and core doesn't send information along on the entity,
// Display suite doesn't have a context to render the fields.
if (!isset($build['#entity_type']) && !isset($build['#bundle'])) {
return;
}
// If no layout is configured, stop as well.
if (!ds_get_layout($build['#entity_type'], $build['#bundle'], $context['view_mode'])) {
return;
}
$entity_type = $build['#entity_type'];
$bundle = $build['#bundle'];
$view_mode = $context['view_mode'];
$entity = $context['entity'];
$layout = ds_get_layout($entity_type, $bundle, $view_mode);
// Do not render panel layouts.
if (!empty($layout['settings']['ds_panels'])) {
return;
}
// Check on field limit.
if (isset($layout['settings']['limit'])) {
foreach ($layout['settings']['limit'] as $field => $limit) {
if (isset($build[$field])) {
$count = count($build[$field]['#items']);
if ($count > $limit) {
$build[$field]['#items'] = array_slice($build[$field]['#items'], 0, $limit);
}
}
}
}
// Add Display Suite display fields.
$fields = ds_get_fields($entity_type);
$field_values = ds_get_field_settings($entity_type, $bundle, $layout['view_mode']);
foreach ($field_values as $key => $field) {
// Ignore if this field is not a DS field.
if (!isset($fields[$key])) {
continue;
}
$field = $fields[$key];
if (isset($field_values[$key]['format'])) {
$field['formatter'] = $field_values[$key]['format'];
}
if (isset($field_values[$key]['formatter_settings'])) {
$field['formatter_settings'] = $field_values[$key]['formatter_settings'];
}
$field_value = ds_get_field_value($key, $field, $entity, $entity_type, $bundle, $view_mode, $build);
// Title label.
if ($key == 'title' && $entity_type == 'node') {
$node_type = node_type_get_type($entity);
$field['title'] = function_exists('i18n_node_translate_type') ? i18n_node_translate_type($node_type->type, 'title_label', $node_type->title_label) : $node_type->title_label;
}
if ($field_value) {
$build[$key] = array(
'#theme' => 'field',
'#field_type' => 'ds',
'#title' => $field['title'],
'#weight' => isset($field_values[$key]['weight']) ? $field_values[$key]['weight'] : 0,
'#label_display' => isset($field_values[$key]['label']) ? $field_values[$key]['label'] : 'inline',
'#field_name' => $key,
'#bundle' => $bundle,
'#entity_type' => $entity_type,
'#view_mode' => $view_mode,
'#access' => TRUE,
'#items' => array(
0 => array(
'value' => $field_value,
),
),
0 => array(
'#markup' => $field_value,
),
);
}
}
// Add path to css file for this layout and disable block regions if necessary.
if (isset($layout['css']) && !isset($loaded_css[$layout['path'] . '/' . $layout['layout'] . '.css'])) {
// Disable blocks.
if (isset($layout['settings']['hide_sidebars']) && $layout['settings']['hide_sidebars']) {
ctools_set_no_blocks();
}
// Register css file.
$loaded_css[$layout['path'] . '/' . $layout['layout'] . '.css'] = TRUE;
// Add css file.
if (isset($layout['module']) && $layout['module'] == 'panels') {
$build['#attached']['css'][] = $layout['path'] . '/' . $layout['panels']['css'];
}
else {
$build['#attached']['css'][] = $layout['path'] . '/' . $layout['layout'] . '.css';
}
}
}
/**
* Add variables to an entity.
*
* This function is added in ds_theme_registry_alter().
*/
function ds_entity_variables(&$vars) {
if (isset($vars['elements']) && isset($vars['elements']['#bundle']) && ($layout = ds_get_layout($vars['elements']['#entity_type'], $vars['elements']['#bundle'], $vars['elements']['#view_mode']))) {
// Do not render panel layouts.
if (!empty($layout['settings']['ds_panels'])) {
return;
}
$render_container = 'content';
// User uses user_profile as container.
if ($vars['elements']['#entity_type'] == 'user') {
$render_container = 'user_profile';
}
// Move any preprocess fields to render container.
// Inconsitency in taxonomy term naming.
$object = $vars['elements']['#entity_type'];
if ($vars['elements']['#entity_type'] == 'taxonomy_term') {
$object = 'term';
}
if (isset($vars[$object]->preprocess_fields)) {
foreach ($vars[$object]->preprocess_fields as $key => $field) {
// Process RDF if the module is enabled before moving preprocess fields.
if (module_exists('rdf')) {
rdf_process($vars, $field);
// Remove it so we can unset the field later on.
unset($vars['rdf_template_variable_attributes_array'][$field]);
}
// Move the field to content so it renders, remove it
// because we don't need it anymore.
if (isset($vars[$field])) {
$vars[$render_container][$field] = $vars[$field];
if (!isset($vars['preprocess_keep'])) {
unset($vars[$field]);
}
}
}
}
// Hide empty regions variable.
$hide_empty_regions = $layout['settings']['hide_empty_regions'];
// Check if this is a flexible panels layout.
if (!empty($layout['flexible'])) {
ctools_include('plugins', 'panels');
$vars['css_id'] = $vars['settings'] = $vars['display'] = $vars['renderer'] = '';
$vars['layout'] = panels_get_layout($layout['panels']['name']);
$vars['theme_hook_suggestion'] = 'panels_flexible';
}
else {
// Template layout.
if (!isset($vars['classes_array'])) {
$vars['classes_array'] = array();
}
$vars['classes_array'][] = 'view-mode-' . $vars['elements']['#view_mode'];
// In case this is a panels layout, use panels info array.
if (isset($layout['module']) && $layout['module'] == 'panels') {
$layout['layout'] = $layout['panels']['theme'];
}
$vars['theme_hook_suggestions'][] = $layout['layout'];
$vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'];
$vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $vars['elements']['#bundle'];
$vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $vars['elements']['#bundle'] . '_' . $vars['elements']['#view_mode'];
}
// Allow modules to alter the region layout before it is rendered.
drupal_alter('ds_regions', $layout, $vars[$object]);
// Create region variables based on the layout settings.
foreach ($layout['regions'] as $region_name => $region) {
// Create the region content.
$vars[$region_name] = ds_render_region($vars[$render_container], $region_name, $layout);
// Hide empty regions.
if ($hide_empty_regions && empty($vars[$region_name]) && empty($layout['flexible'])) {
$vars[$region_name] = FALSE;
}
elseif (empty($vars[$region_name])) {
$vars[$region_name] = ' ';
}
// In case this is a panels layout, add the variable to the $content variable.
if (isset($layout['module']) && $layout['module'] == 'panels') {
$vars['content'][$region_name] = $vars[$region_name];
unset($vars[$region_name]);
}
// Add extras classes to the region.
if (empty($layout['flexible'])) {
$vars[$region_name . '_classes'] = !empty($layout['settings']['classes'][$region_name]) ? ' ' . implode(' ', $layout['settings']['classes'][$region_name]) : '';
}
}
// Let other modules know we have rendered.
$vars['rendered_by_ds'] = TRUE;
}
}
/**
* Render a region.
*
* @param $content
* An array of content fields.
* @param $region
* The name of region to render.
* @param $layout
* The layout definition.
*/
function ds_render_region($content, $region, $layout) {
$output = '';
if (isset($layout['settings']['regions'][$region])) {
foreach ($layout['settings']['regions'][$region] as $key => $field) {
// Make sure the field exists.
if (!isset($content[$field])) {
continue;
}
// Safe-guard against strings.
if (is_string($content[$field])) {
$output .= $content[$field];
}
else {
$output .= drupal_render($content[$field]);
}
}
}
return $output;
}
/**
* Wrapper function around PHP eval(). We don't use php_eval from
* the PHP module because custom fields might need properties from
* the current entity.
*
* @param $code
* The code to evaluate from the custom field.
* @param $object
* An object to use for evaluation.
* @return $output
* The output from eval.
*/
function ds_php_eval($code, $entity, $build = array()) {
global $theme_path, $theme_info, $conf;
// Store current theme path.
$old_theme_path = $theme_path;
// Restore theme_path to the theme, as long as ds_php_eval() executes,
// so code evaluted will not see the caller module as the current theme.
// If theme info is not initialized get the path from theme_default.
if (!isset($theme_info)) {
$theme_path = drupal_get_path('theme', $conf['theme_default']);
}
else {
$theme_path = dirname($theme_info->filename);
}
ob_start();
print eval('?>' . $code);
$output = ob_get_contents();
ob_end_clean();
// Recover original theme path.
$theme_path = $old_theme_path;
return $output;
}
/**
* Create entity context.
*/
function ds_create_entity_context($entity_type, $entity, &$contexts, $context_arguments = array()) {
ctools_include('context');
if (empty($context_arguments)) {
$context_arguments = array(
array(
'keyword' => $entity_type,
'identifier' => drupal_ucfirst($entity_type) . ' being viewed',
'id' => 1,
'name' => 'entity_id:' . $entity_type,
'settings' => array(),
),
);
}
ctools_context_get_context_from_arguments($context_arguments, $contexts, array(
$entity,
));
}
/**
* Render a code field.
*/
function ds_render_code_field($field) {
if (isset($field['properties']['code'])) {
$format = isset($field['properties']['code']['format']) ? $field['properties']['code']['format'] : 'plain_text';
if ($format != 'ds_code') {
$value = check_markup($field['properties']['code']['value'], $format);
}
else {
$value = ds_php_eval($field['properties']['code']['value'], $field['entity'], isset($field['build']) ? $field['build'] : array());
}
// Token support - check on token property so we don't run every single field through token.
if (isset($field['properties']['use_token']) && $field['properties']['use_token'] == TRUE) {
$value = token_replace($value, array(
$field['entity_type'] => $field['entity'],
), array(
'clear' => TRUE,
));
}
return $value;
}
}
/**
* Render a CTools field.
*/
function ds_render_ctools_field($field) {
if (isset($field['formatter_settings']['ctools'])) {
// Extreme use case where a taxonomy_term object is not
// loaded on the entity and triggers notices if a view is embedded
// with taxonomy term fields from the same object.
// see http://drupal.org/node/1238132 - To reproduce:
// 1) add 2 taxonomy field instances on a bundle
// 2) configure a ds layout showing only one
// 3) embed a view with the 2 taxonomies as fields.
if (isset($field['formatter_settings']['load_terms']) && $field['formatter_settings']['load_terms']) {
static $terms_loaded = array();
$language = $field['entity']->language;
$instances = field_info_instances($field['entity_type'], $field['bundle']);
foreach ($instances as $key => $instance) {
$info = field_info_field($key);
if ($info['module'] == 'taxonomy') {
if (empty($field['entity']->{$key})) {
continue;
}
if (!isset($field['entity']->{$key}[$language])) {
$language = LANGUAGE_NONE;
}
foreach ($field['entity']->{$key}[$language] as $tkey => $item) {
if (isset($item['tid']) && !isset($item['taxonomy_term'])) {
if (!isset($terms_loaded[$item['tid']])) {
$terms_loaded[$item['tid']] = taxonomy_term_load($item['tid']);
}
$field['entity']->{$key}[$language][$tkey]['taxonomy_term'] = $terms_loaded[$item['tid']];
}
}
}
}
}
ctools_include('content');
ctools_include('context');
// Get variables.
$show_title = $field['formatter_settings']['show_title'];
$title_wrapper = trim($field['formatter_settings']['title_wrapper']);
$ctools = unserialize($field['formatter_settings']['ctools']);
$type = $ctools['type'];
$subtype = $ctools['subtype'];
$conf = $ctools['conf'];
$entity_type = $field['entity_type'];
$keywords = $arguments = $contexts = array();
// Create entity context.
ds_create_entity_context($entity_type, $field['entity'], $contexts);
// Build the content.
$data = ctools_content_render($type, $subtype, $conf, $keywords, $arguments, $contexts);
// Return content.
if (!empty($data->content)) {
$content = '';
if ($show_title) {
if (empty($title_wrapper)) {
$title_wrapper = 'div';
}
$content .= '<' . check_plain($title_wrapper) . '>' . $data->title . '</' . check_plain($title_wrapper) . '>';
}
if (is_array($data->content)) {
$content .= drupal_render($data->content);
}
else {
$content .= $data->content;
}
return $content;
}
}
}
/**
* Render a block field.
*/
function ds_render_block_field($field) {
list($module, $delta) = explode('|', $field['properties']['block']);
$block = module_invoke($module, 'block_view', $delta);
if (isset($block['content']) && is_array($block['content'])) {
$block['content'] = drupal_render($block['content']);
}
if (!empty($block['content'])) {
global $theme_key;
$block_title = db_query("SELECT title FROM {block} WHERE module = :module AND delta = :delta AND theme = :theme", array(
':module' => $module,
':delta' => $delta,
':theme' => $theme_key,
))
->fetchField();
if (!empty($block_title)) {
$block['subject'] = $block_title == '<none>' ? '' : check_plain($block_title);
}
// i18n support.
if (function_exists('i18n_block_block_view_alter')) {
// Check language visibility.
global $language;
static $block_languages = FALSE;
if (!$block_languages) {
$block_languages = array();
$result = db_query('SELECT module, delta, language FROM {i18n_block_language}');
foreach ($result as $record) {
$block_languages[$record->module][$record->delta][$record->language] = TRUE;
}
}
if (isset($block_languages[$module][$delta]) && !isset($block_languages[$module][$delta][$language->language])) {
return;
}
// Translate.
$i18n_block = db_query("SELECT * FROM {block} WHERE module = :module AND delta = :delta", array(
':module' => $module,
':delta' => $delta,
))
->fetchObject();
if (!empty($i18n_block->i18n_mode)) {
i18n_block_block_view_alter($block, $i18n_block);
// OH WTF ...
if (!empty($i18n_block->title)) {
$block['subject'] = $i18n_block->title;
}
}
}
$block = (object) $block;
switch ($field['properties']['block_render']) {
case DS_BLOCK_TEMPLATE:
if (!isset($block->subject)) {
$block->subject = NULL;
}
$block->region = NULL;
$block->module = $module;
$block->delta = $delta;
$elements = array(
'elements' => array(
'#block' => $block,
'#children' => $block->content,
),
);
// Add contextual links
if (module_exists('contextual') && user_access('access contextual links')) {
$elements['elements'] += array(
'#contextual_links' => array(
'block' => array(
'admin/structure/block/manage',
array(
$block->module,
$block->delta,
),
),
),
);
}
return theme('block', $elements);
break;
case DS_BLOCK_TITLE_CONTENT:
return '<h2 class="block-title">' . $block->subject . '</h2>' . $block->content;
break;
case DS_BLOCK_CONTENT:
return $block->content;
break;
}
}
}
/**
* Render a field.
*/
function ds_render_field($field) {
$output = '';
$settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : array();
$settings += $field['properties']['default'];
// Basic string.
if (isset($settings['link text'])) {
$output = t($settings['link text']);
}
elseif (isset($field['properties']['entity_render_key']) && isset($field['entity']->{$field['properties']['entity_render_key']})) {
if ($field['entity_type'] == 'user' && $field['properties']['entity_render_key'] == 'name') {
$output = format_username($field['entity']);
}
else {
$output = $field['entity']->{$field['properties']['entity_render_key']};
}
}
if (empty($output)) {
return;
}
// Link.
if ($settings['link']) {
if (isset($field['entity']->uri['path'])) {
$path = $field['entity']->uri['path'];
}
else {
$uri_info = entity_uri($field['entity_type'], $field['entity']);
$path = $uri_info['path'];
}
$output = l($output, $path);
}
else {
$output = check_plain($output);
}
// Wrapper and class.
if (!empty($settings['wrapper'])) {
$wrapper = check_plain($settings['wrapper']);
$class = !empty($settings['class']) ? ' class="' . check_plain($settings['class']) . '"' : '';
$output = '<' . $wrapper . $class . '>' . $output . '</' . $wrapper . '>';
}
return $output;
}
/**
* Render an author field.
*/
function ds_render_author_field($field) {
// Users without a user name are anonymous users. These are never linked.
if (empty($field['entity']->name)) {
return check_plain(variable_get('anonymous', t('Anonymous')));
}
if ($field['formatter'] == 'author') {
return check_plain($field['entity']->name);
}
if ($field['formatter'] == 'author_linked') {
return theme('username', array(
'account' => $field['entity'],
));
}
}
/**
* Render a markup field.
*/
function ds_render_markup($field) {
if (isset($field['entity']->{$field['properties']['key']})) {
// Check for format, and let's take filtered_html as a sane default.
$format = isset($field['entity']->{$field['properties']['format']}) ? $field['entity']->{$field['properties']['format']} : 'filtered_html';
return check_markup($field['entity']->{$field['properties']['key']}, $format, '', TRUE);
}
}
/**
* Return the picture.
*/
function ds_return_picture($entity) {
// Gravatar support.
if (module_exists('gravatar')) {
$entity = _gravatar_load_account($entity);
$entity->picture = _gravatar_get_account_user_picture($entity);
}
if (!empty($entity->picture)) {
if (is_numeric($entity->picture)) {
return file_load($entity->picture);
}
else {
return $entity->picture;
}
}
elseif (variable_get('user_picture_default', '')) {
return variable_get('user_picture_default', '');
}
}
/**
* Render a user picture.
*/
function ds_render_user_picture($field) {
$picture = ds_return_picture($field['entity']);
if (!empty($picture)) {
$vars = array();
$filepath = isset($picture->uri) ? $picture->uri : $picture;
$name = !empty($field['entity']->name) ? $field['entity']->name : variable_get('anonymous', t('Anonymous'));
$alt = t("@user's picture", array(
'@user' => $name,
));
$vars = array(
'path' => $filepath,
'alt' => $alt,
'title' => $alt,
);
// If the image does not have a valid Drupal scheme (for eg. HTTP),
// don't load image styles.
if (module_exists('image') && file_valid_uri($filepath)) {
$vars['style_name'] = str_replace('ds_picture_', '', $field['formatter']);
$image = theme('image_style', $vars);
}
else {
$image = theme('image', $vars);
}
if (!empty($field['entity']->uid) && user_access('access user profiles')) {
return l($image, 'user/' . $field['entity']->uid, array(
'html' => TRUE,
));
}
else {
return $image;
}
}
}
/**
* Render a date field.
*/
function ds_render_date_field($field) {
$date_format = str_replace('ds_post_date_', '', $field['formatter']);
return format_date($field['entity']->created, $date_format);
}
/**
* Implements hook_field_formatter_info().
*/
function ds_field_formatter_info() {
$formatters = array();
// Add view modes of taxonomy to term formatters.
if (module_exists('taxonomy')) {
$view_modes = ds_entity_view_modes('taxonomy_term');
foreach ($view_modes as $key => $view_mode) {
$formatters['taxonomy_' . $key] = array(
'label' => t('View mode: ' . $view_mode['label']),
'field types' => array(
'taxonomy_term_reference',
),
);
}
}
return $formatters;
}
/**
* Implements hook_field_formatter_view().
*/
function ds_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$view_mode = substr($display['type'], 0, 9) == 'taxonomy_' ? substr($display['type'], 9) : $display['type'];
foreach ($items as $delta => $item) {
if ($item['tid'] == 'autocreate') {
// We don't necessarily have a link when this is
// an autocreated term, usually in preview.
// So just send the term as check plained markup.
$build['#markup'] = check_plain($item['name']);
}
else {
$build = taxonomy_term_view(taxonomy_term_load($item['tid']), $view_mode, $langcode);
}
$element[$delta] = $build;
}
return $element;
}
Functions
Constants
Name | Description |
---|---|
DS_BLOCK_CONTENT | |
DS_BLOCK_TEMPLATE | Constants for block fields rendering. |
DS_BLOCK_TITLE_CONTENT | |
DS_FIELD_TYPE_BLOCK | |
DS_FIELD_TYPE_CODE | |
DS_FIELD_TYPE_CTOOLS | |
DS_FIELD_TYPE_FUNCTION | |
DS_FIELD_TYPE_IGNORE | |
DS_FIELD_TYPE_PREPROCESS | |
DS_FIELD_TYPE_THEME | Constants for field types. |