3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Provide a Views argument validation plugin for farm assets.

This commit is contained in:
Michael Stenta 2017-04-27 09:55:22 -04:00
parent c335616ce8
commit c949b6e41a
4 changed files with 79 additions and 8 deletions

View file

@ -7,3 +7,4 @@ dependencies[] = views
dependencies[] = views_bulk_operations
files[] = farm_asset.class.inc
files[] = views/handlers/farm_asset_handler_relationship_entity_reverse.inc
files[] = views/plugins/farm_asset_plugin_argument_validate_farm_asset.inc

View file

@ -42,3 +42,18 @@ function farm_asset_views_data_alter(&$data) {
}
}
}
/**
* Implements hook_views_plugins().
*/
function farm_asset_views_plugins() {
return array(
'argument validator' => array(
'farm_asset' => array(
'title' => t('Farm asset'),
'handler' => 'farm_asset_plugin_argument_validate_farm_asset',
'path' => drupal_get_path('module', 'farm_asset') . '/views/plugins',
),
),
);
}

View file

@ -118,14 +118,7 @@ function farm_asset_children_views_default_views() {
$handler->display->display_options['arguments']['field_farm_parent_target_id']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['field_farm_parent_target_id']['summary_options']['items_per_page'] = '25';
$handler->display->display_options['arguments']['field_farm_parent_target_id']['specify_validation'] = TRUE;
$handler->display->display_options['arguments']['field_farm_parent_target_id']['validate']['type'] = 'php';
$handler->display->display_options['arguments']['field_farm_parent_target_id']['validate_options']['code'] = '// Attempt to load the asset.
$farm_asset = farm_asset_load($argument);
if (!empty($farm_asset)) {
// Set the title.
$handler->validated_title = entity_label(\'farm_asset\', $farm_asset);
return TRUE;
}';
$handler->display->display_options['arguments']['field_farm_parent_target_id']['validate']['type'] = 'farm_asset';
/* Filter criterion: Farm asset: Name */
$handler->display->display_options['filters']['name']['id'] = 'name';
$handler->display->display_options['filters']['name']['table'] = 'farm_asset';

View file

@ -0,0 +1,62 @@
<?php
/**
* @file
* Contains the 'farm asset' argument validator plugin.
*/
/**
* Validate whether an argument is an acceptable farm_asset entity.
*/
class farm_asset_plugin_argument_validate_farm_asset extends views_plugin_argument_validate {
function option_definition() {
$options = parent::option_definition();
$options['types'] = array('default' => array());
return $options;
}
function options_form(&$form, &$form_state) {
$types = farm_asset_types();
$options = array();
foreach ($types as $type => $definition) {
$options[$type] = check_plain($definition->label);
}
$form['types'] = array(
'#type' => 'checkboxes',
'#title' => t('Asset types'),
'#options' => $options,
'#default_value' => $this->options['types'],
'#description' => t('If you wish to validate for specific asset types, check them; if none are checked, all assets will pass.'),
);
}
function options_submit(&$form, &$form_state, &$options = array()) {
// Filter unselected items so we don't unnecessarily store giant arrays.
$options['types'] = array_filter($options['types']);
}
function validate_argument($argument) {
// If the argument is not a number, fail.
if (!is_numeric($argument)) {
return FALSE;
}
// Attempt to load the farm asset entity.
$farm_asset = farm_asset_load($argument);
// If the asset didn't load, fail.
if (empty($farm_asset)) {
return FALSE;
}
// Set the validated title.
$this->argument->validated_title = check_plain(entity_label('farm_asset', $farm_asset));
// Load the valid asset types from options, and return TRUE if the list is
// empty, or if the type is in the list.
$types = array_filter($this->options['types']);
return empty($types) || !empty($types[$farm_asset->type]);
}
}