You are here

function space_customizer_block::form in Spaces 6.2

Same name and namespace in other branches
  1. 6 spaces.module \space_customizer_block::form()

Implementation of form().

Overrides space_customizer::form

File

./spaces.spaces.inc, line 157

Class

space_customizer_block
Customizer for feature blocks.

Code

function form($space, $feature) {
  global $theme_key;
  init_theme();
  $regions = system_region_list($theme_key);

  // Collect all contexts associated with this feature
  $feature_contexts = drupal_map_assoc(spaces_features_items('context', $feature));
  $contexts = context_enabled_contexts();
  $contexts = array_intersect_key($contexts, $feature_contexts);
  $master_contexts = array();
  $master_defaults = array();
  foreach ($contexts as $identifier => $context) {
    if (!empty($context->block)) {

      // Get customized values -- unfortunately we can't use our own customization
      // methods as block customization is a bit more complex.
      $customizer = !empty($space->customizer['block'][$identifier]) ? $space->customizer['block'][$identifier] : array();
      $subject = !empty($space->customizer['block']['subject']) ? $space->customizer['block']['subject'] : array();
      $form = array(
        '#title' => $identifier,
        '#tree' => TRUE,
      );
      $defaults = array(
        '#tree' => TRUE,
      );
      foreach ($context->block as $i => $block) {

        // @TODO: remove this once context is less confused about itself.
        $block = (array) $block;
        $bid = "{$block['module']}-{$block['delta']}";

        // Sanity check that this region exists
        if (!empty($block['region']) && !empty($regions[$block['region']])) {
          $region = $block['region'];
          if (!isset($form[$region])) {
            $form[$region] = array(
              '#title' => $regions[$region],
              '#tree' => TRUE,
            );
          }
          $block_details = module_invoke($block['module'], 'block', 'view', $block['delta']);
          $default_subject = !empty($block_details['subject']) ? $block_details['subject'] : '';
          $default_weight = isset($block[$i]['weight']) ? $block[$i]['weight'] : 0;
          $default_status = isset($block[$i]['status']) ? $block[$i]['status'] : 1;

          // Store defaults to compare against changed values in submit handler
          $defaults[$region][$bid]['weight'] = array(
            '#type' => 'value',
            '#value' => $default_weight,
          );
          $defaults[$region][$bid]['status'] = array(
            '#type' => 'value',
            '#value' => $default_status,
          );
          $defaults[$region][$bid]['subject'] = array(
            '#type' => 'value',
            '#value' => $default_subject,
          );
          $form[$region][$bid] = array(
            '#tree' => TRUE,
            '#weight' => isset($customizer[$region][$bid]['weight']) ? $customizer[$region][$bid]['weight'] : 0,
          );
          $form[$region][$bid]['weight'] = array(
            '#type' => 'weight',
            '#delta' => 25,
            '#default_value' => isset($customizer[$region][$bid]['weight']) ? $customizer[$region][$bid]['weight'] : 0,
          );
          $form[$region][$bid]['status'] = array(
            '#type' => 'checkbox',
            '#default_value' => isset($customizer[$region][$bid]['status']) ? $customizer[$region][$bid]['status'] : 1,
          );

          // If the block subject is empty, it's likely to be for a good reason
          // e.g. the subject is generated dynamically or the block is slimmed
          // down. Let's respect that.
          if (!empty($default_subject)) {
            $form[$region][$bid]['subject'] = array(
              '#type' => 'textfield',
              '#default_value' => !empty($subject[$bid]) ? $subject[$bid] : $default_subject,
              '#description' => $this
                ->get_block_info($block['module'], $block['delta']),
            );
          }
          else {
            $form[$region][$bid]['subject'] = array(
              '#type' => 'markup',
              '#value' => $this
                ->get_block_info($block['module'], $block['delta']),
            );
          }
        }
      }
      $master_contexts[$identifier] = $form;
      $master_defaults[$identifier] = $defaults;
    }
  }
  if (!empty($master_contexts) && !empty($master_defaults)) {
    $master_form = array(
      '#theme' => 'spaces_block_customizer_settings_form',
      'contexts' => array_merge(array(
        '#tree' => TRUE,
      ), $master_contexts),
      'defaults' => array_merge(array(
        '#tree' => TRUE,
      ), $master_defaults),
    );
    return $master_form;
  }
  else {
    return NULL;
  }
}