You are here

icon_block.module in Icon API 7

Same filename and directory in other branches
  1. 8 modules/icon_block/icon_block.module

icon_block.module Provides icon integration with blocks.

File

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

/**
 * @file
 * icon_block.module
 * Provides icon integration with blocks.
 */

/**
 * Implements hook_icon_permission().
 */
function icon_block_icon_permission() {
  return array(
    'administer block icons' => array(
      'title' => t('Administer block icons'),
      'description' => t('Grants selected roles the ability to administer icons in blocks.'),
    ),
  );
}

/**
 * Helper function to return the default icon block values.
 */
function icon_block_defaults() {
  return array(
    'icon' => '',
    'position' => 'title_before',
    'bundle' => '',
    'wrapper' => '',
    'wrapper_class' => '',
  );
}

/**
 * Retrieves the icon block settings.
 *
 * @param stdClass $block
 *   The block object, if any.
 *
 * @return array
 *   An associative array of icon block settings.
 */
function icon_block_get_settings(stdClass $block = NULL) {
  $settings = icon_block_defaults();
  if (isset($block) && !empty($block->icon)) {
    $settings = drupal_array_merge_deep($settings, (array) unserialize($block->icon));
  }
  return $settings;
}

/**
 * Implements hook_preprocess_block().
 *
 * Add icon to blocks if necessary.
 */
function icon_block_preprocess_block(&$variables) {
  $block =& $variables['block'];
  $settings = icon_block_get_settings($block);
  if (!empty($settings['bundle']) && !empty($settings['icon'])) {
    if ($icon = theme('icon', $settings)) {
      $variables['classes_array'][] = 'has-icon';
      switch ($settings['position']) {
        case 'title_before':
          $variables['title_prefix'][] = array(
            '#markup' => $icon,
          );
          break;
        case 'title_after':
          $variables['title_suffix'][] = array(
            '#markup' => $icon,
          );
          break;
        case 'title_inside_before':
          $block->subject = $icon . $block->subject;
          break;
        case 'title_inside_after':
          $block->subject .= $icon;
          break;
        case 'content_before':
          $variables['content'] = $icon . $variables['content'];
          break;
        case 'content_after':
          $variables['content'] .= $icon;
          break;
      }
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function icon_block_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'block_admin_configure' || $form_id == 'block_add_block_form') {
    $access = user_access('administer icons') || user_access('administer block icons');
    $block = block_load($form['module']['#value'], $form['delta']['#value']);
    $settings = icon_block_get_settings($block);

    // Add an icon selector input element.
    $form['settings']['icon_selector'] = array(
      '#access' => $access,
      '#type' => 'icon_selector',
      '#default_bundle' => $settings['bundle'],
      '#default_icon' => $settings['icon'],
      '#default_wrapper' => $settings['wrapper'],
      '#default_wrapper_class' => $settings['wrapper_class'],
    );

    // Additional configuration on where to place the icon in the block.
    $form['settings']['icon_selector']['position'] = array(
      '#type' => 'select',
      '#title' => t('Position'),
      '#options' => array(
        'title_before' => t('Before title'),
        'title_after' => t('After title'),
        'title_inside_before' => t('Before title (inside markup)'),
        'title_inside_after' => t('After title (inside markup)'),
        'content_before' => t('Before content'),
        'content_after' => t('After content'),
      ),
      '#default_value' => $settings['position'],
      '#states' => array(
        'invisible' => array(
          _icon_states_selector('icon_selector[icon]') => array(
            'value' => '',
          ),
        ),
      ),
    );
    if ($access) {
      $form['#submit'][] = 'icon_block_form_submit';
    }
  }
}

/**
 * Save supplied class.
 */
function icon_block_form_submit($form, &$form_state) {
  if ($form_state['values']['form_id'] == 'block_admin_configure' || $form_state['values']['form_id'] == 'block_add_block_form') {
    $module = $form_state['values']['module'];
    $delta = $form_state['values']['delta'];

    // Update block icon settings.
    if (isset($form_state['values']['icon_selector'])) {
      db_merge('block')
        ->key(array(
        'module' => $module,
        'delta' => $delta,
      ))
        ->fields(array(
        'icon' => serialize($form_state['values']['icon_selector']),
      ))
        ->execute();
    }
  }
}

Functions

Namesort descending Description
icon_block_defaults Helper function to return the default icon block values.
icon_block_form_alter Implements hook_form_alter().
icon_block_form_submit Save supplied class.
icon_block_get_settings Retrieves the icon block settings.
icon_block_icon_permission Implements hook_icon_permission().
icon_block_preprocess_block Implements hook_preprocess_block().