6.x-1.x branch
Defines a field type for referencing a block from a node.
| Name | Description |
|---|---|
| blockreference_allowed_values | Implementation of hook_allowed_values(). |
| blockreference_autocomplete | Retrieve a pipe delimited string of autocomplete suggestions |
| blockreference_autocomplete_process | Process an individual element. |
| blockreference_autocomplete_validate | Validate an autocomplete element. |
| blockreference_autocomplete_value | Value for a blockreference autocomplete element. |
| blockreference_content_is_empty | Implementation of hook_content_is_empty(). |
| blockreference_elements | Implementation of FAPI hook_elements(). |
| blockreference_field | Implementation of hook_field(). |
| blockreference_field_formatter_info | Implementation of hook_field_formatter_info(). |
| blockreference_field_info | Implementation of hook_field_info(). |
| blockreference_field_settings | Implementation of hook_field_settings(). |
| blockreference_get_block_modules | Get an array of block modules, where the keys are the module short name and the values are the module name as set in the .info file. |
| blockreference_menu | Implementation of hook_menu(). |
| blockreference_select_process | Process an individual element. |
| blockreference_select_sort_process | Process an individual element. |
| blockreference_select_validate | Validate an select element. |
| blockreference_theme | Implementation of hook_theme(). |
| blockreference_widget | Implementation of hook_widget(). |
| blockreference_widget_info | Implementation of hook_widget_info(). |
| theme_blockreference_autocomplete | |
| theme_blockreference_formatter_default | Theme function for 'default' blockreference field formatter. |
| theme_blockreference_formatter_plain | Theme function for 'plain' blockreference field formatter. |
| theme_blockreference_formatter_title | Theme function for 'title' blockreference field formatter. |
| theme_blockreference_formatter_without_title | Theme function for 'without_title' blockreference field formatter. |
| theme_blockreference_item_advanced | |
| theme_blockreference_item_simple | |
| theme_blockreference_select | FAPI theme for an individual elements. |
| theme_blockreference_select_sort | |
| _blockreference_item | |
| _blockreference_potential_references | Fetch an array of all candidate referenced blocks, for use in presenting the selection form to the user. |
| _theme_blockreference_formatter_get_block | Helper function for theming normal block views, returns appropriate block. |
- <?php
-
- /**
- * @file
- * Defines a field type for referencing a block from a node.
- */
-
- /**
- * Implementation of hook_menu().
- */
- function blockreference_menu() {
- $items = array();
- $items['blockreference/autocomplete'] = array(
- 'title' => 'Blockreference autocomplete',
- 'page callback' => 'blockreference_autocomplete',
- 'access arguments' => array('access content'),
- 'type' => MENU_CALLBACK
- );
- return $items;
- }
-
- /**
- * Implementation of hook_theme().
- */
- function blockreference_theme() {
- return array(
- 'blockreference_item_simple' => array(
- 'arguments' => array('item' => NULL),
- ),
- 'blockreference_item_advanced' => array(
- 'arguments' => array('item' => NULL, 'view' => NULL),
- ),
- 'blockreference_select' => array(
- 'arguments' => array('element' => NULL),
- ),
- 'blockreference_select_sort' => array(
- 'arguments' => array('element' => NULL),
- ),
- 'blockreference_autocomplete' => array(
- 'arguments' => array('element' => NULL),
- ),
- 'blockreference_formatter_default' => array(
- 'arguments' => array('element'),
- ),
- 'blockreference_formatter_without_title' => array(
- 'arguments' => array('element'),
- ),
- 'blockreference_formatter_plain' => array(
- 'arguments' => array('element'),
- ),
- 'blockreference_formatter_title' => array(
- 'arguments' => array('element'),
- ),
- );
- }
-
- /**
- * Implementation of hook_field_info().
- *
- * Here we indicate that the content module will use its default
- * handling for the view of this field.
- *
- * Callbacks can be omitted if default handing is used.
- * They're included here just so this module can be used
- * as an example for custom modules that might do things
- * differently.
- */
- function blockreference_field_info() {
- return array(
- 'blockreference' => array(
- 'label' => t('Block reference'),
- 'description' => t('Store the ID of a related block as an integer value.'),
- 'callbacks' => array(
- 'tables' => CONTENT_CALLBACK_DEFAULT,
- 'arguments' => CONTENT_CALLBACK_DEFAULT,
- ),
- ),
- );
- }
-
- /**
- * Get an array of block modules, where the keys are the module short name and
- * the values are the module name as set in the .info file.
- */
- function blockreference_get_block_modules() {
- $block_modules = array();
-
- // Get current list of modules
- $files = drupal_system_listing('\.module$', 'modules', 'name', 0);
-
- // Extract current files from database.
- system_get_files_database($files, 'module');
-
- // Get modules that define blocks.
- $modules = module_implements('block', TRUE);
-
- foreach ($modules as $module) {
- if (isset($files[$module])) {
- $file = &$files[$module];
- // Look for the info file.
- $file->info = drupal_parse_info_file(dirname($file->filename) . '/' . $file->name . '.info');
- $block_modules[$module] = isset($file->info['name']) ? $file->info['name'] : ucfirst($module);
- }
- }
- return $block_modules;
- }
-
- /**
- * Implementation of hook_field_settings().
- */
- function blockreference_field_settings($op, $field) {
-
- switch ($op) {
-
- case 'form':
- $form = array();
- $form['referenceable_regions'] = array(
- '#type' => 'checkboxes',
- '#title' => t('Regions containing blocks that can be referenced'),
- '#multiple' => TRUE,
- '#default_value' => !empty($field['referenceable_regions']) ? $field['referenceable_regions'] : array(),
- '#options' => array('' => '(Disabled)') + system_region_list(variable_get('theme_default', 'garland')),
- '#description' => t('If no regions are selected, blocks from all regions will be available.'),
- );
- $form['referenceable_modules'] = array(
- '#type' => 'checkboxes',
- '#title' => t('Modules defining blocks that can be referenced'),
- '#multiple' => TRUE,
- '#default_value' => !empty($field['referenceable_modules']) ? $field['referenceable_modules'] : array(),
- '#options' => blockreference_get_block_modules(),
- '#description' => t('If no modules are selected, blocks from all modules will be available.'),
- );
- $form['referenceable_operator'] = array(
- '#type' => 'radios',
- '#title' => t('Operator to use if referenceable regions and referenceable modules are selected.'),
- '#default_value' => !empty($field['referenceable_operator'])
- ? $field['referenceable_operator']
- : 'AND',
- '#options' => array(
- 'AND' => '<strong>AND</strong> - ' . t('Block must be both contained in a referenceable region and defined by a referenceable module.'),
- 'OR' => '<strong>OR</strong> - ' . t('Block must be either contained in a referenceable region or defined by a referenceable module.'),
- ),
- '#description' => t('If regions and modules are selected, choose operator to use when retrieving blocks list.'),
- );
- return $form;
-
- case 'save':
- $settings = array('referenceable_regions', 'referenceable_modules', 'referenceable_operator');
- return $settings;
-
- case 'database columns':
- $columns = array(
- 'bid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => FALSE),
- );
- return $columns;
-
- case 'views data':
- $data = content_views_field_views_data($field);
- $db_info = content_database_info($field);
- $table_alias = content_views_tablename($field);
-
- // Filter: swap the handler to the 'in' operator.
- $data[$table_alias][$field['field_name'] .'_bid']['filter']['handler'] = 'content_handler_filter_many_to_one';
-
- $data["node_$table_alias"]['table']['join']['node'] = array(
- 'table' => 'node',
- 'field' => 'vid',
- 'left_table' => $table_alias,
- 'left_field' => 'vid',
- );
-
- $data[$table_alias][$field['field_name'] .'_bid']['argument']['handler'] = 'content_handler_argument_reference';
- $data[$table_alias][$field['field_name'] .'_bid']['argument']['name table'] = "node_$table_alias";
- $data[$table_alias][$field['field_name'] .'_bid']['argument']['name field'] = 'title';
- // Relationship: add a relationship for related block.
- $data[$table_alias][$field['field_name'] .'_bid']['relationship'] = array(
- 'base' => 'blocks',
- 'field' => $db_info['columns']['bid']['column'],
- 'handler' => 'content_handler_relationship',
- 'label' => t($field['widget']['label']),
- 'content_field_name' => $field['field_name'],
- );
- return $data;
- }
- }
-
- /**
- * Implementation of hook_field().
- */
- function blockreference_field($op, &$node, $field, &$items, $teaser, $page) {
- switch ($op) {
- case 'validate':
- $refs = _blockreference_potential_references($field, TRUE);
- foreach ($items as $delta => $item) {
- if (is_array($item) && !empty($item['error_field'])) {
- $error_field = $item['error_field'];
- unset($item['error_field']);
- if (!empty($item['bid'])) {
- if (!in_array($item['bid'], array_keys($refs))) {
- form_set_error($error_field, t("%name : This block can't be referenced.", array('%name' => t($field['widget']['label']))));
- }
- }
- }
- }
- return $items;
- }
- }
-
- /**
- * Implementation of hook_content_is_empty().
- */
- function blockreference_content_is_empty($item, $field) {
- if (empty($item['bid'])) {
- return TRUE;
- }
- return FALSE;
- }
-
- /**
- * Implementation of hook_field_formatter_info().
- */
- function blockreference_field_formatter_info() {
- return array(
- 'default' => array(
- 'label' => t('Default'),
- 'field types' => array('blockreference'),
- 'multiple values' => CONTENT_HANDLE_CORE,
- ),
- 'without_title' => array(
- 'label' => t('Normal, without title'),
- 'field types' => array('blockreference'),
- 'multiple values' => CONTENT_HANDLE_CORE,
- ),
- 'plain' => array(
- 'label' => t('Plain (info)'),
- 'field types' => array('blockreference'),
- 'multiple values' => CONTENT_HANDLE_CORE,
- ),
- 'title' => array(
- 'label' => t('Title only'),
- 'field types' => array('blockreference'),
- 'multiple values' => CONTENT_HANDLE_CORE,
- ),
-
- );
- }
-
- /**
- * Helper function for theming normal block views, returns appropriate block.
- */
- function _theme_blockreference_formatter_get_block($element) {
- if (!empty($element['#item']['bid']) && is_numeric($element['#item']['bid'])) {
- $block = db_fetch_object(db_query(db_rewrite_sql("SELECT * FROM {blocks} WHERE bid = ". $element['#item']['bid'], 'blocks', 'bid')));
- $block->enabled = TRUE;
- $block->status = TRUE;
- $block->page_match = TRUE;
-
- // Code adapted from the block module.
- if (!count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET' && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) {
- $block_view = $cache->data;
- }
- else {
- $block_view = module_invoke($block->module, 'block', 'view', $block->delta);
- if (isset($cid)) {
- cache_set($cid, $block_view, 'cache_block', CACHE_TEMPORARY);
- }
- }
-
- $block->content = $block_view['content'];
- $block->subject = $block->title ? $block->title : $block_view['subject'];
- }
-
- return $block;
- }
-
- /**
- * Theme function for 'default' blockreference field formatter.
- */
- function theme_blockreference_formatter_default($element) {
- $block = _theme_blockreference_formatter_get_block($element);
- $output = '';
- if ($block->content) {
- $output = theme('block', $block);
- }
- return $output;
- }
-
- /**
- * Theme function for 'without_title' blockreference field formatter.
- */
- function theme_blockreference_formatter_without_title($element) {
- $block = _theme_blockreference_formatter_get_block($element);
- $block->subject = ''; // Without title
- $output = '';
- if ($block->content) {
- $output = theme('block', $block);
- }
- return $output;
- }
-
- /**
- * Theme function for 'plain' blockreference field formatter.
- */
- function theme_blockreference_formatter_plain($element) {
- $output = '';
- if (isset($element['#item']['bid'])) {
- $args[] = $element['#item']['bid'];
- $result = db_query(db_rewrite_sql("SELECT b.module, b.delta FROM {blocks} b WHERE b.bid = '%s'", 'blocks', 'bid', $args), $args);
- $block = db_fetch_object($result);
- $info = module_invoke($block->module, 'block', 'list');
- $output = $info[$block->delta]['info'];
- }
- return $output;
- }
-
- /**
- * Theme function for 'title' blockreference field formatter.
- */
- function theme_blockreference_formatter_title($element) {
- $field_name = $element['#field_name'];
- $field = content_fields($field_name);
- $output = '';
- if (!empty($element['#item']['bid']) && is_numeric($element['#item']['bid'])) {
- $block = db_fetch_object(db_query(db_rewrite_sql("SELECT * FROM {blocks} WHERE bid = ". $element['#item']['bid'], 'blocks', 'bid')));
- $block->enabled = TRUE;
- $block->status = TRUE;
- $block->page_match = TRUE;
- $block_view = module_invoke($block->module, 'block', 'view', $block->delta);
- $subject = $block->title ? $block->title : $block_view['subject'];
- if ($block_view['content']) {
- $output = check_plain($subject);
- }
- }
- return $output;
- }
-
- /**
- * Implementation of hook_widget_info().
- *
- * We need custom handling of multiple values for the blockreference_select
- * widget because we need to combine them into a options list rather
- * than display multiple elements.
- *
- * We will use the content module's default handling for default value.
- *
- * Callbacks can be omitted if default handing is used.
- * They're included here just so this module can be used
- * as an example for custom modules that might do things
- * differently.
- */
- function blockreference_widget_info() {
- return array(
- 'blockreference_select' => array(
- 'label' => t('Select list'),
- 'field types' => array('blockreference'),
- 'multiple values' => CONTENT_HANDLE_MODULE,
- 'callbacks' => array(
- 'default value' => CONTENT_CALLBACK_DEFAULT,
- ),
- ),
- 'blockreference_select_sort' => array(
- 'label' => t('Select list (with drag-and-drop sort)'),
- 'field types' => array('blockreference'),
- 'multiple values' => CONTENT_HANDLE_CORE,
- 'callbacks' => array(
- 'default value' => CONTENT_CALLBACK_DEFAULT,
- ),
- ),
- 'blockreference_autocomplete' => array(
- 'label' => t('Autocomplete text field'),
- 'field types' => array('blockreference'),
- 'multiple values' => CONTENT_HANDLE_CORE,
- 'callbacks' => array(
- 'default value' => CONTENT_CALLBACK_DEFAULT,
- ),
- ),
- );
- }
-
- /**
- * Implementation of FAPI hook_elements().
- *
- * Any FAPI callbacks needed for individual widgets can be declared here,
- * and the element will be passed to those callbacks for processing.
- *
- * Drupal will automatically theme the element using a theme with
- * the same name as the hook_elements key.
- *
- * Autocomplete_path is not used by text_widget but other widgets can use it
- * (see blockreference and userreference).
- */
- function blockreference_elements() {
- return array(
- 'blockreference_select' => array(
- '#input' => TRUE,
- '#columns' => array('uid'), '#delta' => 0,
- '#process' => array('blockreference_select_process'),
- ),
- 'blockreference_select_sort' => array(
- '#input' => TRUE,
- '#columns' => array('uid'), '#delta' => 0,
- '#process' => array('blockreference_select_sort_process'),
- ),
- 'blockreference_autocomplete' => array(
- '#input' => TRUE,
- '#columns' => array('name'), '#delta' => 0,
- '#process' => array('blockreference_autocomplete_process'),
- '#autocomplete_path' => FALSE,
- ),
- );
- }
-
- /**
- * Implementation of hook_widget().
- *
- * Attach a single form element to the form. It will be built out and
- * validated in the callback(s) listed in hook_elements. We build it
- * out in the callbacks rather than here in hook_widget so it can be
- * plugged into any module that can provide it with valid
- * $field information.
- *
- * Content module will set the weight, field name and delta values
- * for each form element. This is a change from earlier CCK versions
- * where the widget managed its own multiple values.
- *
- * If there are multiple values for this field, the content module will
- * call this function as many times as needed.
- *
- * @param $form
- * the entire form array, $form['#node'] holds node information
- * @param $form_state
- * the form_state, $form_state['values'][$field['field_name']]
- * holds the field's form values.
- * @param $field
- * the field array
- * @param $items
- * array of default values for this field
- * @param $delta
- * the order of this item in the array of subelements (0, 1, 2, etc)
- *
- * @return
- * the form item for a single element for this field
- */
- function blockreference_widget(&$form, &$form_state, $field, $items, $delta = 0) {
- switch ($field['widget']['type']) {
- case 'blockreference_select':
- $element = array(
- '#type' => 'blockreference_select',
- '#default_value' => $items,
- );
- break;
- case 'blockreference_select_sort':
- $element = array(
- '#type' => 'blockreference_select_sort',
- '#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
- );
- break;
- case 'blockreference_autocomplete':
- $element = array(
- '#type' => 'blockreference_autocomplete',
- '#default_value' => isset($items[$delta]) ? $items[$delta] : NULL,
- '#value_callback' => 'blockreference_autocomplete_value',
- );
- break;
- }
- return $element;
- }
-
- /**
- * Value for a blockreference autocomplete element.
- *
- * Substitute in the block title for the block bid.
- */
- function blockreference_autocomplete_value($element, $edit = FALSE) {
- $field_key = $element['#columns'][0];
- if (!empty($element['#default_value'][$field_key])) {
- $bid = $element['#default_value'][$field_key];
- $args[] = $bid;
- $result = db_query(db_rewrite_sql("SELECT b.module, b.delta FROM {blocks} b WHERE b.bid = '%d'", 'blocks', 'bid', $args), $args);
- $block = db_fetch_object($result);
- $info = module_invoke($block->module, 'block', 'list');
- $value = $info[$block->delta]['info'];
- $value .= ' [bid:'. $bid .']';
- return array($field_key => $value);
- }
- return array($field_key => NULL);
- }
-
- /**
- * Process an individual element.
- *
- * Build the form element. When creating a form using FAPI #process,
- * note that $element['#value'] is already set.
- *
- * The $fields array is in $form['#field_info'][$element['#field_name']].
- */
- function blockreference_select_process($element, $edit, $form_state, $form) {
- // The blockreference_select widget doesn't need to create its own
- // element, it can wrap around the optionwidgets_select element.
- // Add a validation step where the value can be unwrapped.
- $field_key = $element['#columns'][0];
- $element[$field_key] = array(
- '#type' => 'optionwidgets_select',
- '#default_value' => isset($element['#value']) ? $element['#value'] : '',
-
- // The following values were set by the content module and need
- // to be passed down to the nested element.
- '#field_name' => $element['#field_name'],
- '#delta' => $element['#delta'],
- '#columns' => $element['#columns'],
- '#title' => $element['#title'],
- '#required' => $element['#required'],
- '#description' => $element['#description'],
- );
- if (empty($element[$field_key]['#element_validate'])) {
- $element[$field_key]['#element_validate'] = array();
- }
- array_unshift($element[$field_key]['#element_validate'], 'optionwidgets_validate', 'blockreference_select_validate');
-
- return $element;
- }
-
- /**
- * Process an individual element.
- *
- * Build the form element. When creating a form using FAPI #process,
- * note that $element['#value'] is already set.
- *
- * The $fields array is in $form['#field_info'][$element['#field_name']].
- */
- function blockreference_select_sort_process($element, $edit, $form_state, $form) {
- $field = $form['#field_info'][$element['#field_name']];
- $field_key = $element['#columns'][0];
- $element[$field_key] = array(
- '#type' => 'select',
- '#options' => blockreference_allowed_values($field),
- '#multiple' => 0,
- '#default_value' => isset($element['#value'][$element['#columns'][0]]) ? $element['#value'][$element['#columns'][0]] : '',
-
- // The following values were set by the content module and need
- // to be passed down to the nested element.
- '#field_name' => $element['#field_name'],
- '#delta' => $element['#delta'],
- '#columns' => $element['#columns'],
- '#title' => $element['#title'],
- '#required' => $element['#required'],
- '#description' => $element['#description'],
- );
- return $element;
- }
-
- /**
- * Process an individual element.
- *
- * Build the form element. When creating a form using FAPI #process,
- * note that $element['#value'] is already set.
- *
- */
- function blockreference_autocomplete_process($element, $edit, $form_state, $form) {
- // The blockreference autocomplete widget doesn't need to create its own
- // element, it can wrap around the text_textfield element and add an autocomplete
- // path and some extra processing to it.
- // Add a validation step where the value can be unwrapped.
- $field_key = $element['#columns'][0];
-
- $element[$field_key] = array(
- '#type' => 'text_textfield',
- '#default_value' => isset($element['#value']) ? $element['#value'] : '',
- '#autocomplete_path' => 'blockreference/autocomplete/'. $element['#field_name'],
-
- // The following values were set by the content module and need
- // to be passed down to the nested element.
- '#field_name' => $element['#field_name'],
- '#delta' => $element['#delta'],
- '#columns' => $element['#columns'],
- '#title' => $element['#title'],
- '#required' => $element['#required'],
- '#description' => $element['#description'],
- );
- if (empty($element[$field_key]['#element_validate'])) {
- $element[$field_key]['#element_validate'] = array();
- }
- array_unshift($element[$field_key]['#element_validate'], 'blockreference_autocomplete_validate');
-
- return $element;
- }
-
- /**
- * Validate an select element.
- *
- * Remove the wrapper layer and set the right element's value.
- * We don't know exactly where this element is, so we drill down
- * through the element until we get to our key.
- */
- function blockreference_select_validate($element, &$form_state) {
- $field_key = $element['#columns'][0];
- $new_parents = array();
- $value = $form_state['values'];
- foreach ($element['#parents'] as $parent) {
- $value = $value[$parent];
- if ($parent == $field_key) {
- $element['#parents'] = $new_parents;
- form_set_value($element, $value, $form_state);
- break;
- }
- $new_parents[] = $parent;
- }
- }
-
- /**
- * Validate an autocomplete element.
- *
- * Remove the wrapper layer and set the right element's value.
- */
- function blockreference_autocomplete_validate($element, &$form_state) {
- $field_name = $element['#field_name'];
- $field = content_fields($field_name);
- $field_key = $element['#columns'][0];
- $delta = $element['#delta'];
- $value = $element['#value'][$field_key];
- $bid = NULL;
- if (!empty($value)) {
- preg_match('/^(?:\s*|(.*) )?\[\s*bid\s*:\s*(\d+)\s*\]$/', $value, $matches);
- if (!empty($matches)) {
- // explicit bid
- list(, $info, $bid) = $matches;
-
- $args[] = $bid;
- $result = db_query(db_rewrite_sql("SELECT b.module, b.delta FROM {blocks} b WHERE b.bid = '%s'", 'blocks', 'bid', $args), $args);
- $block = db_fetch_object($result);
- $info = module_invoke($block->module, 'block', 'list');
- $block->info = $info[$block->delta]['info'];
-
- if (!empty($title) && ($b = db_fetch_object($result)) && $info != $b->info) {
- form_set_error($element[$field_key], t('%name: Title mismatch. Please check your selection.'), array('%name' => t($element[$field_key]['#title'])));
- }
- }
- else {
- // no explicit bid
- // TODO :
- // the best thing would be to present the user with an additional form,
- // allowing the user to choose between valid candidates with the same title
- // ATM, we pick the first matching candidate...
- $bids = _blockreference_potential_references($field, FALSE, $value, TRUE);
- $bid = (!empty($bids)) ? array_shift(array_keys($bids)) : 0;
- }
- }
- form_set_value($element, $bid, $form_state);
- return $element;
- }
-
- /**
- * Implementation of hook_allowed_values().
- */
- function blockreference_allowed_values($field) {
- $options = _blockreference_potential_references($field, TRUE);
-
- foreach ($options as $key => $value) {
- $options[$key] = _blockreference_item($field, $value);
- }
-
- natcasesort($options);
-
- if (!$field['required']) {
- $options = array(0 => ' - '. t('none') .' - ') + $options;
- }
- return $options;
- }
-
- /**
- * Fetch an array of all candidate referenced blocks,
- * for use in presenting the selection form to the user.
- *
- * @param $field
- * Array containing field data.
- * @param $return_full_blocks
- * Whether to return full blocks.
- * @param $string
- * Filter string to match blocks.
- * @param $exact_string
- * Strictly match string like for validation.
- *
- */
- function _blockreference_potential_references($field, $return_full_blocks = FALSE, $string = '', $exact_string = FALSE) {
- static $block_info = array();
- $related_regions = array();
- $related_modules = array();
- $related_clauses = array();
- $args = array();
-
- // Handle related regions - this will be used in extra query conditions.
- if (isset($field['referenceable_regions']) && is_array($field['referenceable_regions'])) {
- foreach ($field['referenceable_regions'] as $related_region) {
- if ($related_region !== 0 && $related_region != '0') {
- if (isset($related_region)) {
- $related_regions[] = " b.region = '%s'";
- $args[] = $related_region;
- }
- }
- }
- }
- if (!empty($related_regions)) {
- $related_clauses[] = implode(' OR ', $related_regions);
- }
-
- // Handle related modules - this will be used in extra query conditions.
- if (isset($field['referenceable_modules']) && is_array($field['referenceable_modules'])) {
- foreach ($field['referenceable_modules'] as $related_module) {
- if ($related_module !== 0 && $related_module != '0') {
- $related_modules[] = " b.module = '%s'";
- $args[] = $related_module;
- }
- }
- }
- if (!empty($related_modules)) {
- $related_clauses[] = implode(' OR ', $related_modules);
- }
-
- // Assemble the extra query condition.
- $related_operator = !empty($field['referenceable_operator']) ? $field['referenceable_operator'] : 'AND';
- $related_clause = !empty($related_clauses) ? ' AND (('. implode(') ' . $related_operator . ' (', $related_clauses) .'))' : '';
-
- // Assemble the query.
- $result = db_query(
- 'SELECT b.bid, b.module, b.delta, b.title, b.region '.
- 'FROM {blocks} b '.
- "WHERE b.theme = '". variable_get('theme_default', 'garland') ."'".
- $related_clause .' '.
- 'ORDER BY b.region, b.weight',
- $args
- );
-
- // Execute the query, test each row, and return the blocks or block info.
- $rows = array();
- while ($block = db_fetch_object($result)) {
- if (!isset($block_info[$block->module][$block->delta])) {
- $block_info[$block->module] = module_invoke($block->module, 'block', 'list');
- }
- $block->info = $block_info[$block->module][$block->delta]['info'];
- if (
- (
- $exact_string &&
- !empty($string) &&
- $string == $block->info
- ) ||
- (
- !empty($string) &&
- (
- (!empty($block->info) && stripos($block->info, $string) !== FALSE) ||
- (!empty($block->title) && stripos($block->title, $string) !== FALSE) ||
- (!empty($block->module) && stripos($block->module . ' ' . $block->delta, $string) !== FALSE) ||
- (!empty($block->bid) && stripos($block->bid, $string) !== FALSE)
- )
- ) ||
- (
- empty($string)
- )
- ) {
- $rows[$block->bid] = $return_full_blocks ? $block : $block->info;
- }
- }
- drupal_alter('blockreference_potential_references', $rows, $field, $return_full_blocks, $string, $exact_string);
- return $rows;
- }
-
- /**
- * Retrieve a pipe delimited string of autocomplete suggestions
- */
- function blockreference_autocomplete($field_name, $string = '') {
- // If the request has a '/' in the search text, then the menu system will have
- // split it into multiple arguments, recover the intended $string.
- $args = func_get_args();
- // Shift off the $field_name argument.
- array_shift($args);
- $string = implode('/', $args);
-
- $fields = content_fields();
- $field = $fields[$field_name];
- $matches = array();
- $references = _blockreference_potential_references($field, TRUE, $string);
- foreach ($references as $rowbid => $rowname) {
- $matches[_blockreference_item($field, $rowname) .' [bid:'. $rowbid .']'] = _blockreference_item($field, $rowname);
- }
- drupal_json($matches);
- }
-
- function _blockreference_item($field, $item, $html = FALSE) {
- $output = theme('blockreference_item_simple', $item);
- $output = $html ? check_plain($output) : $output;
- return $output;
- }
-
- function theme_blockreference_item_advanced($item, $field_names, $view) {
- $item_fields = array();
- $item = (array) $item;
- foreach ($item as $delta => $value) {
- // remove link tags (ex : for block titles)
- $value = preg_replace('/<a[^>]*>(.*)<\/a>/iU', '$1', $value);
- if (!empty($value)) {
- $item_fields[] = "<span class='view-field view-data-$field_names[$delta]'>$value</span>";;
- }
- }
- $output = implode(' - ', $item_fields);
- $output = "<span class='view-item view-item-$view->name'>$output</span>";
- return $output;
- }
-
- function theme_blockreference_item_simple($item) {
- return $item->info;
- }
-
- /**
- * FAPI theme for an individual elements.
- *
- * The textfield or select is already rendered by the
- * textfield or select themes and the html output
- * lives in $element['#children']. Override this theme to
- * make custom changes to the output.
- *
- * $element['#field_name'] contains the field name
- * $element['#delta] is the position of this element in the group
- */
- function theme_blockreference_select($element) {
- return $element['#children'];
- }
-
- function theme_blockreference_select_sort($element) {
- return $element['#children'];
- }
-
- function theme_blockreference_autocomplete($element) {
- return $element['#children'];
- }
-