Custom Field Handlers for Views

This is the goal: Extending the  schedulermodule  tobe able to view a countdown until a node is published orunpublished. Countdown meaning "How many time units until the nodeis published or unpublished". This would be the perfect job for theviews module. Scheduler already exposes its publish_on andunpublish_on fields for use with views.

Defining the supported views version in filescheduler.module

<?php
function  scheduler_views_api (){
  
$info [ 'api' ]=  2 ;
  return 
$info ;
}
?>

Defining the table and fields in filescheduler.views.inc:

<?php
function scheduler_views_data(){
  
$tables['scheduler']['table']['group']= t('Scheduler');

  
//how is the scheduler table linked to the nodes
  
$tables['scheduler']['table']['join']['node']= array(
    
'left_field' => 'nid',
    
'field' => 'nid',
  );

  
//description of the fields (table columns)
  
$tables['scheduler']['publish_on']= array(
    
'title' => t('Publishon'),
    
'help' => t('Date/timeon which the article will be automaticallypublished'),
    
'field' =>array(
      
'handler' => 'views_handler_field_date',
      
'clicksortable' => TRUE,
    ),
    
'filter' =>array(
      
'handler' => 'views_handler_filter_date',
      
'label' => t('Publishon'),
    ),
    
'sort' =>array(
      
'handler' => 'views_handler_sort_date',
    ),
  );

  
$tables['scheduler']['unpublish_on']= array(
    
'title' => t('Unpublishon'),
    
'help' => t('Date/timeon which the article will be automaticallyunpublished'),
    
'field' =>array(
      
'handler' => 'views_handler_field_date',
      
'clicksortable' => TRUE,
    ),
    
'filter' =>array(
      
'handler' => 'views_handler_filter_date',
      
'label' => t('Unpublishon'),
    ),
    
'sort' =>array(
      
'handler' => 'views_handler_sort_date',
    ),
  );

  return 
$tables;
}
?>

Since we just exposed existing DB fields we only needed to definehow the scheduler table is to be joined with the node table. Fordisplaying the values we could rely on a predefined handler fordate values (views_handler_field_date).That is the code that we need to build upon.

What we need now is a custom handler because there is no predefinedhandler for our purpose. At first we need to define the field.Let's start with a simple second count down:


<?php
  $tables
['scheduler']['publish_countdown']= array(
    
'title' => t('Publishcountdown'),
    
'help' => t('Timeuntil the article will be automaticallypublished'),
    
'field' =>array(
      
'handler' => 'scheduler_handler_field_scheduler_countdown',
      
'clicksortable' => FALSE,
      
'timestamp_field' => 'publish_on',
    ),
  );
?>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值