Skip to content

Commit

Permalink
Convert variables to config
Browse files Browse the repository at this point in the history
  • Loading branch information
herbdool committed Dec 13, 2024
1 parent 5a0a262 commit 84e5767
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 67 deletions.
21 changes: 21 additions & 0 deletions facetapi_pretty_paths.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* @file
* Install, updates and uninstall functions.
*/

/**
* Migrate variables to config.
*/
function facetapi_pretty_paths_update_1000() {
$config = config('facetapi_pretty_paths.settings');
foreach (facetapi_get_searcher_info() as $info) {
$id = 'facetapi_pretty_paths_searcher_' . $info['name'];
$config->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();
}
69 changes: 53 additions & 16 deletions facetapi_pretty_paths.module
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*/
Expand All @@ -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';
}
}
Expand Down Expand Up @@ -79,21 +90,19 @@ function facetapi_pretty_paths_form_facetapi_facet_display_form_alter(&$form, &$
array('@taxonomy_segment' => '<alias>/<term-name>-<term-id>')),
);

// 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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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'])),
Expand Down Expand Up @@ -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.'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
class FacetApiPrettyPathsAdapterBasePathProvider implements FacetApiPrettyPathsBasePathProvider {

/**
* @param FacetapiUrlProcessorPrettyPaths $urlProcessor
* @return base path.
* @inheritdoc
*/
public function getBasePath(FacetapiUrlProcessorPrettyPaths $urlProcessor) {
return $urlProcessor->getAdapter()->getSearchPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ interface FacetApiPrettyPathsBasePathProvider {

/**
* @param FacetapiUrlProcessorPrettyPaths $urlProcessor
* @return base path.
* @return string
* base path.
*/
public function getBasePath(FacetapiUrlProcessorPrettyPaths $urlProcessor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
class FacetApiPrettyPathsDefaultBasePathProvider implements FacetApiPrettyPathsBasePathProvider {

/**
* @param FacetapiUrlProcessorPrettyPaths $urlProcessor
* @return base path.
* @inheritdoc
*/
public function getBasePath(FacetapiUrlProcessorPrettyPaths $urlProcessor) {
return $urlProcessor->getPathWithoutSegments();
Expand Down
36 changes: 22 additions & 14 deletions plugins/coders/facetapi_pretty_paths_coder_default.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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: <alias>/<value>
$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'];
Expand All @@ -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'];
Expand All @@ -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);
}
}
8 changes: 4 additions & 4 deletions plugins/coders/facetapi_pretty_paths_coder_taxonomy.inc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
class FacetApiPrettyPathsCoderTaxonomy extends FacetApiPrettyPathsCoderDefault {

/**
* Taxonomy special case: <alias>/<term-name>-<term-id>
* @inheritdoc
*
* @see FacetApiPrettyPathsCoderDefault::encodePathSegment()
* Taxonomy special case: <alias>/<term-name>-<term-id>
*/
public function encodePathSegment(array $args) {
if ($term = taxonomy_term_load($args['segment']['value'])) {
Expand All @@ -23,9 +23,9 @@ class FacetApiPrettyPathsCoderTaxonomy extends FacetApiPrettyPathsCoderDefault {
}

/**
* Taxonomy special case: <alias>/<term-name>-<term-id>
* @inheritdoc
*
* @see FacetApiPrettyPathsCoderDefault::decodePathSegmentValue()
* Taxonomy special case: <alias>/<term-name>-<term-id>
*/
public function decodePathSegmentValue(array $args) {
$exploded = explode('-', $args['value']);
Expand Down
27 changes: 15 additions & 12 deletions plugins/coders/facetapi_pretty_paths_coder_taxonomy_pathauto.inc
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
class FacetApiPrettyPathsCoderTaxonomyPathauto extends FacetApiPrettyPathsCoderDefault {

/**
* Taxonomy pathauto special case: <facet alias>/<term-name alias>
* @inhericdoc
*
* @see FacetApiPrettyPathsCoderDefault::encodePathSegment()
* Taxonomy pathauto special case: <facet alias>/<term-name alias>
*/
public function encodePathSegment(array $args) {
$voc_alias = $this->getVocabularyPathAlias($args['facet'], $args['adapter']);
if ($voc_alias) {
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) {
Expand All @@ -40,16 +40,16 @@ class FacetApiPrettyPathsCoderTaxonomyPathauto extends FacetApiPrettyPathsCoderD
}

/**
* Taxonomy pathauto special case: <facet alias>/<term-name alias>
* @inheritdoc
*
* @see FacetApiPrettyPathsCoderDefault::decodePathSegmentValue()
* Taxonomy pathauto special case: <facet alias>/<term-name alias>
*/
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) {
Expand All @@ -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']];
Expand Down
Loading

0 comments on commit 84e5767

Please sign in to comment.