diff --git a/_bootstrap/index.php b/_bootstrap/index.php index 285efbe..939385a 100644 --- a/_bootstrap/index.php +++ b/_bootstrap/index.php @@ -95,6 +95,15 @@ ], 'name', true)) { echo "Error creating MagicPreview Plugin.\n"; } + +if (!createObject('modSnippet', [ + 'name' => 'mpField', + 'description' => 'Returns click-to-field data attributes for a resource field or TV.', + 'static' => true, + 'static_file' => $componentPath.'/core/components/magicpreview/elements/snippets/mpfield.snippet.php', +], 'name', true)) { + echo "Error creating mpField snippet.\n"; +} $vcPlugin = $modx->getObject('modPlugin', ['name' => 'MagicPreview']); if ($vcPlugin) { if (!createObject('modPluginEvent', [ @@ -127,7 +136,7 @@ } if (!createObject('modPluginEvent', [ 'pluginid' => $vcPlugin->get('id'), - 'event' => 'OnWebPagePrerender', + 'event' => 'ContentBlocks_BeforeParse', 'priority' => 0, ], ['pluginid','event'], false)) { echo "Error creating modPluginEvent.\n"; @@ -139,6 +148,24 @@ ], ['pluginid','event'], false)) { echo "Error creating modPluginEvent.\n"; } + + // Drop event registrations older versions created that are no longer + // handled. Mirrors _build/resolvers/staleevents.resolver.php, which does + // the same on package upgrade -- keep the two lists in step. + $staleEvents = [ + 'OnWebPagePrerender', + 'OnWebPageComplete', + ]; + foreach ($staleEvents as $staleEvent) { + $stalePluginEvent = $modx->getObject('modPluginEvent', [ + 'pluginid' => $vcPlugin->get('id'), + 'event' => $staleEvent, + ]); + if ($stalePluginEvent) { + $stalePluginEvent->remove(); + echo "Removed stale modPluginEvent {$staleEvent}.\n"; + } + } } diff --git a/_build/build.transport.php b/_build/build.transport.php index ec048a1..dab90cb 100644 --- a/_build/build.transport.php +++ b/_build/build.transport.php @@ -23,7 +23,7 @@ function getSnippetContent($filename = '') { /* define version */ define('PKG_NAME','MagicPreview'); define('PKG_NAME_LOWER',strtolower(PKG_NAME)); - define('PKG_VERSION','1.7.1'); + define('PKG_VERSION','1.8.0'); define('PKG_RELEASE','pl'); /* load modx */ @@ -89,6 +89,10 @@ function getSnippetContent($filename = '') { 'type' => 'php', 'source' => $sources['resolvers'] . 'customevents.resolver.php', ], + [ + 'type' => 'php', + 'source' => $sources['resolvers'] . 'staleevents.resolver.php', + ], ] ] ); @@ -142,6 +146,21 @@ function getSnippetContent($filename = '') { $modx->log(modX::LOG_LEVEL_INFO,'Packaged in '.count($plugins).' plugins.'); flush(); unset($plugins,$plugin,$attributes); +/* add snippets */ +$snippets = include $sources['data'] . 'transport.snippets.php'; +if (!is_array($snippets)) { $modx->log(modX::LOG_LEVEL_FATAL,'Adding snippets failed.'); } +$attributes= [ + xPDOTransport::UNIQUE_KEY => 'name', + xPDOTransport::PRESERVE_KEYS => false, + xPDOTransport::UPDATE_OBJECT => true, +]; +foreach ($snippets as $snippet) { + $vehicle = $builder->createVehicle($snippet, $attributes); + $builder->putVehicle($vehicle); +} +$modx->log(modX::LOG_LEVEL_INFO,'Packaged in '.count($snippets).' snippets.'); flush(); +unset($snippets,$snippet,$attributes); + /* now pack in the license file, readme and setup options */ $builder->setPackageAttributes([ diff --git a/_build/data/transport.snippets.php b/_build/data/transport.snippets.php new file mode 100644 index 0000000..7f87c7b --- /dev/null +++ b/_build/data/transport.snippets.php @@ -0,0 +1,13 @@ +newObject('modSnippet'); +$snippets[0]->fromArray([ + 'id' => 1, + 'name' => 'mpField', + 'description' => 'Returns click-to-field data attributes for a resource field or TV.', + 'snippet' => getSnippetContent($sources['snippets'] . 'mpfield.snippet.php'), + 'category' => 0, +], '', true, true); + +return $snippets; diff --git a/_build/events/events.magicpreview.php b/_build/events/events.magicpreview.php index 2d87a8e..3aa0d85 100644 --- a/_build/events/events.magicpreview.php +++ b/_build/events/events.magicpreview.php @@ -7,7 +7,7 @@ 'OnDocFormSave', 'OnLoadWebDocument', 'OnManagerPageBeforeRender', - 'OnWebPagePrerender', + 'ContentBlocks_BeforeParse', 'ContentBlocks_AfterParse', ]; diff --git a/_build/resolvers/staleevents.resolver.php b/_build/resolvers/staleevents.resolver.php new file mode 100644 index 0000000..909d76b --- /dev/null +++ b/_build/resolvers/staleevents.resolver.php @@ -0,0 +1,41 @@ + false, so + * dropping an event from the package does not delete an existing row. Without + * this, an upgraded install keeps firing the MagicPreview plugin on every + * front-end page render for OnWebPagePrerender, which no longer has a handler. + * + * @var modX $modx + * @var modTransportPackage $transport + * @var array $options + */ +if ($transport->xpdo) { + $modx = $transport->xpdo; + + $staleEvents = [ + 'OnWebPagePrerender', + 'OnWebPageComplete', + ]; + + switch ($options[xPDOTransport::PACKAGE_ACTION]) { + case xPDOTransport::ACTION_UPGRADE: + $plugin = $modx->getObject('modPlugin', ['name' => 'MagicPreview']); + if ($plugin) { + foreach ($staleEvents as $eventName) { + $pluginEvent = $modx->getObject('modPluginEvent', [ + 'pluginid' => $plugin->get('id'), + 'event' => $eventName, + ]); + if ($pluginEvent) { + $pluginEvent->remove(); + } + } + } + + break; + } +} +return true; diff --git a/core/components/magicpreview/docs/changelog.txt b/core/components/magicpreview/docs/changelog.txt index efa262d..b7942d9 100644 --- a/core/components/magicpreview/docs/changelog.txt +++ b/core/components/magicpreview/docs/changelog.txt @@ -1,3 +1,15 @@ +MagicPreview 1.8.0-pl +--------------------- +Released on 2026-08-14 + +- Fix click_to_field breaking Fenom (and any other custom parser) output in previews. MagicPreview no longer replaces the MODX parser during a preview render. (#55) +- BREAKING: core resource fields and TVs are no longer marked clickable automatically. Mark them in your template with the new mpField snippet, e.g.