You are here

function icon_block_form_alter in Icon API 8

Same name and namespace in other branches
  1. 7 modules/icon_block/icon_block.module \icon_block_form_alter()

Implements hook_form_alter().

File

modules/icon_block/icon_block.module, line 106
icon_block.module Provides icon integration with blocks.

Code

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';
    }
  }
}