I have a plugin that is supposed to calculate a “similarity score” between references based on template variables. While the iterator is pulling multiple resources, the first foreach is only run once. Then at the end the tv value isn’t set for the first iteration of the first foreach loop. I’m just trying to get it to run on OnDocFormSave first and then I’ll test the rest of the events. This is the plugin code:
<?php
$templateId = 6;
// Only proceed for resources with template ID 6 on add, update, delete, publish/unpublish
$eventTypes = ['OnDocFormSave', 'OnDocPublished', 'OnDocUnpublished', 'OnResourceDelete'];
if (in_array($modx->event->name, $eventTypes) && $resource->get('template') == $templateId) {
$found_resources = $modx->getIterator('modResource', [ 'template' => $templateId ]);
# Logs the right number of resources
$modx->log(1, $modx->getCount('modResource', [ 'template' => $templateId ]));
foreach($found_resources as $idx => $current_resource) {
$current_id = $current_resource->get('id');
$similar_resources = [];
$modx->log(1, "Processing resource with ID: $current_id");
foreach($found_resources as $resource) {
$id = $resource->get('id');
if ($current_id != $id) {
# This function is cut for brevity
$similarity_score = calculateSimilarity($current_resource, $resource);
$similar_resources[$id] = $similarity_score;
}
}
arsort($similar_resources);
$top_matches = array_slice(array_keys($similar_resources), 0, 2);
$match_values =implode(',', $top_matches);
$modx->log(1, $match_values);
$modx->log(1, print_r($similar_resources));
if (!$current_resource->setTVValue('matchValues', $match_values)) {
# This does not log
$modx->log(1, "Could not set matchValues to $match_values");
} else {
# pretty sure I don't need to do this, it isn't with or without this
$current_resource->save();
}
}
}