public function views_handler_filter_dynamic_fields::value_form in Views Dynamic Fields 7
Provide a list of all fields that are for display.
Overrides views_handler_filter::value_form
File
- handlers/
views_handler_filter_dynamic_fields.inc, line 194 - Views dynamic fields filter handler.
Class
- views_handler_filter_dynamic_fields
- Simple filter to pick and choose the fields to show in the view.
Code
public function value_form(&$form, &$form_state) {
// All the non-permanently excluded fields of the view that we will display
// in the exposed filter.
$all_fields = array();
// Just the names of all the non-permanently excluded fields.
$field_names = array();
$view_fields = $this->view_fields;
$combined_fields = $this->combined_fields;
// The final fields that are part of the filter after going through a
// pruning process.
$displayed_field_names = array();
$filterable_fields = array_filter($this->options['filterable_fields']);
$defaults_filterable_fields = array_filter($this->options['defaults_filterable_fields']);
// Create fields to be displayed as part of the filter.
foreach ($view_fields as $field_name => $field) {
// Apply filterable fields options removing any field not in the set of
// filterable fields.
if (!empty($filterable_fields) && !isset($filterable_fields[$field_name])) {
continue;
}
if (!isset($_SESSION[$this->view->name]['perm_exc']) && isset($field['exclude']) && $field['exclude']) {
// Fields already marked as 'exclude from display' in the original
// view should be omitted.
$_SESSION[$this->view->name]['perm_exc'][$field_name] = $field_name;
continue;
}
if (!isset($_SESSION[$this->view->name]['perm_exc'][$field_name])) {
// Use the field id as label if label is empty
// @see http://drupal.org/node/1951742
if (empty($field['label'])) {
$all_fields[$field_name] = ucfirst($field['id']);
}
else {
$all_fields[$field_name] = $field['label'];
}
$field_names[] = $field_name;
// Get fields to display in the plain (unfiltered) view.
if (isset($defaults_filterable_fields[$field_name]) && $defaults_filterable_fields[$field_name] == $field_name) {
$field_names_defaults[$field_name] = $field_name;
}
}
}
// Also add any combined fields to the default fields to display
// as configured.
foreach ($combined_fields as $c_parent => $c_child) {
if ($defaults_filterable_fields[$c_parent] == $c_parent) {
$field_names_defaults[$c_parent] = $c_parent;
}
}
// Initialize the permanent exclusion array if not set.
if (!isset($_SESSION[$this->view->name]['perm_exc'])) {
$_SESSION[$this->view->name]['perm_exc'] = array();
}
// Display all the filter fields in the plain view if no explicit.
// defaults are set in the view.
$field_names_defaults = empty($field_names_defaults) ? $all_fields : $field_names_defaults;
if ($form_state['exposed']) {
// Get submitted filter data if any.
$exposed_input = $this->view
->get_exposed_input();
if (isset($exposed_input[$this->options['expose']['identifier']])) {
$exposed_input_fields = $exposed_input[$this->options['expose']['identifier']];
$exposed_field_names = isset($exposed_input['field_names']) ? unserialize($exposed_input['field_names']) : $field_names;
}
$this->options['expose']['multiple'] = $this->options['checkboxes'];
// If checkboxes has not been enabled, display the fields in as
// select list / drop down.
if (!$this->options['checkboxes']) {
$form['value'] = array(
'#type' => 'select',
'#no_convert' => $this->options['checkboxes'],
'#title' => $this->options['expose']['label'] ? $this->options['expose']['label'] : t('Fields'),
'#options' => $all_fields,
'#description' => $this->options['description'] ? check_plain($this->options['description']) : t('Select the field to display in the view'),
);
$displayed_field_names = $field_names;
}
else {
// For a view that has already been filtered by the user.
if (!empty($exposed_input_fields)) {
$delta = count($exposed_input_fields);
$key = 0;
// Regenerate the sortable table.
foreach ($exposed_input_fields as $oldid => $oldvalue) {
$def_check = isset($oldvalue['check']) && $oldvalue['check'] || empty($all_fields[$exposed_field_names[$oldid]]);
$def_sort = $oldvalue['sort'] ? $oldvalue['sort'] : $key;
$def_disabled = empty($all_fields[$exposed_field_names[$oldid]]);
$def_title = '';
if (isset($all_fields[$exposed_field_names[$oldid]]) && !empty($all_fields[$exposed_field_names[$oldid]])) {
$def_title = $all_fields[$exposed_field_names[$oldid]];
}
else {
if (empty($view_fields[$exposed_field_names[$oldid]]['label'])) {
$def_title = ucfirst($view_fields[$exposed_field_names[$oldid]]['id']);
}
else {
$def_title = $view_fields[$exposed_field_names[$oldid]]['label'];
}
}
$def_desc = empty($all_fields[$exposed_field_names[$oldid]]) ? t('Visible') : '';
$form['value'][$key]['check'] = array(
'#type' => 'checkbox',
'#value' => $def_check,
// Set field to hidden style rather than disabling them.
// @see http://drupal.org/node/2291727
// notes: disabled attribute prevents the item to be submitted
// readonly attribute only protects the checkbox value and
// not its status (checked/unchecked) so we don't use it
// @see https://www.drupal.org/node/2368771
'#attributes' => $def_disabled ? array(
'style' => 'visibility: hidden',
) : '',
'#description' => check_plain($def_desc),
);
$form['value'][$key]['title'] = array(
'#markup' => $def_title,
);
$form['value'][$key]['sort'] = array(
'#type' => 'weight',
'#delta' => $delta,
'#value' => $def_sort,
'#default_value' => $def_sort,
);
$displayed_field_names[] = $exposed_field_names[$oldid];
++$key;
}
}
else {
$delta = count($view_fields) - count($combined_fields);
$key = 0;
// Create the sortable table.
foreach ($view_fields as $field_title => $field_info) {
// Skip over permanently excluded fields.
if (isset($field_info['exclude']) && $field_info['exclude']) {
continue;
}
// Display only default filterable fields
// if there is no exposed input.
$def_check = in_array($field_title, array_keys($field_names_defaults)) || empty($all_fields[$field_title]);
$def_sort = $key;
$def_desc = empty($all_fields[$field_title]) ? t('Visible') : '';
$def_disabled = empty($all_fields[$field_title]);
$def_title = '';
if (isset($all_fields[$field_title]) && !empty($all_fields[$field_title])) {
$def_title = $all_fields[$field_title];
}
else {
if (empty($view_fields[$field_title]['label'])) {
$def_title = ucfirst($view_fields[$field_title]['id']);
}
else {
$def_title = $view_fields[$field_title]['label'];
}
}
$form['value'][$key]['check'] = array(
'#type' => 'checkbox',
'#value' => $def_check,
// Set field to hidden style rather than disabling them.
// @see http://drupal.org/node/2291727
// notes: disabled attribute prevents the item to be submitted
// readonly attribute only protects the checkbox value and
// not its status (checked/unchecked) so we don't use it
// @see https://www.drupal.org/node/2368771
'#attributes' => $def_disabled ? array(
'style' => 'visibility: hidden',
) : '',
'#description' => check_plain($def_desc),
);
$form['value'][$key]['title'] = array(
'#markup' => $def_title,
);
$form['value'][$key]['sort'] = array(
'#type' => 'weight',
'#delta' => $delta,
'#default_value' => $def_sort,
);
$displayed_field_names[] = $field_title;
++$key;
}
}
// Theme the draggable sort table.
$form['value']['#theme'] = 'views_dynamic_fields_sort_filter_fields';
}
$form['value']['#tree'] = TRUE;
$form['value']['#title'] = $this->options['expose']['label'] ? $this->options['expose']['label'] : t('Fields');
// Store the fields being displayed in the filter for use
// as form data when processing filter submissions.
$form['field_names'] = array(
'#type' => 'hidden',
'#value' => serialize($displayed_field_names),
);
$form['combined'] = array(
'#type' => 'hidden',
'#value' => serialize($combined_fields),
'#weight' => 9,
);
if ($this->options['reverse']) {
$form['reverse'] = array(
'#type' => 'checkbox',
'#default_value' => $this->options['reverse'],
'#title' => $this->options['reverse_label'] ? $this->options['reverse_label'] : t('Selected fields will be excluded'),
'#description' => t('Check this to exclude the selected fields from the view.'),
);
}
}
else {
// Ensure there is something in the 'value'.
$form['value'] = array(
'#type' => 'value',
'#value' => NULL,
);
}
}