-
-
Save ethode/c955290e4f86449d63e77d080ec0801d to your computer and use it in GitHub Desktop.
Drupal 7 Views 3 custom field handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies[] = ctools | |
; Views Handlers | |
files[] = views/jcycles_handler_handlername.inc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_views_api(). | |
*/ | |
function jcycles_views_api() { | |
return array( | |
'api' => 3, | |
'path' => drupal_get_path('module', 'jcycles') . '/views', | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file must be at jcycles/views directory. | |
/** | |
* @file | |
* Definition of jcycles_handler_handlername. | |
*/ | |
/** | |
* Description of what my handler does. | |
*/ | |
class jcycles_handler_handlername extends views_handler_field { | |
/** | |
* Add some required fields needed on render(). | |
*/ | |
function construct() { | |
parent::construct(); | |
$this->additional_fields['field_something'] = array( | |
'table' => 'field_data_field_something', | |
'field' => 'field_something_value', | |
); | |
} | |
/** | |
* Loads additional fields. | |
*/ | |
function query() { | |
$this->ensure_my_table(); | |
$this->add_additional_fields(); | |
} | |
/** | |
* Default options form. | |
*/ | |
function option_definition() { | |
$options = parent::option_definition(); | |
$options['option_a'] = array('default' => ''); | |
$options['option_b'] = array('default' => ''); | |
return $options; | |
} | |
/** | |
* Creates the form item for the options added. | |
*/ | |
function options_form(&$form, &$form_state) { | |
parent::options_form($form, $form_state); | |
$form['option_a'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Label'), | |
'#default_value' => $this->options['option_a'], | |
'#description' => t('Some description.'), | |
'#weight' => -10, | |
); | |
$form['option_b'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Label'), | |
'#default_value' => $this->options['option_b'], | |
'#description' => t('Some description.'), | |
'#weight' => -9, | |
); | |
} | |
/** | |
* Renders the field handler. | |
*/ | |
function render($values) { | |
return format_string('<div class="@option_a" data-tag="@field_something" data-follow_name="@option_b"></div>', array( | |
'@field_something' => $values->field_data_field_something_field_something_value, | |
'@option_a' => $this->options['option_a'], | |
'@option_b' => $this->options['option_b'], | |
)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This file must be at jcycles/views directory. | |
/** | |
* @file | |
* Views definitions for jcycles module. | |
*/ | |
/** | |
* Implements hook_views_data(). | |
*/ | |
function jcycles_views_data() { | |
$data = array(); | |
$data['node']['handlername'] = array( | |
'title' => t('Name of my handler'), | |
'help' => t('Description of my handler.'), | |
'field' => array( | |
'handler' => 'jcycles_handler_handlername', | |
), | |
); | |
return $data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment