You are here

icon_block.module in Icon API 8

Same filename and directory in other branches
  1. 7 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.
 */
use Drupal\Core\Form\FormStateInterface;

/**
 * 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\Component\Utility\NestedArray::mergeDeep($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'];

  // @FIXME line below was...
  // $settings = icon_block_get_settings($block);
  $settings = array_merge(icon_block_defaults(), isset($block->icon) ? (array) unserialize($block->icon) : array());
  if (!empty($settings['bundle']) && !empty($settings['icon'])) {

    // @FIXME
    // theme() has been renamed to _theme() and should NEVER be called directly.
    // Calling _theme() directly can alter the expected output and potentially
    // introduce security issues (see https://www.drupal.org/node/2195739). You
    // should use renderable arrays instead.
    //
    //
    // @see https://www.drupal.org/node/2195739
    // if ($icon = theme('icon', array('bundle' => $settings['bundle'], 'icon' => $settings['icon']))) {
    //       $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, FormStateInterface &$form_state, $form_id) {
  if ($form_id == 'block_admin_configure' || $form_id == 'block_add_block_form' || $form_id == 'block_content_basic_form' || $form_id == 'block_form') {
    $access = \Drupal::currentUser()
      ->hasPermission('administer icons') || \Drupal::currentUser()
      ->hasPermission('administer block icons');

    // @FIXME Next line was...
    // $block = block_load($form['module']['#value'], $form['delta']['#value']); // From 7.x-1.0
    $block = $form_state
      ->getFormObject()
      ->getEntity();

    // From 8.x-dev upgraded
    // @FIXME 3.5 versions of the next line!
    // $settings = array_merge(icon_block_defaults(), isset($block->icon) ? (array) unserialize($block->icon) : array()); // From merge base - i.e. pre-7.x-1.0
    // Or
    // $settings = $form_state->getValue('settings'); // From 8.x-dev upgraded
    // $settings = icon_block_defaults();             // Also from 8.x-dev upgraded
    // Or
    $settings = icon_block_get_settings($block);

    // From 7.x-1.0 upgraded
    if ($block
      ->isNew()) {

      // @FIXME This empty if from 8.x-dev upgraded
    }

    // 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(
      '#access' => $access,
      '#type' => 'select',
      '#title' => t('Icon 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(
          // @FIXME was array(':input[name="icon_selector[icon]"]' => array('value' => '') // From 8.x-dev upgraded
          _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'])) {
      \Drupal::database()
        ->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().