diff --git a/includes/base.inc b/includes/base.inc index b944cf61..b2c8622a 100644 --- a/includes/base.inc +++ b/includes/base.inc @@ -58,6 +58,28 @@ public function option_definition() { return array(); } + /** + * Collect this handler's option definition and alter them, ready for use. + * + * @return array + * Returns the options of this handler/plugin after allowing for alters. + * + * @see hook_views_plugin_option_definition_alter() + * @see hook_views_handler_option_definition_alter() + */ + function altered_option_definition() { + $definition = $this->option_definition(); + if (!empty($this->is_plugin)) { + // Trigger hook_views_plugin_option_definition_alter(). + drupal_alter('views_plugin_option_definition', $definition, $this); + } + else { + // Trigger hook_views_handler_option_definition_alter(). + drupal_alter('views_handler_option_definition', $definition, $this); + } + return $definition; + } + /** * Views handlers use a special construct function. * @@ -85,7 +107,7 @@ public function options(&$options) { * that will likely disappear at some point. */ public function set_default_options() { - $this->_set_option_defaults($this->options, $this->option_definition()); + $this->_set_option_defaults($this->options, $this->altered_option_definition()); // Retained for complex defaults plus backward compatibility. $this->options($this->options); @@ -119,7 +141,7 @@ public function unpack_options(&$storage, $options, $definition = NULL, $all = T } if (!isset($definition)) { - $definition = $this->option_definition(); + $definition = $this->altered_option_definition(); } if (!empty($this->view)) { @@ -222,7 +244,7 @@ public function destroy() { */ public function export_options($indent, $prefix) { $output = ''; - foreach ($this->option_definition() as $option => $definition) { + foreach ($this->altered_option_definition() as $option => $definition) { $output .= $this->export_option($indent, $prefix, $this->options, $option, $definition, array()); } @@ -297,7 +319,7 @@ public function export_option_always($indent, $prefix, $storage, $option, $defin * Unpacks each handler to store translatable texts. */ public function unpack_translatables(&$translatable, $parents = array()) { - foreach ($this->option_definition() as $option => $definition) { + foreach ($this->altered_option_definition() as $option => $definition) { $this->unpack_translatable($translatable, $this->options, $option, $definition, $parents, array()); } } diff --git a/views.api.php b/views.api.php index 22600e8a..feab4435 100644 --- a/views.api.php +++ b/views.api.php @@ -720,6 +720,62 @@ function hook_views_plugins_alter(&$plugins) { $plugins['row']['node']['base'][] = 'apachesolr'; } +/** + * Alter existing plugin option definitions. + * + * This can be used to edit default or add new option definitions to existing + * plugins. The reason for doing this is that only overriding the relevent form + * with hook_form_alter() is insufficent because submitted form values will be + * ignored if they haven't been declared as an available option. + * + * An alternative approach you could also take is to extend each plugin + * individually. However if your goal is to override many, or even all plugins, + * this results in a lot of additional code and files. This makes it a lot more + * troublesome to maintain the codebase, as well as interoperability with other + * modules. + * + * @param array $options + * The option definitions to be altered. + * @param $plugin + * A views object of the plugin where the default options are defined. + * + * @see views_object::option_definition() + * @see hook_views_handler_option_definition_alter() + * @see hook_form_alter() + */ +function hook_views_plugin_option_definition_alter(&$options, $plugin) { + // Add a new option definition. + $options['option_name'] = array('default' => ''); +} + +/** + * Alter existing handler option definitions. + * + * This can be used to edit default or add new option definitions to existing + * handers. The reason for doing this is that only overriding the relevent form + * with hook_form_alter() is insufficent because submitted form values will be + * ignored if they haven't been declared as an available option. + * + * An alternative approach you could also take is to extend each handler + * individually. However if your goal is to override many, or even all handlers, + * this results in a lot of additional code and files. This makes it a lot more + * troublesome to maintain the codebase, as well as interoperability with other + * modules. + * + * @param array $options + * The option definitions to be altered. + * @param $handler + * A views object of the handler where the default options are defined. + * + * @see views_handler::option_definition() + * @see hook_views_plugin_option_definition_alter() + * @see hook_form_alter() + */ +function hook_views_handler_option_definition_alter(&$options, $handler) { + // Add a new option definition. + $options['option_name'] = array('default' => ''); +} + /** * Register View API information. *