Skip to content

Instantly share code, notes, and snippets.

@Jako
Last active March 15, 2021 08:54
Show Gist options
  • Save Jako/8ad6a6976b1dd04791d267342704ab60 to your computer and use it in GitHub Desktop.
Save Jako/8ad6a6976b1dd04791d267342704ab60 to your computer and use it in GitHub Desktop.
Code patterns for Extras compatibility in MODX3 and MODX2

Code patterns for Extras compatibility in MODX3 and MODX2

Use a fqn prefix for modTransport classes:

$modx->getTableName('modTransportPackage')

to

$modx->getTableName('transport.modTransportPackage')

Replace retrieving the manager_language system setting with a $_SESSION based one:

$language = $this->modx->getOption('manager_language');

to

$language = $this->modx->getOption('manager_language', array(), $this->modx->getOption('manager_language', $_SESSION, 'en'));

If a processor class uses $this->classKey to avoid ambiguous column names, $this->classAlias can be set to the class name in the constructor:

    /**
     * @var string $classAlias The class alias
     */
    protected $classAlias;

    /**
     * Set the class alias on base of the class key for compatibility between 2.x/3.x
     * @param modX $modx
     * @param array $properties
     */
    public function __construct(modX &$modx, array $properties = array())
    {
        parent::__construct($modx, $properties);

        $this->classAlias = $this->classKey;
        if (strpos($this->classAlias, '\\') !== false) {
            $explodedAlias = explode('\\', $this->classAlias);
            $this->classAlias = array_pop($explodedAlias);
        }
    }

Later on $this->classKey has to be replaced with $this->classAlias to reference a column:

    /**
     * {@inheritDoc}
     * @param xPDOQuery $c
     * @return xPDOQuery
     */
    public function prepareQueryBeforeCount(xPDOQuery $c): xPDOQuery
    {
        $valuesQuery = $this->getProperty('valuesqry');
        $query = (!$valuesQuery) ? $this->getProperty('query') : '';
        if (!empty($query)) {
            $c->where([
                $this->classAlias . '.username:LIKE' => '%' . $query . '%',
                'Profile.fullname:LIKE' => '%' . $query . '%'
            ]);
        }
        $c->leftJoin('modUserProfile', 'Profile');
        $c->select($this->modx->getSelectColumns($this->classKey, $this->classAlias, '', [
            'id', 'username'
        ]));
        $c->select($this->modx->getSelectColumns('modUserProfile', 'Profile', 'profile_', [
            'id', 'fullname'
        ]));

        return $c;
    }

If a core processor class was extended and has to be work in both versions:

require_once MODX_PROCESSORS_PATH . 'security/user/getlist.class.php';

to

// Compatibility between 2.x/3.x
if (file_exists(MODX_PROCESSORS_PATH . 'security/user/getlist.class.php')) {
    require_once MODX_PROCESSORS_PATH . 'security/user/getlist.class.php';
} elseif (!class_exists('modContextGetListProcessor')) {
    class_alias(\MODX\Revolution\Processors\Security\User\GetList::class, \modUserGetListProcessor::class);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment