array( 'label' => t('CCK Blocks'), 'custom settings' => TRUE, ), ); } /** * Implements hook_field_ui_view_modes_tabs(). */ function cck_blocks_field_ui_view_modes_tabs() { return array( 'cck_blocks' => array( 'title' => t('CCK Blocks'), 'view modes' => array('cck_blocks'), 'custom settings' => TRUE, ) ); } /** * Implements hook_block_info(). */ function cck_blocks_block_info(){ $blocks = array(); $fields = field_info_fields(); foreach($fields as $field_name => $field_info) { if (variable_get('cck_blocks_' . $field_name . '_block_availability', CCK_BLOCKS_FIELD_BLOCK_DISABLED) == CCK_BLOCKS_FIELD_BLOCK_ENABLED) { $blocks[$field_name] = array( 'info' => t('Field: @field ', array('@field' => $field_name)), 'cache' => DRUPAL_NO_CACHE, ); } } return $blocks; } /** * Implements hook_block_configure(). */ function cck_blocks_block_configure(){ $form = array(); // Provide information about available tokens, if the token ui module is installed. if(module_exists('token')){ $token_tree = array( '#theme' => 'token_tree', '#token_types' => array('node'), // Specific token types to include. '#global_types' => TRUE, // Whether or not to include global token types like current-user, date, etc. '#click_insert' => TRUE, // Make tokens clickable & insert into last focused textfield '#recursion_limit' => 2, // Since tokens like comment and taxonomy terms can recurse infinitely, we have to set some kind of limit. ); $form['view']['token_help'] = array( '#type' => 'fieldset', '#title' => t('Replacement patterns'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#description' => t('You may use the following replacement patterns in the block title.') . theme('token_tree', $token_tree), ); } return $form; } /** * Implements hook_block_view(). */ function cck_blocks_block_view($delta = '') { static $built_nodes = NULL; $fields = field_info_fields(); $block = array(); // Get the currently viewed node. $node = menu_get_object(); /* Determine whether the node's content is being displayed to the user. * It is, when any revision is displayed, inlcuding the latest one. * For example, the node's content is not visible to the user when he is * selecting revisions for comparison or when he is editing the node. */ $display_nodecontent = (!arg(2) || arg(2) == 'revisions' && is_numeric(arg(3))); /* Only create a block, when a node is loaded, the node's content is displayed * to the user and the requested field is available in the fields array */ if (isset($node->nid) && $display_nodecontent && $fields[$delta]) { $nid = $node->nid; // build the node in cck_blocks mode if that hasn't been done yet if (!isset($built_nodes[$nid])) { node_build_content($node, 'cck_blocks'); $built_nodes[$nid] = $node; } else { $node = $built_nodes[$nid]; } // look directly for the cck_field in the content array $field_data = false; if (isset($node->content[$delta])) { $field_data = $node->content[$delta]; } else { // cycle through all content data arrays looking for cck groups // the cck_field may be within a group foreach ($node->content as $key => $data) { if (is_array($data) && (strpos($key, 'group_') == 0) && isset($data['group'][$delta])) { $field_data = $data['group'][$delta]; } } } if ($field_data) { $block_content = drupal_render($field_data); if (trim($block_content) != '') { // Evaluate tokens in a user-defined title, if token module is installed global $user; $data = array( 'node' => $node, 'user' => $user, ); $title = db_query("SELECT title FROM {block} WHERE delta = :delta", array(':delta' => $delta))->fetchField(); if ($title){ $block['title'] = token_replace($title, $data); } /* Use the label of the field as the block's title. Only visible, * if the user did not enter a custom title for the block as $block->subject * is replaced by $block->title (if set) in block_list(). */ $block['subject'] = t($field_data['#title']); // Set the field's data as the content of the block $block['content'] = $block_content; } } } return $block; } /** * Implements hook_form_FORM_ID_alter(). * * Adds options to the field configuration page for making the field available as a * block for this specific content type or for every content type that uses it. */ function cck_blocks_form_field_ui_field_edit_form_alter(&$form, $form_state) { // Global settings form $form['field']['global_block_settings'] = array( '#type' => 'radios', '#title' => t('Provide block for this field'), '#default_value' => variable_get('cck_blocks_' . $form['#field']['field_name'] . '_block_availability', CCK_BLOCKS_FIELD_BLOCK_DISABLED), '#description' => t('When enabled, this field becomes available as a block in the block admin page. Overridden by content-type-specific settings'), '#options' => array( CCK_BLOCKS_FIELD_BLOCK_DISABLED => t('Disabled'), CCK_BLOCKS_FIELD_BLOCK_ENABLED => t('Enabled'), ), ); // Add custom form handler to the submit function $form['#submit'][] = 'cck_blocks_field_settings_submit'; } function cck_blocks_field_settings_submit($form, &$form_state) { if ($form_state['values']['field']['global_block_settings'] == CCK_BLOCKS_FIELD_BLOCK_ENABLED) { variable_set('cck_blocks_' . $form['#field']['field_name'] . '_block_availability', CCK_BLOCKS_FIELD_BLOCK_ENABLED); } else { variable_del('cck_blocks_' . $form['#field']['field_name'] . '_block_availability'); } }