You are here

function boxes_block in Boxes 6

Implementation of hook_block().

2 calls to boxes_block()
boxes_create_hash in ./boxes.module
Create a hash for a block id.
boxes_footer in ./boxes.module
Implementation of hook_footer().

File

./boxes.module, line 56

Code

function boxes_block($op = 'list', $delta = '', $edit = array()) {
  switch ($op) {
    case 'list':
      $boxes = boxes_load();
      $blocks = array();
      foreach ($boxes as $box) {
        $blocks[$box->delta]['info'] = $box->description;

        // Not worth caching.
        $blocks[$box->delta]['cache'] = BLOCK_NO_CACHE;
      }

      // 'Add' blocks for editing a page inline
      ctools_include('plugins');
      $plugins = ctools_get_plugins('boxes', 'plugins');
      foreach ($plugins as $key => $info) {
        if (isset($info['title'])) {
          $blocks["boxes_add__{$key}"]['info'] = t('Add custom @title', array(
            '@title' => strtolower($info['title']),
          ));
          $blocks["boxes_add__{$key}"]['cache'] = BLOCK_NO_CACHE;
        }
      }
      return $blocks;
    case 'configure':
      if ($delta && strpos($delta, 'boxes_add__') !== 0) {
        if ($box = boxes_load($delta)) {
          if ($box
            ->options_form()) {
            $form = boxes_box_form(array(
              'box' => $box,
            ));
            unset($form['submit']);
            return $form;
          }
        }
      }
      break;
    case 'save':
      $edit['delta'] = $delta;
      $box = boxes_factory($edit['plugin_key'], $edit);
      $box
        ->save();
      break;
    case 'view':

      // 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) {
        ctools_include('form');
        $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,
        );
        $form = ctools_build_form('boxes_box_form', $form_state);
        $block['delta'] = $delta;
        $block['content'] = '';
        $block['editing'] = $form;
        $block['content'] = theme('boxes_box', $block);
        $plugin = ctools_get_plugins('boxes', 'plugins', $plugin_key);
        $block['subject'] = t('Add custom @title', array(
          '@title' => strtolower($plugin['title']),
        ));
        return $block;
      }
      elseif ($box = boxes_load($delta)) {

        // Generate content and provide an editing form if user has proper
        // permissions.
        $block = $box
          ->render();
        if (boxes_access_admin()) {
          $block['controls'] = theme('links', array(
            'edit' => array(
              'title' => t('Edit'),
              'href' => $_GET['q'],
              'query' => 'plugin_key=&boxes_delta=' . $block['delta'],
              'attributes' => array(
                'class' => 'ctools-use-ajax',
              ),
            ),
            'cancel' => array(
              'title' => t('Cancel'),
              'href' => $_GET['q'],
            ),
          ));
        }
        $block['content'] = theme('boxes_box', $block);

        // Avoid that block module overrides title and set both subject and title.
        $block['title'] = $block['subject'];
        return $block;
      }
      break;
  }
}