Presented by Tom Friedhof
http://youtube.com/activelamp
Go check it out on our YouTube channel (when it's released).
name = My Cool Module
description = Provides an area handler for views
core = 7.x
dependencies[] = views
dependencies[] = xautoload (>= 7.x-5.0)
/**
* Implements hook_views_api().
*/
function mymodule_views_api() {
return [
'api' => 3
];
}
/**
* Implements hook_views_data().
*/
function mymodule_views_data() {
return [
'views' => [
'mymodule_area' => [
'title' => t('My Custom Area'),
'help' => t('Adds a custom area handler.'),
'area' => [
'handler' => 'Drupal\mymodule\Views\Handlers\Area\MyCoolHandler'
]
]
]
];
}
namespace Drupal\mymodule\Views\Handlers\Area;
/**
* Defines a new handler class for views.
*/
class MyCoolHandler {
}
We're going to create a psuedo field
'node' => [
'my_custom_field' => [
'title' => t('My Custom Field'),
'help' => t('Showing how to write fields at SandCamp'),
'real field' => 'created',
'field' => [
'handler' => 'Drupal\mymodule\Views\Handlers\Field\MyCustomField'
],
'filter' => [
'handler' => 'Drupal\mymodule\Views\Handlers\Filter\MyCustomFilter'
]
],
]
namespace Drupal\mymodule\Views\Handlers\Field;
class MyCustomField extends \views_handler_field {
function render($values) {
$value = $this->get_value($values);
return date('Y-m-d', $value);
}
}
function option_definition() {
$options = parent::option_definition();
$options['even_odd'] = array('default' => 'odd');
return $options;
}
function options_form(&$form, &$form_state) {
$form['even_odd'] = [
'#type' => 'radios',
'#title' => t('Even or Odd'),
'#default_value' => $this->options['even_odd'],
'#options' => ['odd' => t('Only odd numbers'), 'even' => t('Just even numbers')],
'#description' => t('Demonstrating how to create your own options form')
];
parent::options_form($form, $form_state);
}
function admin_summary() {
return $this->options['even_odd'] == 'even' ? t('Just even numbers') : t('Just odd numbers');
}
function query() {
$this->ensure_my_table();
$operator = $this->options['even_odd'] == 'even' ? '=' : '>';
$this->query->add_where_expression($this->options['group'], "($this->table_alias.$this->real_field % 2) $operator 0");
}
http://youtube.com/activelamp
Send me an email:
tom@activelamp.com