Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uncaught TypeError in explode() when using TaggerGetResourcesWhere with queryString #143

Open
flyinliamryan opened this issue May 14, 2024 · 0 comments

Comments

@flyinliamryan
Copy link

flyinliamryan commented May 14, 2024

File: /www/core/components/tagger/src/Utils.php Line 17

I encountered a PHP Fatal error: Uncaught TypeError in the Tagger component when using TaggerGetResourcesWhere in MODX Revo. The issue arises specifically when passing the output of [[!queryString:urldecode]] as the &tags parameter. I have done it this way many times before and not had any issue.

Error Details:

[14-May-2024 16:43:36 Europe/London] PHP Fatal error:  Uncaught TypeError: explode(): Argument #2 ($string) must be of type string, array given in /www/core/components/tagger/src/Utils.php:17
Stack trace:
#0 /www/core/components/tagger/src/Utils.php(17): explode()
#1 /www/core/components/tagger/src/Tagger.php(114): Tagger\Utils::explodeAndClean()
#2 /www/core/cache/includes/elements/modx/revolution/modsnippet/47.include.cache.php(208): Tagger\Tagger->getCurrentTags()
#3 /www/core/src/Revolution/modScript.php(88): include('...')
#4 /www/core/src/Revolution/modParser.php(508): MODX\Revolution\modScript->process()
#5 /www/core/components/pdotools/src/Parsing/Parser.php(276): MODX\Revolution\modParser->processTag()
#6 /www/core/src/Revolution/modParser.php(221): ModxPro\PdoTools\Parsing\Parser->processTag()
#7 /www/core/components/pdotools/src/Parsing/Parser.php(73): MODX\Revolution\modParser->processElementTags()
#8 /www/core/src/Revolution/modTemplate.php(147): ModxPro\PdoTools\Parsing\Parser->processElementTags()
#9 /www/core/src/Revolution/modResource.php(485): MODX\Revolution\modTemplate->process()
#10 /www/core/src/Revolution/modResource.php(465): MODX\Revolution\modResource->process()
#11 /www/core/src/Revolution/modResponse.php(72): MODX\Revolution\modResource->prepare()
#12 /www/core/src/Revolution/modRequest.php(154): MODX\Revolution\modResponse->outputContent()
#13 /www/core/src/Revolution/modRequest.php(138): MODX\Revolution\modRequest->prepareResponse()
#14 /www/core/src/Revolution/modX.php(1499): MODX\Revolution\modRequest->handleRequest()
#15 /www/index.php(63): MODX\Revolution\modX->handleRequest()
#16 {main}
  thrown in /www/core/components/tagger/src/Utils.php on line 17

Steps to Reproduce:

Use TaggerGetResourcesWhere in a snippet call with [[!queryString:urldecode]] as the &tags parameter.
Example usage:

[[!pdoPage:default=`<div class="col-12"><p class="no-results text-center">No resources found</p></div>`?
    &element=`pdoResources`
    &where=`[[!TaggerGetResourcesWhere? &tags=`[[!queryString:urldecode]]`]]`
    ...
]]

When the [[!queryString:urldecode]] output is replaced directly with a string like tag1,tag2 it works as expected.

Relevant Code:

The QueryString snippet used for processing tags is as follows:

<?php
$result = '';

// Check if 'tag' exists
if (isset($_GET['tag'])) {
    // Normalize 'tag' to an array if it's a string
    $tags = is_array($_GET['tag']) ? $_GET['tag'] : explode(",", $_GET['tag']);
    
    // Clean up the array by trimming whitespace and removing empty entries
    $tags = array_filter(array_map('trim', $tags), function($value) {
        return $value !== '';
    });

    // Implode the array into a comma-separated string if not empty
    if (!empty($tags)) {
        $result = implode(",", $tags);
    }
}

echo $result; // This ensures we are returning a string

How I got it to work:

I adjusted the explodeAndClean function to handle array inputs by converting them to strings before processing. Here is the updated function:

public static function explodeAndClean($input, $delimiter = ',', $keepZero = false)
{
    if (is_array($input)) {
        $input = implode($delimiter, $input);  // Ensure $input is a string
    }
    $array = explode($delimiter, $input);     // Now explode it into parts

    // Optionally clean the array
    $array = array_filter($array, function ($item) use ($keepZero) {
        return $keepZero ? $item !== '' : $item !== '' && $item !== '0';
    });

    return $array;
}

Additional Information:

MODX version: 3.0.4
Tagger version: 2.0.0
PHP version: 8.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant