Custom groups processor

Gallery3x2 min readPage 20 of 22

The list of groups in the edit windows (images, videos, files, embeds) and in the editor insert window comes from the processor set in gallery3x.groups_processor. By default it is Gallery3x\Processors\GetGroupsCombined: all groups of all resources (respecting gallery3x.groups_per_user).

You need your own processor if the groups should differ, for example be separate for each resource. The processor receives the current resource's resource_id; the response is success with results (an array of ['name' => …]) and total.

Example: groups of the current resource only

File core/components/gallery3x/src/Processors/GetGroupsByResource.php:

<?php
namespace Gallery3x\Processors;

use Gallery3x\Model\Gallery3xAttachment;
use Gallery3x\Model\Gallery3xEmbed;
use Gallery3x\Model\Gallery3xFile;
use Gallery3x\Model\Gallery3xVideo;

class GetGroupsByResource extends GetGroupsCombined
{
    public function process()
    {
        $resourceId = (int)$this->getProperty('resource_id');
        $groups = [];
        $classes = [Gallery3xFile::class, Gallery3xVideo::class, Gallery3xAttachment::class, Gallery3xEmbed::class];
        foreach ($classes as $class) {
            $c = $this->modx->newQuery($class, ['resource_id' => $resourceId, 'group:!=' => '']);
            $c->select('group');
            if ($c->prepare() && $c->stmt->execute()) {
                foreach ($c->stmt->fetchAll(\PDO::FETCH_COLUMN) as $value) {
                    foreach (explode('||', (string)$value) as $g) {
                        if (($g = trim($g)) !== '') {
                            $groups[$g] = true;
                        }
                    }
                }
            }
        }
        $names = array_keys($groups);
        sort($names);
        $results = array_map(fn($name) => ['name' => $name], $names);
        return $this->success('', ['results' => $results, 'total' => count($results)]);
    }
}

Then in the system settings:

gallery3x.groups_processor = Gallery3x\Processors\GetGroupsByResource
  • Permissions are checked by the parent class (gallery3x_save), so extend GetGroupsCombined.
  • A class from the Gallery3x\Processors namespace in the component's src/Processors/ folder is loaded automatically. The file is not in the package, so a Gallery3x update will not delete it — but keep a copy.
  • You can specify a class from your own component in another namespace — it must be loadable by an autoloader.