My site is about chemistry labs which uses many studios like a blood test.
Some studio fields are Gender (male, female, both) and Age (2 fields, min and max values, could be days, months, years). Essentially, when I add an appointment to some patients, I need to filter an entity reference view. Studios that matches the patient's gender and age will be shown.
When the user displays a patient to add an appointment, I run code to store age and gender in global variables. I have in views only the studios nodes, and global variables, only PHP available.
I have done this before by adding a pseudo field with mymodule.views.inc and a class (views_handler_my_custom_field_filter extends views_handler_field
). I added my custom field to the view and field value as row class (hide-row). It works fine in almost all the view display modes, but not in an entity reference view, which doesn't have any class row setting.
I also tried to add a custom filter to my custom field by a views_handler_field, but I always get a column not found warning on query. Of course, it doesn't find my column because it doesn't exist. Using a real field in the handler isn't possible because I don't have a field for the query.
I also tried hook_views_query_alter()
, but the custom field doesn't appear in the query.
I also tried the Views Variable Field module. I have global variables available, but it allows only to compare a global variable with a field value.
I also tried the Entity reference view widget module, but I get AJAX errors queries pull about 150 nodes.
When I add nodes with a big list of entity reference nodes, i allways use autocomplete, a select list eat all the memory.
Please share some different approach. I wish I could not use PHP filter.
The query code I am using is the following.
function query() {
$this->ensure_my_table();
// $this->query->add_where_expression($this->options['group'], 'LIKE "show"');
// $field = $this->table_alias.$this->real_field;
$field = $this->table_alias['my_custom_field'];
dpm($field);
// $this->query->add_where($this->options['show_hide'], $field, 'show', '=');
// $this->query->add_where($this->$options['show_hide'], 'customfield'.$field, TRUE, "=");
}
The custom field handler code is the following.
function render($values) {
$r = return_field_value($values);
if ($r == 0) {
return 'show';
}
if ($r != 0) {
return 'hide';
}
}
function return_field_value($values) {
global $user;
$uuid = $user->uid;
$edad = 'edad_paciente'. $uuid;
$genero = 'genero_paciente' . $uuid;
$edad_paciente = variable_get($edad);
$genero_paciente = variable_get($genero);
$bool = TRUE;
$bool_edad = TRUE;
$bool_genero = TRUE;
$node = $values->_field_data['nid']['entity'];
$min = $node->field_estudio_valor_minimo_dias[LANGUAGE_NONE][0]['value'];
$max = $node->field_estudio_valor_maximo_dias[LANGUAGE_NONE][0]['value'];
$genero_estudio = $node->field_estudio_genero[LANGUAGE_NONE][0]['value'];
if ($edad_paciente >= $min AND $edad_paciente <= $max) {
$bool_edad = FALSE;
}
else {
$bool_edad = TRUE;
}
if ($genero_estudio == 2) {
$bool_genero = FALSE;
}
if ($genero_estudio == $genero_paciente) {
$bool_genero = FALSE;
}
if ($bool_edad == FALSE && $bool_genero == FALSE) {
$bool = FALSE;
}
else {
$bool = TRUE;
}
return $bool;
}
The filter handler in mymodule.views.inc uses the following code.
function customfield_views_data_alter(&$data) {
$data['customfield']['table']['group'] = t('Custom field');
$data['customfield']['table']['join'] = array(
'#global' => array(),
);
$data['customfield']['my_custom_field'] = array(
'title' => t('My custom field'),
'help' => t('My custom field displays Show or Hide'),
// 'real field' => 'my_custom_field',
'field' => array(
'handler' => 'views_handler_my_custom_field',
),
// 'filter' => array(
// 'handler' => 'views_handler_my_custom_field_filter',
// ),
);
return $data;
}