From 84e57677867f643163c54ae2a0cb9573d0d7b310 Mon Sep 17 00:00:00 2001 From: Herb v/d Dool Date: Fri, 13 Dec 2024 16:30:10 -0500 Subject: [PATCH] Convert variables to config --- facetapi_pretty_paths.install | 21 ++++++ facetapi_pretty_paths.module | 69 ++++++++++++++----- ...retty_paths_adapter_base_path_provider.inc | 3 +- ...cetapi_pretty_paths_base_path_provider.inc | 3 +- ...retty_paths_default_base_path_provider.inc | 3 +- .../facetapi_pretty_paths_coder_default.inc | 36 ++++++---- .../facetapi_pretty_paths_coder_taxonomy.inc | 8 +-- ...i_pretty_paths_coder_taxonomy_pathauto.inc | 27 ++++---- .../facetapi/url_processor_pretty_paths.inc | 61 +++++++++++----- 9 files changed, 164 insertions(+), 67 deletions(-) create mode 100644 facetapi_pretty_paths.install diff --git a/facetapi_pretty_paths.install b/facetapi_pretty_paths.install new file mode 100644 index 0000000..e8bebc4 --- /dev/null +++ b/facetapi_pretty_paths.install @@ -0,0 +1,21 @@ +set('searcher.' . $info['name'] . '.enabled', update_variable_get($id, ($info['url processor'] == 'pretty_paths'))); + $config->set('searcher.' . $info['name'] . '.options', update_variable_get($id . '_options', array())); + + update_variable_del($id); + update_variable_del($id . '_options'); + } + $config->save(); +} diff --git a/facetapi_pretty_paths.module b/facetapi_pretty_paths.module index 6cdfc81..19affe8 100644 --- a/facetapi_pretty_paths.module +++ b/facetapi_pretty_paths.module @@ -20,6 +20,17 @@ function facetapi_pretty_paths_autoload_info() { ); } +/** + * Implements hook_config_info(). + */ +function facetapi_pretty_paths_config_info() { + $prefixes['facetapi_pretty_paths.settings'] = array( + 'label' => t('Facet API Pretty Paths settings'), + 'group' => t('Configuration'), + ); + return $prefixes; +} + /** * Implements hook_facetapi_url_processors(). */ @@ -43,10 +54,10 @@ function facetapi_pretty_paths_facetapi_url_processors() { * Implements hook_facetapi_searcher_info(). */ function facetapi_pretty_paths_facetapi_searcher_info_alter(array &$searcher_info) { + $config = config('facetapi_pretty_paths.settings'); foreach ($searcher_info as &$info) { // Activate pretty paths optionally per searcher, as configured. - $id = 'facetapi_pretty_paths_searcher_' . $info['name']; - $info['url processor'] = variable_get($id) ? 'pretty_paths' : 'standard'; + $info['url processor'] = $config->get('searcher.' . $info['name'] . '.enabled') ? 'pretty_paths' : 'standard'; $info['facetapi pretty paths coder'] = 'default'; } } @@ -79,21 +90,19 @@ function facetapi_pretty_paths_form_facetapi_facet_display_form_alter(&$form, &$ array('@taxonomy_segment' => '/-')), ); - // Taxonomy Pathauto Coder settings. + // Taxonomy Path pattern Coder settings. - // 1. Check if pathauto is enabled. - // 2. Check for Apache Solr taxonomy term fields. - // 3. Check for Search API taxonomy term fields. - if (module_exists('pathauto') && - ((!empty($facet['map options']['module_name']) && $facet['map options']['module_name'] == 'Taxonomy') || - (!empty($facet['field type']) && $facet['field type'] == 'taxonomy_term'))) { + // 1. Check for Apache Solr taxonomy term fields. + // 2. Check for Search API taxonomy term fields. + if ((!empty($facet['map options']['module_name']) && $facet['map options']['module_name'] == 'Taxonomy') || + (!empty($facet['field type']) && $facet['field type'] == 'taxonomy_term')) { $facet_settings = $adapter->getFacetSettingsGlobal($facet); $form['global']['pretty_paths_taxonomy_pathauto'] = array( '#type' => 'checkbox', '#title' => t('Reuse term aliases'), '#default_value' => !empty($facet_settings->settings['pretty_paths_taxonomy_pathauto']) ? TRUE : FALSE, - '#description' => t('If set, the term alias from the pathauto settings will be reused, which avoids term ids in the facet aliases. This setting only works if the default taxonomy path pattern \'[term:vocabulary]/[term:name]\' is used.'), + '#description' => t('If set, the term alias from the path pattern settings will be reused, which avoids term ids in the facet aliases. This setting only works if the default taxonomy path pattern \'[term:vocabulary]/[term:name]\' is used.'), ); $options = array(); foreach (taxonomy_get_vocabularies() as $voc) { @@ -160,7 +169,7 @@ function facetapi_pretty_paths_menu() { $items['admin/config/search/facetapi_pretty_paths'] = array( 'title' => 'FacetAPI Pretty Paths', 'description' => 'Configure pretty paths settings', - 'page callback' => 'drupal_get_form', + 'page callback' => 'backdrop_get_form', 'page arguments' => array('facetapi_pretty_paths_admin_form'), 'access arguments' => array('administer facetapi pretty paths'), 'type' => MENU_NORMAL_ITEM, @@ -184,23 +193,27 @@ function facetapi_pretty_paths_permission() { */ function facetapi_pretty_paths_admin_form($form, &$form_state) { // Allow to enable / disable pretty paths per searcher. - $searcher_info = facetapi_get_searcher_info(); + $config = config('facetapi_pretty_paths.settings'); + $form['#config'] = 'facetapi_pretty_paths.settings'; + $form['#tree'] = TRUE; $form['searcher'] = array( '#type' => 'fieldset', '#title' => t('Enable pretty paths per searcher'), '#type' => 'fieldset', ); foreach (facetapi_get_searcher_info() as $info) { - $id = 'facetapi_pretty_paths_searcher_' . $info['name']; + $id = $info['name']; + $searcher_config = $config->get('searcher.' . $id); + $enabled = ($searcher_config == NULL) ? ($info['url processor'] == 'pretty_paths') : $searcher_config['enabled']; // Add a checkbox to enable pretty paths per searcher. $form['searcher'][$id] = array( '#type' => 'checkbox', '#title' => t('@searcher', array('@searcher' => $info['name'])), - '#default_value' => variable_get($id, $info['url processor'] == 'pretty_paths'), + '#default_value' => $enabled, '#description' => t("Use pretty paths for the @searcher_label", array("@searcher_label" => $info['label'])), ); // An additional fieldset provides additional options per searcher. - $options = variable_get($id . '_options'); + $options = $searcher_config['options']; $form['searcher'][$id . '_options'] = array( '#type' => 'fieldset', '#title' => t('Options for @searcher', array('@searcher' => $info['name'])), @@ -232,7 +245,31 @@ function facetapi_pretty_paths_admin_form($form, &$form_state) { } $form['searcher'][$id . '_options']['base_path_provider'] = $base_path_providers_form_item; } - return system_settings_form($form); + // return system_settings_form($form); + $form['actions']['#type'] = 'actions'; + $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration')); + + return $form; +} + +/** + * Submit handler for facetapi_pretty_paths_admin_form(). + */ +function facetapi_pretty_paths_admin_form_submit($form, &$form_state) { + $values = $form_state['values']; + $config = config('facetapi_pretty_paths.settings'); + foreach ($values['searcher'] as $key => $value) { + if (strrpos($key, '_options') !== FALSE) { + $key = substr($key, 0, -8); + $config->set('searcher.' . $key . '.options', $value); + } + else { + $config->set('searcher.' . $key . '.enabled', $value); + } + } + $config->save(); + + backdrop_set_message(t('The configuration options have been saved.')); } /** diff --git a/plugins/base_path_provider/facetapi_pretty_paths_adapter_base_path_provider.inc b/plugins/base_path_provider/facetapi_pretty_paths_adapter_base_path_provider.inc index da67780..624f942 100644 --- a/plugins/base_path_provider/facetapi_pretty_paths_adapter_base_path_provider.inc +++ b/plugins/base_path_provider/facetapi_pretty_paths_adapter_base_path_provider.inc @@ -11,8 +11,7 @@ class FacetApiPrettyPathsAdapterBasePathProvider implements FacetApiPrettyPathsBasePathProvider { /** - * @param FacetapiUrlProcessorPrettyPaths $urlProcessor - * @return base path. + * @inheritdoc */ public function getBasePath(FacetapiUrlProcessorPrettyPaths $urlProcessor) { return $urlProcessor->getAdapter()->getSearchPath(); diff --git a/plugins/base_path_provider/facetapi_pretty_paths_base_path_provider.inc b/plugins/base_path_provider/facetapi_pretty_paths_base_path_provider.inc index 6c7d8b7..3f5f116 100644 --- a/plugins/base_path_provider/facetapi_pretty_paths_base_path_provider.inc +++ b/plugins/base_path_provider/facetapi_pretty_paths_base_path_provider.inc @@ -12,7 +12,8 @@ interface FacetApiPrettyPathsBasePathProvider { /** * @param FacetapiUrlProcessorPrettyPaths $urlProcessor - * @return base path. + * @return string + * base path. */ public function getBasePath(FacetapiUrlProcessorPrettyPaths $urlProcessor); diff --git a/plugins/base_path_provider/facetapi_pretty_paths_default_base_path_provider.inc b/plugins/base_path_provider/facetapi_pretty_paths_default_base_path_provider.inc index 7834041..e2af57d 100644 --- a/plugins/base_path_provider/facetapi_pretty_paths_default_base_path_provider.inc +++ b/plugins/base_path_provider/facetapi_pretty_paths_default_base_path_provider.inc @@ -11,8 +11,7 @@ class FacetApiPrettyPathsDefaultBasePathProvider implements FacetApiPrettyPathsBasePathProvider { /** - * @param FacetapiUrlProcessorPrettyPaths $urlProcessor - * @return base path. + * @inheritdoc */ public function getBasePath(FacetapiUrlProcessorPrettyPaths $urlProcessor) { return $urlProcessor->getPathWithoutSegments(); diff --git a/plugins/coders/facetapi_pretty_paths_coder_default.inc b/plugins/coders/facetapi_pretty_paths_coder_default.inc index 2491886..5c68a58 100644 --- a/plugins/coders/facetapi_pretty_paths_coder_default.inc +++ b/plugins/coders/facetapi_pretty_paths_coder_default.inc @@ -13,23 +13,30 @@ class FacetApiPrettyPathsCoderDefault { /** * Encode a path segment in order to output it as part of a pretty path. * - * @param array $args An associative array containing: - * - segment: The path segment to encode. - * - facet: The facet that triggered the segment. + * @param array $args + * An associative array containing: + * - segment: The path segment to encode. + * - facet: The facet that triggered the segment. + * @return array */ public function encodePathSegment(array $args) { // Default: / $args['segment']['value'] = str_replace('/', '%2F', $args['segment']['value']); $args['segment']['value'] = str_replace('.', '%2E', $args['segment']['value']); $args['segment']['alias'] = rawurlencode($args['segment']['alias']); + + return $args; } /** * Decode the alias part of a path segment in order * to interpret it from a given pretty path. * - * @param array $args An associative array containing: - * - alias: The alias to decode. + * @param array $args + * An associative array containing: + * - alias: The alias to decode. + * @return string + * Decoded alias */ public function decodePathSegmentAlias(array $args) { $alias = $args['alias']; @@ -43,9 +50,12 @@ class FacetApiPrettyPathsCoderDefault { * Decode the value part of a path segment in order * to interpret it from a given pretty path. * - * @param array $args An associative array containing: - * - segment: The path segment to decode. - * - facet: The facet that is related to the path segment. + * @param array $args + * An associative array containing: + * - segment: The path segment to decode. + * - facet: The facet that is related to the path segment. + * @return string + * Decoded path value */ public function decodePathSegmentValue(array $args) { $value = $args['value']; @@ -56,14 +66,12 @@ class FacetApiPrettyPathsCoderDefault { /** * Convert a given text to a pretty path using pathauto, if available. + * + * @param string $text + * @return string */ protected function prettyPath($text) { // @todo: Make this pluggable? - if (module_exists('pathauto')) { - // Needed, as of http://drupal.org/node/907578#comment-5564008 - require_once drupal_get_path('module', 'pathauto') . '/pathauto.inc'; - return pathauto_cleanstring($text); - } - return $text; + return path_clean_string($text); } } diff --git a/plugins/coders/facetapi_pretty_paths_coder_taxonomy.inc b/plugins/coders/facetapi_pretty_paths_coder_taxonomy.inc index c2aa454..647843e 100644 --- a/plugins/coders/facetapi_pretty_paths_coder_taxonomy.inc +++ b/plugins/coders/facetapi_pretty_paths_coder_taxonomy.inc @@ -11,9 +11,9 @@ class FacetApiPrettyPathsCoderTaxonomy extends FacetApiPrettyPathsCoderDefault { /** - * Taxonomy special case: /- + * @inheritdoc * - * @see FacetApiPrettyPathsCoderDefault::encodePathSegment() + * Taxonomy special case: /- */ public function encodePathSegment(array $args) { if ($term = taxonomy_term_load($args['segment']['value'])) { @@ -23,9 +23,9 @@ class FacetApiPrettyPathsCoderTaxonomy extends FacetApiPrettyPathsCoderDefault { } /** - * Taxonomy special case: /- + * @inheritdoc * - * @see FacetApiPrettyPathsCoderDefault::decodePathSegmentValue() + * Taxonomy special case: /- */ public function decodePathSegmentValue(array $args) { $exploded = explode('-', $args['value']); diff --git a/plugins/coders/facetapi_pretty_paths_coder_taxonomy_pathauto.inc b/plugins/coders/facetapi_pretty_paths_coder_taxonomy_pathauto.inc index 1cb8ed7..f2a8b0f 100644 --- a/plugins/coders/facetapi_pretty_paths_coder_taxonomy_pathauto.inc +++ b/plugins/coders/facetapi_pretty_paths_coder_taxonomy_pathauto.inc @@ -17,9 +17,9 @@ class FacetApiPrettyPathsCoderTaxonomyPathauto extends FacetApiPrettyPathsCoderDefault { /** - * Taxonomy pathauto special case: / + * @inhericdoc * - * @see FacetApiPrettyPathsCoderDefault::encodePathSegment() + * Taxonomy pathauto special case: / */ public function encodePathSegment(array $args) { $voc_alias = $this->getVocabularyPathAlias($args['facet'], $args['adapter']); @@ -27,7 +27,7 @@ class FacetApiPrettyPathsCoderTaxonomyPathauto extends FacetApiPrettyPathsCoderD if ($term = taxonomy_term_load($args['segment']['value'])) { // Get the alias ([term:vocabulary]/[term:name]) for this term and // extract the term:name part. - $alias = drupal_lookup_path('alias', 'taxonomy/term/' . $term->tid); + $alias = backdrop_lookup_path('alias', 'taxonomy/term/' . $term->tid); if ($alias) { $parts = explode('/', $alias); if (count($parts) == 2) { @@ -40,16 +40,16 @@ class FacetApiPrettyPathsCoderTaxonomyPathauto extends FacetApiPrettyPathsCoderD } /** - * Taxonomy pathauto special case: / + * @inheritdoc * - * @see FacetApiPrettyPathsCoderDefault::decodePathSegmentValue() + * Taxonomy pathauto special case: / */ public function decodePathSegmentValue(array $args) { $voc_alias = $this->getVocabularyPathAlias($args['facet'], $args['adapter']); if ($voc_alias && $args['value'] != '!') { // Rebuild the term alias, get the source (taxonomy/term/[term:tid]) and // extract the term id. - $source = drupal_lookup_path('source', $voc_alias . '/' . $args['value']); + $source = backdrop_lookup_path('source', $voc_alias . '/' . $args['value']); if ($source) { $exploded = explode('/', $source); if (count($exploded) == 3) { @@ -67,17 +67,20 @@ class FacetApiPrettyPathsCoderTaxonomyPathauto extends FacetApiPrettyPathsCoderD /** * Helper function that returns the path alias for a vocabulary. + * + * @param array $facet_info + * @param FacetapiAdapter $adapter + * + * @return string|false */ - private function getVocabularyPathAlias($facet_info, $adapter) { + private function getVocabularyPathAlias($facet_info, FacetapiAdapter $adapter) { static $aliases = array(); if (!isset($aliases[$facet_info['name']])) { $aliases[$facet_info['name']] = FALSE; $facet_settings = $adapter->getFacetSettingsGlobal($facet_info); - $voc = taxonomy_vocabulary_machine_name_load($facet_settings->settings['pretty_paths_taxonomy_pathauto_vocabulary']); - if ($voc && module_exists('pathauto')) { - // Needed, as of http://drupal.org/node/907578#comment-5564008 - require_once drupal_get_path('module', 'pathauto') . '/pathauto.inc'; - $aliases[$facet_info['name']] = pathauto_cleanstring($voc->name); + $voc = taxonomy_vocabulary_load($facet_settings->settings['pretty_paths_taxonomy_pathauto_vocabulary']); + if ($voc) { + $aliases[$facet_info['name']] = path_clean_string($voc->name); } } return $aliases[$facet_info['name']]; diff --git a/plugins/facetapi/url_processor_pretty_paths.inc b/plugins/facetapi/url_processor_pretty_paths.inc index f4b81a9..8c1f76c 100644 --- a/plugins/facetapi/url_processor_pretty_paths.inc +++ b/plugins/facetapi/url_processor_pretty_paths.inc @@ -13,14 +13,14 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { /** * An array of filter params. * - * @var array. + * @var array */ protected $filterParams = array(); /** * An array of pretty path segments. * - * @var array. + * @var array */ protected $pathSegments = array(); @@ -33,16 +33,22 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { /** * Options that apply for this FacetapiUrlProcessor. + * + * @var array */ protected $options = array(); /** * The full path string, as we override $_GET['q]. + * + * @var string|null */ protected $fullPath = NULL; /** * The base path for constructing pretty facet paths. + * + * @var string|null */ protected $pathWithoutSegments = NULL; @@ -54,23 +60,19 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { protected $basePathProvider; /** - * Constructor, sets adapter. - * - * @param FacetapiAdapter $adapter - * The adapter that uses this FacetapiUrlProcessor. + * @inheritdoc */ function __construct(FacetapiAdapter $adapter) { + $config = config('facetapi_pretty_paths.settings'); $this->adapter = $adapter; - $this->options = variable_get('facetapi_pretty_paths_searcher_' . $this->adapter->getSearcher() . '_options'); + $this->options = $config->get('searcher.' . $this->adapter->getSearcher() . '.options'); $base_path_provider = isset($this->options['base_path_provider']) ? $this->options['base_path_provider'] : 'default'; $class = plugin_manager_load_class('facetapi_pretty_paths', 'base_path_provider', $base_path_provider, 'handler'); $this->basePathProvider = new $class; } /** - * Pulls facet params from the $_GET variable. - * - * Overrides FacetapiUrlProcessorStandard::fetchParams(). + * @inheritdoc */ public function fetchParams() { @@ -139,9 +141,9 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { } /** - * Remove the filter query part from params and return it. + * @inheritdoc * - * Overrides FacetapiUrlProcessorStandard::getQueryString(). + * Remove the filter query part from params and return it. */ public function getQueryString(array $facet, array $values, $active) { $params = $this->params; @@ -150,12 +152,12 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { } /** + * @inheritdoc + * * Pretty paths will be generated as "search/url/segment1/segment2/". * * By default, a segment will look like: * "/". - * - * Overrides FacetapiUrlProcessorStandard::getFacetPath(). */ public function getFacetPath(array $facet, array $values, $active) { $segments = $this->pathSegments; @@ -209,6 +211,10 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { /** * Generate a path segment for a given facet + value. + * + * @param array $facet + * Passed by reference. + * @param string $value */ protected function getPathSegment(&$facet, $value) { $pretty_paths_alias = $this->getFacetPrettyPathsAlias($facet); @@ -220,6 +226,13 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { ); } + /** + * Encode path segment + * + * @param array $segment + * Passed by reference + * @param array $facet + */ protected function encodePathSegment(array &$segment, array $facet) { facetapi_pretty_paths_coder_callback('encodePathSegment', array( @@ -230,6 +243,12 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { ); } + /** + * Decode path segment alias + * + * @param string $alias + * @return array + */ protected function decodePathSegmentAlias($alias) { return facetapi_pretty_paths_coder_callback('decodePathSegmentAlias', array( @@ -238,6 +257,13 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { ); } + /** + * Decode path segment value + * + * @param string $value + * @param array $facet + * @return array + */ protected function decodePathSegmentValue($value, array $facet) { return facetapi_pretty_paths_coder_callback('decodePathSegmentValue', array( @@ -253,6 +279,9 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { * * If there is no custom pretty_paths_alias settings, in will default * to rawurlencode($facet['field alias']). + * + * @param array $facet + * @return string */ public function getFacetPrettyPathsAlias(array $facet) { $facet_settings = $this->adapter->getFacetSettingsGlobal($facet); @@ -265,7 +294,7 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { * Implements FacetapiUrlProcessorPrettyPaths::setBreadcrumb(). */ public function setBreadcrumb() { - $breadcrumb = drupal_get_breadcrumb(); + $breadcrumb = backdrop_get_breadcrumb(); // $facets to use later to get the segment $facets = $this->adapter->getEnabledFacets(); @@ -320,7 +349,7 @@ class FacetapiUrlProcessorPrettyPaths extends FacetapiUrlProcessorStandard { } // Sets the breadcrumb trail with h keys and filters. - drupal_set_breadcrumb($breadcrumb); + backdrop_set_breadcrumb($breadcrumb); } /**