Sunday 17 October 2010

Drupal views title block with clickable "more" link

Ok, so you have a block created in views with i.e. last 10 comments and you want to add link to the page with the rest of the comments. You can add standard "more" link in views which will be included under the block, but you saw on Facebook nice "more" link included in the block title.
----------------------------------
Title                more>
----------------------------------
Here I will show you how you can do it.

We will create a small module called 'Block "more" link', which will add new display to views.

There are 4 files needed

1. block_more_link.info

name = Block "more" link
description = Adds "more" link in Views block header, (title link)
core = 6.x
package = Views 
 
2. block_more_link.views.inc

<?php
function block_more_link_views_plugins() {
  return array(
    'display' => array(
      'block_more_link' => array(
        'title' => t('Block (more link)'),
        'help' => t('Display the view as a block with "more" linked to one of the other handlers.'),
        'handler' => 'views_plugin_display_block_more_link',
        'theme' => 'views_view',
        'uses hook block' => TRUE,
        'use ajax' => TRUE,
        'use pager' => TRUE,
        'use more' => TRUE,
        'accept attachments' => TRUE,
        'admin' => t('Block ("more" link)'),
        'help topic' => 'display-block',
        'parent' => 'block',
      ),
    ),
  );
} 
 
3. views_plugin_display_block_more_link.inc

<?php
/**
 * @file
 * Contains the block display plugin.
 */

/**
 * The plugin that handles a block.
 *
 * @ingroup views_display_plugins
 */
class views_plugin_display_block_more_link extends views_plugin_display_block {
  function option_definition() {
    $options = parent::option_definition();

    $options['subject_link'] = array('default' => '');

    return $options;
  }

  /**
   * The display block handler returns the structure necessary for a block.
   */
  function execute() {
    $info = parent::execute();

    if (!is_array($info)) {
      return;
    }

    // Now check to see if we have a link to add:
    $subject_link = $this->get_option('subject_link');
    if (!empty($subject_link)) {
      $displays = $this->_get_displays($this->view);
      if (isset($displays[$subject_link])) {
        $this_display = $this->display->id;
        $this->view->set_display($subject_link);
        if ($this->view->access($subject_link)) {
          $info['subject'] = '
' . $info['subject'] . '
' . l(t('more') , $this->view->get_url(), array('html' => TRUE)) . '
'; } $this->view->set_display($this_display); } } return $info; } /** * Provide the summary for page options in the views UI. * * This output is returned as an array. */ function options_summary(&$categories, &$options) { // It is very important to call the parent function here: parent::options_summary($categories, $options); $subject_link = $this->get_option('subject_link'); if (empty($subject_link)) { $subject_link = t('No link'); } else { $displays = $this->_get_displays($this->view); if (isset($displays[$subject_link])) { $subject_link = $displays[$subject_link]; } } if (strlen($subject_link) > 16) { $subject_link = substr($subject_link, 0, 16) . '...'; } $options['subject_link'] = array( 'category' => 'block', 'title' => t('Block "more" link'), 'value' => $subject_link, ); } /** * Provide the default form for setting options. */ function options_form(&$form, &$form_state) { // It is very important to call the parent function here: parent::options_form($form, $form_state); switch ($form_state['section']) { case 'subject_link': $form['#title'] .= t('Block "more" link'); $options = array_merge(array('' => t('No link'), ' ' => '-'), $this->_get_displays($this->view)); $form['subject_link'] = array( '#type' => 'select', '#description' => t('Select a page display to link this block\'s title to.'), '#default_value' => $this->get_option('subject_link'), '#options' => $options, ); break; } //dpm($this); } function _get_displays($view) { $displays = array(); foreach ($view->display as $display) { $displays[$display->id] = $display->display_title; } return $displays; } /** * Perform any necessary changes to the form values prior to storage. * There is no need for this function to actually store the data. */ function options_submit($form, &$form_state) { // It is very important to call the parent function here: parent::options_submit($form, $form_state); switch ($form_state['section']) { case 'subject_link': $this->set_option('subject_link', trim($form_state['values']['subject_link'])); break; } } } 
 
4. block_more_link.module

<?php
function block_more_link_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'block_more_link'),
  );
}

Now add some CSS to your style.css file to float div's:
 
.block-title .title-left {
  float:left;
}
.block-title .title-right {
  float:right;
} 
  
Put all 4 files in one directory called "block_more_link", place this directory in sites/all/modules enable module on administration page. Go to your views and you will see new display called "Block ("more" link)"

This is a modified version of the code found on http://www.computerminds.co.uk website.

No comments:

Post a Comment