Skip to content

gsOnImportValues

Triggered after retrieving and initially processing the values. Here we can modify the data, perform additional filtering, or add new data.

Available variables:

NameDescription
valuesRetrieved data
objectImport object

modUser

If there is no email, generate one.

php
if ($modx->event->name === 'gsOnImportValues') {
    if ($object->model_class === 'modUser') {
        $values = array_map(function($value) {
            if (empty($value['email']) && isset($value['username'])) {
                $value['email'] = "{$value['username']}@gmail.com";
            }
            return $value;
        }, $values);
    }
    $modx->event->params['values'] = $values;
}

modResource

Create a parent if it does not exist.

php
if ($modx->event->name === 'gsOnImportValues') {
    if ($object->model_class === 'modResource') {
        $values = array_map(function($value) use ($modx) {
            if (isset($value['parent'])) {
                if (!$modx->getObject(modResource::class, ['pagetitle' => $value['parent']])) {
                    $alias = $modx->call(modResource::class, 'filterPathSegment', [&$modx, $value['parent']]);
                    $resource = $modx->newObject(modResource::class);
                    $resource->fromArray([
                        'pagetitle' => $value['parent'],
                        'alias' => $alias,
                        'uri' => $alias,
                        'parent' => 0,
                        'template' => 1,
                        'context_key' => 'web',
                        'published' => 1
                    ], '', true);
                    $resource->save();
                }
            }
            return $value;
        }, $values);
    }
    $modx->event->params['values'] = $values;
}

msProduct

Update the price.

php
if ($modx->event->name === 'gsOnImportValues') {
    if ($object->model_class === 'msProduct') {
        $values = array_map(function($value) {
            if (isset($value['price'])) {
                $value['price'] = $value['price'] * 1.1;
            }
            return $value;
        }, $values);
    }
    $modx->event->params['values'] = $values;
}

© GoogleSheets 2019-present