Automatically update the list of resources in Selections Collections in MODX Revolution admin panel

Automatically update the list of resources in Selections Collections in MODX Revolution admin panel

Let's see how to set up automatic updating of the list of resources in the Selections of the Collections extra, using a custom plugin in the MODX Revolution admin panel.

In MODX Revolution, the Collections package, which adds a table view of resources in the admin panel, has an extremely handy tool - Selections.

They are like a virtual Collection - you can view a list of resources in a Selections, and the resources can be children of other Collections or Resources at the moment.

The simplest use case is that you have products or articles organized into categories, and each category is a resource. In this case, you cannot view all products or articles in the admin table at the same time, but... This is where Selections come to the rescue.

Actually, to the essence of the problem. I was very surprised that there is no possibility of mass adding resources to the selection, and to bind 100 or even 1000 resources 1 at a time - thank you, I do not participate in this.

To solve this problem on wrote a simple plugin that when you save a resource in the admin checks:

  1. whether the resource is a Selection of Collections extra
  2. description field is filled in

In the description field we add an SQL condition to select the resources that should be in this Selection - everything between WHERE and ;.

An SQL query is executed to completely clean up the Selection, and then add resources based on the condition.

Probably not the most elegant and safe solution, but I do it for me, it works - and well. To improve security, you can, for example, hide the description field of Selection resources from the admin area and display it only to the admin. 

Plugin for auto-update

Create pl_collection_selection_update on OnDocFormSave event:

<?php
if ($modx->event->name != 'OnDocFormSave') {
	return;
}

if ($resource->get('class_key') == 'Collections\Model\SelectionContainer' && $resource->get('description') != "") {
	$table_prefix = $modx->getOption('table_prefix');
	$where = $resource->get('description'); // Template ID of resources to be added to the selection
	$collection_resource_id = $resource->get('id'); // ID selection resource
	
	// Delete all resources from this selection
	$sql = "DELETE FROM `".$table_prefix."collection_selections` 
	WHERE `collection` = ".$collection_resource_id;
	$modx->query($sql);
	// $modx->log(modX::LOG_LEVEL_ERROR, $sql);
	
	$modx->query("
	INSERT IGNORE INTO `".$table_prefix."collection_selections` 
		SELECT ".$collection_resource_id.", `id`, 1 
		FROM `".$table_prefix."site_content` 
		WHERE $where
	");
	
}

In the description field of the Selection, enter a SQL WHERE to select the resources to be added, for example:

`template` IN (2) AND `published` IN (0,1)

SQL queries

If you don't need to automatically fill the selection, you can fill it with a regular SQL query:

/* The table prefix needs to be changed if you don't have a standard one */
/* for example, 86 - selection id */
DELETE FROM `modx_collection_selections` 
	WHERE `collection` = 86;

INSERT IGNORE INTO `modx_collection_selections` 
	SELECT 86, `id`, 1 /* 1 - is the status of the resource in the selection */
	FROM `modx_site_content` 
	WHERE `template` = 1; /* you can write your condition here */

Or through the console, which is more convenient:

$table_prefix = $modx->getOption('table_prefix');
$template = 2; // Template ID of resources to be added to the selection
$collection_resource_id = 262; // ID of selection resource

// Delete all from this selection
$sql = "DELETE FROM `".$table_prefix."collection_selections` WHERE `collection` = ".$collection_resource_id;
$modx->query($sql);

$modx->query("
INSERT IGNORE INTO `".$table_prefix."collection_selections` 
	SELECT ".$collection_resource_id.", `id`, 1 
	FROM `".$table_prefix."site_content` 
	WHERE `template` = ".$template."  AND `published` = 1
");

I hope this article helps you cut down on time spent on work tasks! Have a good mind!

Please rate this article
(5 stars / 1 votes)