View source
<?php
define('FIELDBLOCK_STORAGE_STATIC', 'fieldblock_storage');
function fieldblock_module_implements_alter(&$implementations, $hook) {
$view_alter_hooks = array(
'entity_view_alter',
'node_view_alter',
'taxonomy_term_alter',
'user_view_alter',
);
if (in_array($hook, $view_alter_hooks) && isset($implementations['fieldblock'])) {
$group = $implementations['fieldblock'];
unset($implementations['fieldblock']);
$implementations['fieldblock'] = $group;
}
}
function fieldblock_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
$view_mode = $form['#view_mode'];
$variable_name = 'fieldblock-' . $entity_type . '-' . $bundle . '-' . $view_mode;
$settings = variable_get($variable_name, array());
$form['fields']['#header'][] = t('Display as block');
$field_names = array_merge($form['#fields'], $form['#extra']);
foreach ($field_names as $field_name) {
$form['fields'][$field_name]['fieldblock'] = array(
'#type' => 'checkbox',
'#default_value' => isset($settings[$field_name]) ? true : false,
'#title' => '',
);
}
$form['#submit'][] = 'fieldblock_field_display_submit';
}
function fieldblock_field_display_submit($form, &$form_state) {
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
$view_mode = $form['#view_mode'];
$variable_name = 'fieldblock-' . $entity_type . '-' . $bundle . '-' . $view_mode;
$settings = array();
foreach ($form_state['values']['fields'] as $field_name => $field) {
if (!empty($field['fieldblock'])) {
$settings[$field_name] = $form['fields'][$field_name]['human_name']['#markup'];
}
}
if (empty($settings)) {
variable_del($variable_name);
}
else {
variable_set($variable_name, $settings);
drupal_set_message(t('One or more fields have been made available as block. Do not forget to assign the block(s) to a region.'));
}
}
function fieldblock_block_info() {
$blocks = array();
$fieldblocks = fieldblock_get_block_list();
foreach ($fieldblocks as $fieldblock_id => $description) {
$blocks[$fieldblock_id] = array(
'info' => $description,
'cache' => DRUPAL_NO_CACHE,
);
}
return $blocks;
}
function fieldblock_get_block_list() {
$fieldblocks =& drupal_static(__FUNCTION__);
if (!isset($fieldblocks)) {
$fieldblocks = array();
$entities = entity_get_info();
foreach ($entities as $entity_type => $entity_info) {
foreach ($entity_info['bundles'] as $bundle => $bundle_info) {
$view_modes = field_view_mode_settings($entity_type, $bundle);
$view_modes['default']['custom_settings'] = true;
foreach ($view_modes as $view_mode => $view_mode_info) {
if (!$view_mode_info['custom_settings']) {
continue;
}
$variable_name = 'fieldblock-' . $entity_type . '-' . $bundle . '-' . $view_mode;
$fieldblock_settings = variable_get($variable_name, array());
foreach ($fieldblock_settings as $field_name => $field_label) {
$fieldblock_id = md5($variable_name . '-' . $field_name);
$fieldblocks[$fieldblock_id] = t('@field field (from @type: @bundle: @view_mode)', array(
'@field' => $field_label,
'@type' => $entity_type,
'@bundle' => $bundle_info['label'],
'@view_mode' => $view_mode,
));
}
}
}
}
}
return $fieldblocks;
}
function fieldblock_block_view($delta = '') {
$block = array();
$fieldblocks_storage =& drupal_static(FIELDBLOCK_STORAGE_STATIC);
if (isset($fieldblocks_storage[$delta])) {
$block['subject'] = '';
if (isset($fieldblocks_storage[$delta]['#label_display']) && $fieldblocks_storage[$delta]['#label_display'] != 'hidden') {
$block['subject'] = $fieldblocks_storage[$delta]['#title'];
$fieldblocks_storage[$delta]['#label_display'] = 'hidden';
}
$block['content'] = $fieldblocks_storage[$delta];
$block['fieldblock_name'] = $fieldblocks_storage[$delta]['fieldblock_name'];
}
return $block;
}
function fieldblock_entity_view_alter(&$build, $type) {
if (!isset($build['#bundle'], $build['#entity_type'], $build['#view_mode'])) {
return;
}
$entity_type = $build['#entity_type'];
$bundle = $build['#bundle'];
$view_mode = $build['#view_mode'];
$view_mode_settings = field_view_mode_settings($entity_type, $bundle);
$actual_mode = !empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default';
$fieldblocks_storage =& drupal_static(FIELDBLOCK_STORAGE_STATIC);
$variable_name = 'fieldblock-' . $entity_type . '-' . $bundle . '-' . $actual_mode;
$fieldblock_settings = variable_get($variable_name, array());
foreach ($fieldblock_settings as $field_name => $field_label) {
if (isset($build[$field_name])) {
$fieldblock_name = $variable_name . '-' . $field_name;
$fieldblock_id = md5($fieldblock_name);
$fieldblocks_storage[$fieldblock_id] = $build[$field_name];
hide($build[$field_name]);
$fieldblocks_storage[$fieldblock_id]['fieldblock_name'] = $fieldblock_name;
}
}
}
function fieldblock_preprocess_block(&$variables) {
if ($variables['block']->module == 'fieldblock') {
$variables['block_html_id'] = drupal_html_id('block-' . $variables['block']->fieldblock_name);
}
}