public static function BetterExposedFiltersHelper::rewriteOptions in Better Exposed Filters 8.4
Same name and namespace in other branches
- 8.5 src/BetterExposedFiltersHelper.php \Drupal\better_exposed_filters\BetterExposedFiltersHelper::rewriteOptions()
Rewrites a set of options given a string from the config form.
Rewrites should be specified, one per line, using the format old_string|new_string. If new_string is empty, the option will be removed.
Parameters
array $options: An array of key => value pairs that may be rewritten.
string $rewrite_settings: String representing the entry in the settings form.
bool $reorder: Reorder $options based on the rewrite settings.
Return value
array Rewritten $options.
5 calls to BetterExposedFiltersHelper::rewriteOptions()
- BetterExposedFiltersHelperUnitTest::testRewriteOptions in tests/
src/ Unit/ BetterExposedFiltersHelperUnitTest.php - Tests options are rewritten correctly.
- BetterExposedFiltersHelperUnitTest::testRewriteReorderOptions in tests/
src/ Unit/ BetterExposedFiltersHelperUnitTest.php - Tests options are rewritten correctly.
- BetterExposedFiltersHelperUnitTest::testRewriteTaxonomy in tests/
src/ Unit/ BetterExposedFiltersHelperUnitTest.php - Tests options are rewritten correctly.
- FilterWidgetBase::exposedFormAlter in src/
Plugin/ better_exposed_filters/ filter/ FilterWidgetBase.php - Manipulate views exposed from element.
- SortWidgetBase::exposedFormAlter in src/
Plugin/ better_exposed_filters/ sort/ SortWidgetBase.php - Manipulate views exposed from element.
File
- src/
BetterExposedFiltersHelper.php, line 26
Class
- BetterExposedFiltersHelper
- Defines a helper class for better exposed filters.
Namespace
Drupal\better_exposed_filtersCode
public static function rewriteOptions(array $options, $rewrite_settings, $reorder = FALSE) {
// Break out early if we don't have anything to rewrite.
if (empty($rewrite_settings) || !is_string($rewrite_settings)) {
return $options;
}
$rewrites = [];
$order = [];
$return = [];
// Get a copy of the option, flattened with their keys preserved.
$flat_options = self::flattenOptions($options, TRUE);
// Preserve order.
if (!$reorder) {
$order = array_keys($options);
}
$lines = explode("\n", trim($rewrite_settings));
foreach ($lines as $line) {
list($search, $replace) = array_map('trim', explode('|', $line));
if (!empty($search)) {
$rewrites[$search] = $replace;
// Find the key of the option we need to reorder.
if ($reorder) {
$key = array_search($search, $flat_options);
if ($key !== FALSE) {
$order[] = $key;
}
}
}
}
// Reorder options in the order they are specified in rewrites.
// Collect the keys to use later.
if ($reorder && !empty($order)) {
// Start with the items that were listed in the rewrite settings.
foreach ($order as $key) {
$return[$key] = $options[$key];
unset($options[$key]);
}
// Tack remaining options on the end.
$return += $options;
}
else {
$return = $options;
}
// Rewrite the option value.
foreach ($return as $index => &$choice) {
if (is_object($choice) && isset($choice->option)) {
$key = key($choice->option);
$value =& $choice->option[$key];
}
elseif (is_array($choice) && array_key_exists('name', $choice)) {
$value =& $choice['name'];
}
else {
$choice = (string) $choice;
$value =& $choice;
}
if (!is_scalar($value)) {
// We give up...
continue;
}
if (isset($rewrites[$value])) {
if ('' === $rewrites[$value]) {
unset($return[$index]);
}
else {
$value = $rewrites[$value];
}
}
}
return $return;
}