You are here

function boxes_block_view in Boxes 7

Same name and namespace in other branches
  1. 7.2 boxes.module \boxes_block_view()

Implements hook_block_view().

1 call to boxes_block_view()
boxes_footer in ./boxes.module
Implements hook_footer().

File

./boxes.module, line 145
Core functionality for boxes module.

Code

function boxes_block_view($delta) {

  // Add boxes JS.
  boxes_add_js();
  ctools_include('export');

  // If the 'add' box was requested, replace the delta with a unique delta.
  if (strpos($delta, 'boxes_add__') === 0) {
    $plugin_key = str_replace('boxes_add__', '', $delta);
    $identifier = module_exists('spaces') && ($space = spaces_get_space()) ? "{$space->type}-{$space->id}" : 'box';
    $hash = boxes_create_hash($identifier);
    $delta = $identifier . "-" . $hash;
    $box = boxes_factory($plugin_key, array(
      'delta' => $delta,
    ));
    $form_state = array(
      'box' => $box,
      'plugin_key' => $plugin_key,
      'custom_action' => TRUE,
      'no_redirect' => TRUE,
    );

    // We need to explicitly set the form input as empty to avoid the form being
    // processed. The actual form submission is handled in the footer.
    $form_state['input'] = array();
    $form_state['init_form'] = TRUE;
    $form = drupal_build_form('boxes_box_form', $form_state);
    $block['delta'] = $delta;
    $block['content'] = '';
    $block['editing'] = $form;
    $block['content'] = theme('boxes_box', array(
      'block' => $block,
    ));
    $plugin = ctools_get_plugins('boxes', 'plugins', $plugin_key);
    $block['subject'] = t('Add custom @title', array(
      '@title' => strtolower($plugin['title']),
    ));
    $block['boxes_plugin'] = $box->plugin_key;
    return $block;
  }
  elseif ($box = boxes_box_load($delta)) {

    // Generate content and provide an editing form if user has proper
    // permissions.
    $block = $box
      ->render();
    if (boxes_access_edit()) {
      if (variable_get('boxes_edit_location', BOXES_EDIT_IN_PLACE) == BOXES_EDIT_IN_PLACE) {
        $edit_link = array(
          'title' => t('Edit Box'),
          'href' => drupal_is_front_page() ? '<front>' : $_GET['q'],
          'query' => array(
            'plugin_key' => $box->plugin_key,
            'boxes_delta' => $block['delta'],
          ),
          'attributes' => array(
            'class' => array(
              'use-ajax',
            ),
          ),
        );
      }
      else {
        $edit_link = array(
          'title' => t('Edit Box'),
          'href' => 'admin/structure/block/manage/boxes/' . $block['delta'] . '/configure',
          'query' => drupal_get_destination(),
        );
      }
      $block['controls'] = theme('links', array(
        'links' => array(
          'edit' => $edit_link,
          'cancel' => array(
            'title' => t('Cancel'),
            'href' => drupal_is_front_page() ? '<front>' : $_GET['q'],
          ),
        ),
      ));
    }

    // Add additional_classes
    if (!empty($box->options['additional_classes'])) {
      $block['additional_classes'] = $box->options['additional_classes'];
    }
    $block['content'] = theme('boxes_box', array(
      'block' => $block,
    ));
    $block['boxes_plugin'] = $box->plugin_key;
    return $block;
  }
}