You are here

function prev_next_block in Previous/Next API 6

Implementation of hook_block().

File

./prev_next.module, line 268

Code

function prev_next_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $num_blocks = variable_get('prev_next_num_blocks', PREV_NEXT_NUM_BLOCKS_DEFAULT);
      for ($b = 0; $b < $num_blocks; $b++) {
        $blocks[$b] = array(
          'info' => t('Prev/Next links !blocknum', array(
            '!blocknum' => 1 + $b,
          )),
          'status' => 0,
          'cache' => BLOCK_CACHE_PER_PAGE,
        );
      }
      return $blocks;
      break;
    case 'configure':
      $description = module_exists('token') ? 'Use the available tokens (see below) to customize the link text.' : 'The [title] token will be replaced by the actual node title.';
      $form['previous'] = array(
        '#type' => 'fieldset',
        '#title' => t('Previous Node'),
        '#collapsible' => TRUE,
      );
      $form['previous']['prev_next_display_prev' . $delta] = array(
        '#type' => 'checkbox',
        '#title' => t('Display'),
        '#default_value' => variable_get('prev_next_display_prev' . $delta, PREV_NEXT_DISPLAY_DEFAULT),
      );
      $form['previous']['prev_next_display_text_prev' . $delta] = array(
        '#type' => 'textfield',
        '#title' => t('Link text'),
        '#description' => $description,
        '#default_value' => variable_get('prev_next_display_text_prev' . $delta, PREV_NEXT_DISPLAY_TEXT_PREV_DEFAULT),
      );
      if (module_exists('token')) {
        $form['previous']['token_help'] = array(
          '#title' => t('Replacement patterns'),
          '#type' => 'fieldset',
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
        );
        $form['previous']['token_help']['help'] = array(
          '#value' => theme('token_help', 'node'),
        );
      }
      $form['next'] = array(
        '#type' => 'fieldset',
        '#title' => t('Next Node'),
        '#collapsible' => TRUE,
      );
      $form['next']['prev_next_display_next' . $delta] = array(
        '#type' => 'checkbox',
        '#title' => t('Display'),
        '#default_value' => variable_get('prev_next_display_next' . $delta, PREV_NEXT_DISPLAY_DEFAULT),
      );
      $form['next']['prev_next_display_text_next' . $delta] = array(
        '#type' => 'textfield',
        '#title' => t('Link text'),
        '#description' => $description,
        '#default_value' => variable_get('prev_next_display_text_next' . $delta, PREV_NEXT_DISPLAY_TEXT_NEXT_DEFAULT),
      );
      if (module_exists('token')) {
        $form['next']['token_help'] = array(
          '#title' => t('Replacement patterns'),
          '#type' => 'fieldset',
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
        );
        $form['next']['token_help']['help'] = array(
          '#value' => theme('token_help', 'node'),
        );
      }
      return $form;
      break;
    case 'save':
      variable_set('prev_next_display_prev' . $delta, $edit['prev_next_display_prev' . $delta]);
      variable_set('prev_next_display_next' . $delta, $edit['prev_next_display_next' . $delta]);
      variable_set('prev_next_display_text_prev' . $delta, $edit['prev_next_display_text_prev' . $delta]);
      variable_set('prev_next_display_text_next' . $delta, $edit['prev_next_display_text_next' . $delta]);
      break;
    case 'view':
      $content = '';
      $next_display = variable_get('prev_next_display_next' . $delta, PREV_NEXT_DISPLAY_DEFAULT);
      $next_text = variable_get('prev_next_display_text_next' . $delta, PREV_NEXT_DISPLAY_TEXT_NEXT_DEFAULT);
      $prev_display = variable_get('prev_next_display_prev' . $delta, PREV_NEXT_DISPLAY_DEFAULT);
      $prev_text = variable_get('prev_next_display_text_prev' . $delta, PREV_NEXT_DISPLAY_TEXT_PREV_DEFAULT);
      if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2)) {
        $node = node_load(arg(1));
        $n_nid = prev_next_nid($node->nid, 'next');
        $p_nid = prev_next_nid($node->nid, 'prev');
        if ($n_nid || $p_nid) {
          if ($p_nid && $prev_display && $prev_text != '') {
            $p_node = node_load($p_nid);
            if (module_exists('token')) {
              $link = token_replace($prev_text, 'node', $p_node);
            }
            else {
              $link = str_replace('[title]', $p_node->title, $prev_text);
            }
            $options = array(
              'html' => TRUE,
            );
            $content .= '<li class="prev-next-link-prev">' . l($link, "node/{$p_nid}", $options) . '</li>';
          }
          if ($n_nid && $next_display && $next_text != '') {
            $n_node = node_load($n_nid);
            if (module_exists('token')) {
              $link = token_replace($next_text, 'node', $n_node);
            }
            else {
              $link = str_replace('[title]', $n_node->title, $next_text);
            }
            $options = array(
              'html' => TRUE,
            );
            $content .= '<li class="prev-next-link-next">' . l($link, "node/{$n_nid}", $options) . '</li>';
          }
          $block = array(
            'subject' => t('Prev/Next links'),
            'content' => '<ul class="prev-next-links">' . $content . '</ul>',
          );
        }
      }
      return $block;
  }
}