You are here

popup_block.module in Popup 7

File

modules/popup_block/popup_block.module
View source
<?php

/**
 * Implementation of hook_form_alter
 * Adds UI settings to block configuration
 */
function popup_block_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'block_admin_configure' && $form['module']['#value'] != 'popup_menu') {
    module_load_include('inc', 'popup_ui', 'includes/popup_ui.admin');
    $settings = _popup_block_settings();
    $block_settings = isset($settings[$form['module']['#value'] . ':' . $form['delta']['#value']]) ? $settings[$form['module']['#value'] . ':' . $form['delta']['#value']] : FALSE;
    $format_options = array_keys(_popup_ui_formatter_settings());
    array_unshift($format_options, 'Default');
    $display_format_options = array_combine($format_options, $format_options);
    $length_options = range(10, 200, 10);
    $length_options = array_combine($length_options, $length_options);
    $form['display_title'] = array(
      '#type' => 'item',
      '#title' => 'Display settings',
      '#weight' => 1,
    );
    $form['regions'] = array(
      '#type' => 'vertical_tabs',
      '#weight' => 2,
      '#attached' => array(
        'js' => array(
          'modules/block/block.js',
        ),
      ),
      'regions' => $form['regions'],
      'popup' => array(
        '#type' => 'fieldset',
        '#title' => t('Popup settings'),
        '#collapsible' => 0,
        '#group' => 'Display',
        'popup-block' => array(
          '#default_value' => $block_settings['active'],
          '#description' => t('This will render the block with the body initially invisible. When a user hovers over or clicks on the title, the body is displayed.'),
          '#title' => t('Display this block as a popup'),
          '#type' => 'checkbox',
        ),
        'popup-block-format' => array(
          '#default_value' => isset($block_settings['format']) ? $block_settings['format'] : '',
          '#title' => 'Display format',
          '#type' => 'select',
          '#options' => $display_format_options,
          '#description' => t('Select the format in which to display popups. You may manage popup formats !here.', array(
            '!here' => l('here', 'admin/config/user-interface/popup/formats'),
          )),
        ),
        'popup-title-length' => array(
          '#default_value' => isset($block_settings['title-length']) ? $block_settings['title-length'] : 10,
          '#title' => 'Title length',
          '#type' => 'select',
          '#options' => $length_options,
          '#description' => t('Select the length of text to use from the body if no title is present.', array(
            '!here' => l('here', 'admin/config/user-interface/popup/formats'),
          )),
        ),
      ),
    );
    $form['settings']['#weight'] = 0;
    $form['visibility_title']['#weight'] = 3;
    $form['visibility']['#weight'] = 4;
    $form['submit']['#weight'] = 10;
    $form['#submit'][] = 'popup_block_form_submit';
  }
}

/**
 *  Implementation of hook_features_api
 */
function popup_block_features_api() {
  return array(
    'popupblock' => array(
      'name' => t('Popup blocks'),
      'default_hook' => 'popupblock_defaults',
      'default_file' => FEATURES_DEFAULTS_INCLUDED_COMMON,
      'file' => drupal_get_path('module', 'popup_block') . '/includes/popup_block.features.inc',
    ),
  );
}

/* ---- Themeing hooks ---- */
function popup_block_preprocess_block(&$variables) {
  module_load_include('inc', 'popup', 'includes/popup.api');
  $settings = _popup_block_settings();
  $block =& $variables['block'];
  $block_settings = isset($settings[$block->module . ':' . $block->delta]) ? $settings[$block->module . ':' . $block->delta] : FALSE;
  $title_length = isset($block_settings['title-length']) ? $block_settings['title-length'] : 10;
  if ($block_settings && $block_settings['active']) {
    $body = $variables['content'];
    $title_candidates = array_filter(array(
      $variables['elements']['#block']->subject,
      substr(strip_tags($body), 0, $title_length) . '...',
      isset($variables['elements'][1]['title']) ? $variables['elements'][1]['title'] : 0,
    ));
    $title = array_shift($title_candidates);
    $attributes = array(
      'block' => TRUE,
      'delta' => $block->delta,
      'format' => $block_settings['format'],
      'module' => $block->module,
      'title' => $title,
      'title-length' => $title_length,
    );

    // Integration with block_titlelink module
    if (!empty($variables['block']->title_link)) {
      $attributes['path'] = $variables['block']->title_link;
      $variables['block']->subject = strip_tags($block->subject);
    }
    drupal_alter('popup_attributes', $attributes);
    $attributes['altered'] = TRUE;
    $variables['content'] = popup_element($attributes['title'], $body, $attributes);
    $variables['block']->subject = '';
  }
}

/* ---- Forms ---- */
function popup_block_form_submit(&$form, &$form_state) {
  $settings = _popup_block_settings();
  $values = $form_state['values'];
  if (isset($values['popup-block'])) {
    $settings[$values['module'] . ':' . $values['delta']] = array(
      'active' => $values['popup-block'],
      'format' => $values['popup-block-format'],
      'title-length' => $values['popup-title-length'],
    );
    if (isset($settings['active']) && $settings['active'] && $form_state['values']['title'] == '<none>') {
      $form_state['values']['title'] = '';
    }
    _popup_block_settings($settings);
  }
}

/* ---- Settings ---- */
function _popup_block_settings($new_settings = FALSE) {
  static $settings = FALSE;
  if ($new_settings) {
    $settings = $new_settings;
    variable_set('popup-block-settings', $settings);
  }
  if (!$settings) {
    $settings = variable_get('popup-block-settings', array());
  }
  return $settings;
}

Functions

Namesort descending Description
popup_block_features_api Implementation of hook_features_api
popup_block_form_alter Implementation of hook_form_alter Adds UI settings to block configuration
popup_block_form_submit
popup_block_preprocess_block
_popup_block_settings