feat(parser): JSON properties, [[~context]] links, [[@if]] conditional blocks#16904
Open
Ibochkarev wants to merge 2 commits intomodxcms:3.xfrom
Open
feat(parser): JSON properties, [[~context]] links, [[@if]] conditional blocks#16904Ibochkarev wants to merge 2 commits intomodxcms:3.xfrom
Ibochkarev wants to merge 2 commits intomodxcms:3.xfrom
Conversation
…l blocks - modParser: parsePropertyString() accepts JSON object/array for element properties - modParser: processConditionalBlocks() for [[@if (expr)]]...[[@else]]...[[@endif]] - modLinkTag: [[~contextKey]] resolves to context site_start URL (e.g. [[~web]]) - Add tests: testParsePropertiesJson, testConditionalBlocks, testLinkTagContextKey Resolves modxcms#15200
cf08f3a to
6a56d59
Compare
- Updated the assertion for context links to check for the presence of a path or query string in the generated URL, enhancing the test's reliability.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does it do?
JSON for element properties
modParser::parsePropertyString()now accepts a JSON object or array as the property string (e.g.{"prop":"value","num":1}or["a", "b"]). If the string starts with{or[, it is decoded withjson_decode()and returned in the same format as the classickey=value&key2=value2syntax (either values only or full property definitions with name/desc/type/options/value). Existing non-JSON property strings are unchanged.Link tag by context key
modLinkTagsupports[[~contextKey]](e.g.[[~web]]) in addition to[[~resourceId]]. When the link target is not numeric, it is treated as a context key: the parser loads that context, readssite_start, and builds the URL to that resource in the given context. If the context does not exist or has nosite_start, a warning is logged and the tag outputs empty. Resolution is encapsulated inresolveContextLinkTarget().Conditional blocks in content
New processing step in
processElementTags(): before collecting/processing other tags,processConditionalBlocks()runs. It finds[[@if (expr)]]… optional[[@else]]…[[@endif]]blocks, evaluates the expression (whitelist only:$modx->user->*,$modx->resource->*,empty(),isset(), comparisons with==,!=,<,>, etc., noeval()), and replaces the block with the “then” or “else” branch. Nested[[@if]]blocks are supported. Expressions are parsed and compared viaevaluateConditionalExpression()andparseConditionalComparisonValue().Tests
New/updated tests in
modParserTest:testParsePropertiesJson,testConditionalBlocks,testConditionalBlocksNoElse,testLinkTagContextKey.Why is it needed?
&prop=valuestrings; improves readability and tooling (editors, generators).[[~web]]) without hardcoding resource IDs; useful for multi-context sites and reusable templates.eval()for security.How to test
1. Unit tests (parser and link tag)
From the project root, run the modParser test group (requires configured test DB and fixtures; if fixtures fail, you can run only the new tests by name):
Expected:
testParsePropertiesJsonpasses (JSON{"a":1,"b":"two"}parsed to array);testConditionalBlocks/testConditionalBlocksNoElsepass (conditional output for$modx->user->id);testLinkTagContextKeypasses (e.g.[[~web]]becomes a URL string when contextwebexists and hassite_start, or remains unchanged/empty otherwise).2. JSON properties in the manager
{"title":"Home","count":5}.[[$chunk? &title=Home&count=5]]or as default values).title=Home&count=5— behavior should match.3. Link by context key
web) withsite_startset to a resource ID.[[~web]].site_startresource in that context (e.g./or friendly URL).[[~nonexistent]]); expect empty output and a warning in the log.4. Conditional blocks
[[@if ($modx->user->id == 1)]]Hello admin[[@else]]Hello guest[[@endif]]— output depends on current user id.[[@if (empty($modx->resource->longtitle))]][[+pagetitle]][[@else]][[+longtitle]][[@endif]]— output depends on resource field.[[@else]]:[[@if ($modx->user->id == 99999)]]hidden[[@endif]]— should output nothing when condition is false.[[@if (1 == 1)]]a[[@if ($modx->user->id == 1)]]b[[@else]]c[[@endif]]d[[@endif]]— should render “abd” or “acd” depending on user.5. PHPCS (optional)
modLinkTag.phpshould report 0 errors;modParser.phpmay still report existing style issues outside the added code.Related issue(s)/PR(s)
Resolves #15200