From e389f338ef898db3403de8800d912d12b381d72d Mon Sep 17 00:00:00 2001 From: webflo Date: Mon, 21 Mar 2011 23:39:10 +0100 Subject: [PATCH 1/2] add selectbox widget and checkboxes widget. process checkboxes after submit. --- countries.module | 53 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 49 insertions(+), 4 deletions(-) diff --git a/countries.module b/countries.module index 47a2f80..aecb7d2 100644 --- a/countries.module +++ b/countries.module @@ -505,11 +505,34 @@ function countries_field_widget_info() { } /** - * Implement hook_field_widget_settings_form(). + * Implements hook_field_widget_info_alter(). */ -function countries_field_widget_settings_form($field, $instance) { - $widget = $instance['widget']; - $settings = $widget['settings']; +function countries_field_widget_info_alter(&$info) { + $info['options_buttons']['field types'][] = 'country'; + $info['options_select']['field types'][] = 'country'; +} + +/** + * Implements hook_options_list(). + */ +function countries_options_list($field) { + $function = !empty($field['settings']['options_list_callback']) ? $field['settings']['options_list_callback'] : 'countries_country_expand'; + + $filters = array( + 'continents' => array_filter($field['settings']['continents']), + 'enabled' => $field['settings']['enabled'], + ); + + if (!isset($field['#filters'])) { $field['#filters'] = $filters; } + $field = $function($field); + return $field['#options']; +} + +/** + * Implement hook_field_settings_form(). + */ +function countries_field_settings_form($field, $instance, $has_data) { + $settings = $field['settings']; $form['enabled'] = array( '#type' => 'radios', @@ -528,6 +551,7 @@ function countries_field_widget_settings_form($field, $instance) { '#default_value' => $settings['continents'], '#options' => countries_get_continents(), '#description' => t('If no continents are selected, this filter will not be used.'), + '#element_validate' => array('_countries_element_array_filter'), ); $form['size'] = array( @@ -536,10 +560,31 @@ function countries_field_widget_settings_form($field, $instance) { '#default_value' => $settings['size'], '#element_validate' => array('_element_validate_integer_positive'), ); + return $form; } /** + * Helper function: filter form elements recursive + */ +function _countries_element_array_filter($element, &$form_state) { + _countries_element_array_filter_process($element['#array_parents'], $form_state['values']); +} + +/** + * Helper function: invoked by _countries_element_array_filter() + */ +function _countries_element_array_filter_process($element = array(), &$form_state) { + $key = array_shift($element); + if (!empty($key)) { + _countries_element_array_filter_process($element, $form_state[$key]); + return; + } + + $form_state = array_filter(drupal_map_assoc($form_state)); +} + +/** * Implements hook_field_widget_form(). */ function countries_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { -- 1.7.4.1 From d669b1d836873c0f256e08dd2e80da8d58e73c4e Mon Sep 17 00:00:00 2001 From: webflo Date: Mon, 21 Mar 2011 22:51:31 +0100 Subject: [PATCH 2/2] add views integration. --- countries.info | 6 +- countries.module | 12 + views/countries.views.inc | 219 ++++++++++++++++++++ views/views_handler_field_countries_continent.inc | 40 ++++ ...ws_handler_filter_countries_continents_list.inc | 15 ++ views/views_handler_filter_countries_list.inc | 97 +++++++++ 6 files changed, 386 insertions(+), 3 deletions(-) create mode 100755 views/countries.views.inc create mode 100644 views/views_handler_field_countries_continent.inc create mode 100644 views/views_handler_filter_countries_continents_list.inc create mode 100644 views/views_handler_filter_countries_list.inc diff --git a/countries.info b/countries.info index 7e06f8f..720c0a9 100644 --- a/countries.info +++ b/countries.info @@ -3,9 +3,9 @@ description = A module that provides a FAPI / Field country element and a simple package = Fields version = VERSION core = 7.x -files[]=countries.module -files[]=countries.install -files[]=countries.admin.inc +files[] = views/views_handler_field_countries_continent.inc +files[] = views/views_handler_filter_countries_continents_list.inc +files[] = views/views_handler_filter_countries_list.inc configure = admin/config/regional/countries recommends[] = countryicons diff --git a/countries.module b/countries.module index aecb7d2..1588948 100644 --- a/countries.module +++ b/countries.module @@ -6,6 +6,8 @@ define ('COUNTRIES_ALL', 0); define ('COUNTRIES_ENABLED', 1); define ('COUNTRIES_DISABLED', 2); +define ('COUNTRIES_VIEWS_WIDGET_FIELD', 1); +define ('COUNTRIES_VIEWS_WIDGET_CUSTOM', 2); /** * Implement hook_theme(). @@ -826,4 +828,14 @@ function countries_load_multiple($cids = array(), $conditions = array(), $reset function countries_load($cid, $reset = FALSE) { $countries = countries_load_multiple(array($cid), array(), $reset); return reset($countries); +} + +/** + * Implements hook_views_api(). + */ +function countries_views_api() { + return array( + 'api' => '3', + 'path' => drupal_get_path('module', 'countries') . '/views', + ); } \ No newline at end of file diff --git a/views/countries.views.inc b/views/countries.views.inc new file mode 100755 index 0000000..ddf72f4 --- /dev/null +++ b/views/countries.views.inc @@ -0,0 +1,219 @@ + 'cid', + 'title' => t('Countries'), + 'help' => '' + ); + + $data['countries_country']['cid'] = array( + 'title' => t('Country ID'), + 'help' => t('The country ID of the country.'), + 'field' => array( + 'handler' => 'views_handler_field_numeric', + 'click sortable' => TRUE, + ), + 'filter' => array( + 'handler' => 'views_handler_filter_numeric', + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), + ); + + $data['countries_country']['iso2'] = array( + 'title' => t('ISO2'), + 'help' => t('The ISO2 country code of the country.'), + 'field' => array( + 'handler' => 'views_handler_field', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_string', + ), + 'argument' => array( + 'handler' => 'views_handler_argument_string', + ), + ); + + $data['countries_country']['iso2_list'] = array( + 'title' => t('ISO2 - list'), + 'help' => t('The ISO2 country code of the country.'), + 'real field' => 'iso2', + 'filter' => array( + 'handler' => 'views_handler_filter_countries_list', + 'property' => 'iso2', + ), + ); + + $data['countries_country']['iso3'] = array( + 'title' => t('ISO3'), + 'help' => t('The ISO3 country code of the country.'), + 'field' => array( + 'handler' => 'views_handler_field', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_string', + 'property' => 'iso3', + ), + 'argument' => array( + 'handler' => 'views_handler_argument_string', + ), + ); + + $data['countries_country']['iso3_list'] = array( + 'title' => t('ISO3 - list'), + 'help' => t('The ISO3 country code of the country.'), + 'real field' => 'iso2', + 'filter' => array( + 'handler' => 'views_handler_filter_countries_list', + 'property' => 'iso3', + ), + ); + + $data['countries_country']['name'] = array( + 'title' => t('Name'), + 'help' => t('The name of the country.'), + 'field' => array( + 'handler' => 'views_handler_field', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_string', + ), + 'argument' => array( + 'handler' => 'views_handler_argument_string', + ), + ); + + $data['countries_country']['name_list'] = array( + 'title' => t('Name - list'), + 'help' => t('The name of the country.'), + 'real field' => 'iso2', + 'filter' => array( + 'handler' => 'views_handler_filter_countries_list', + 'property' => 'name', + ), + ); + + $data['countries_country']['official_name'] = array( + 'title' => t('Official name'), + 'help' => t('The official name of the country.'), + 'field' => array( + 'handler' => 'views_handler_field', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_string', + ), + 'argument' => array( + 'handler' => 'views_handler_argument_string', + ), + ); + + $data['countries_country']['official_name_list'] = array( + 'title' => t('Offical name - list'), + 'help' => t('The official name of the country.'), + 'real field' => 'iso2', + 'filter' => array( + 'handler' => 'views_handler_filter_countries_list', + 'property' => 'official_name', + ), + ); + + $data['countries_country']['continent'] = array( + 'title' => t('Continent'), + 'help' => t('The contient of this country.'), + 'field' => array( + 'handler' => 'views_handler_field_countries_continent', + 'click sortable' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), + 'filter' => array( + 'handler' => 'views_handler_filter_string', + ), + 'argument' => array( + 'handler' => 'views_handler_argument_string', + ), + ); + + $data['countries_country']['continent_list'] = array( + 'title' => t('Continent - list'), + 'help' => t('The contient of this country.'), + 'real field' => 'continent', + 'filter' => array( + 'handler' => 'views_handler_filter_countries_continents_list', + 'property' => 'continent', + ), + ); + + $data['countries_country']['enabled'] = array( + 'title' => t('Enabled'), + 'help' => t('Whether or not the country is enabled.'), + 'field' => array( + 'handler' => 'views_handler_field_boolean', + 'click sortable' => TRUE, + ), + 'filter' => array( + 'handler' => 'views_handler_filter_boolean_operator', + 'label' => t('Enabled'), + 'type' => 'yes-no', + // use boolean_field = 1 instead of boolean_field <> 0 in WHERE statment + 'use equal' => TRUE, + ), + 'sort' => array( + 'handler' => 'views_handler_sort', + ), + ); + + return $data; +} + +/** + * Implements hook_field_views_data(). + * + * @see field_views_field_default_views_data() + */ +function countries_field_views_data($field) { + $data = field_views_field_default_views_data($field); + foreach ($data as $table_name => $table_data) { + foreach ($table_data as $field_name => $field_data) { + if (!in_array($field_name, array('table', 'entity_id', 'revision_id'))) { + $data[$table_name][$field_name]['relationship'] = array( + 'handler' => 'views_handler_relationship', + 'base' => 'countries_country', + 'base field' => 'iso2', + 'label' => t('country from !field_name', array('!field_name' => $field['field_name'])), + 'countries' => array( + 'field' => $field['field_name'], + ), + ); + } + } + } + + return $data; +} diff --git a/views/views_handler_field_countries_continent.inc b/views/views_handler_field_countries_continent.inc new file mode 100644 index 0000000..3ac8b76 --- /dev/null +++ b/views/views_handler_field_countries_continent.inc @@ -0,0 +1,40 @@ + FALSE); + + return $options; + } + + /** + * Provide continent_code option. + */ + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + $form['continent_code'] = array( + '#title' => t('Output machine name'), + '#description' => t('Display field as the continent machine name.'), + '#type' => 'checkbox', + '#default_value' => !empty($this->options['continent_code']), + ); + } + + /** + * Render contient as human readable name or continent code + */ + function render($values) { + $data = parent::render($values); + if ($this->options['continent_code'] != 1 && $data !== NULL && $data !== '') { + $continents = countries_get_continents(); + return $continents[$data]; + } + return $data; + } +} \ No newline at end of file diff --git a/views/views_handler_filter_countries_continents_list.inc b/views/views_handler_filter_countries_continents_list.inc new file mode 100644 index 0000000..50c0ee8 --- /dev/null +++ b/views/views_handler_filter_countries_continents_list.inc @@ -0,0 +1,15 @@ +value_options = countries_get_continents(); + } +} diff --git a/views/views_handler_filter_countries_list.inc b/views/views_handler_filter_countries_list.inc new file mode 100644 index 0000000..0049c9f --- /dev/null +++ b/views/views_handler_filter_countries_list.inc @@ -0,0 +1,97 @@ + COUNTRIES_VIEWS_WIDGET_FIELD); + $options['filter']['enabled'] = array('default' => COUNTRIES_ENABLED); + $options['filter']['continents'] = array('default' => array()); + return $options; + } + + function has_extra_options() { return TRUE; } + + function extra_options_form(&$form, &$form_state) { + $form['configuration'] = array( + '#type' => 'radios', + '#title' => t('Widget configuration'), + '#required' => TRUE, + '#options' => array( + COUNTRIES_VIEWS_WIDGET_FIELD => 'Field based configuration', + COUNTRIES_VIEWS_WIDGET_CUSTOM => 'Custom', + ), + '#default_value' => $this->options['configuration'], + ); + + $form['filter'] = array( + '#type' => 'fieldset', + '#title' => 'Custom settings', + '#states' => array( + // Only show field when widget equals "custom" + 'visible' => array( + ':input[name="options[configuration]"]' => array('value' => COUNTRIES_VIEWS_WIDGET_FIELD), + ), + ), + ); + + $form['filter']['enabled'] = array( + '#type' => 'radios', + '#title' => t('Country status'), + '#required' => TRUE, + '#states' => array( + // Only show field when widget equals "custom" + 'visible' => array( + ':input[name="options[configuration]"]' => array('value' => COUNTRIES_VIEWS_WIDGET_FIELD), + ), + ), + '#options' => array( + COUNTRIES_ENABLED => t('Enabled countries only'), + COUNTRIES_DISABLED => t('Disabled countries only'), + COUNTRIES_ALL => t('Both'), + ), + '#default_value' => $this->options['filter']['enabled'], + ); + + $form['filter']['continents'] = array( + '#type' => 'checkboxes', + '#title' => t('Filter by continent'), + '#description' => t('If no continents are selected, this filter will not be used.'), + '#states' => array( + // Only show field when widget equals "custom" + 'visible' => array( + ':input[name="options[configuration]"]' => array('value' => COUNTRIES_VIEWS_WIDGET_FIELD), + ), + ), + '#options' => countries_get_continents(), + '#element_validate' => array('_countries_element_array_filter'), + '#default_value' => $this->options['filter']['continents'], + ); + } + + function get_value_options() { + $options = array(); + switch ($this->options['configuration']) { + case COUNTRIES_VIEWS_WIDGET_FIELD: + if (isset($this->options['relationship'])) { + if ($field = field_info_field(substr($this->options['relationship'], 0, -5))) { + $options = $field['settings']; + } + } + break; + case COUNTRIES_VIEWS_WIDGET_CUSTOM: + $options = $this->options['filter']; + break; + } + + $property = (isset($this->definition['property'])) ? $this->definition['property'] : 'name'; + $this->value_options = countries_filter(countries_get_countries($property), $options); + } +} -- 1.7.4.1