2

I've taken a look at app/services/InstallService.php for some inspiration, and I'm not great at navigating/understanding the documentation. I really don't know where to begin with this.

Could I get a little push in the right direction on how to create an Asset Source programmatically via a plugin please?

I'm hoping there's a way I can generate an asset source onBeforeInstall. I'll probably need a method to remove it too should the plugin be uninstalled.

Thanks.

5

I don't think you can add asset source types without modifying core. The list of components that is allowed / scanned for does not contain AssetSourceTypes.

Unless you mean create an asset source using an existing AssetSourceType programmatically?

If you want an asset source saved, I would guess it is similar to below:

$source = new AssetSourceModel();
$source->name   = 'Source name';
$source->handle = 'Source handle';
$source->type   = 'Local';
// Not sure about the settings here
$source->settings = array('path' => 'path/to/','url' => 'url/path/to', 'publicURLs' => '1');

$fieldLayout = craft()->fields->getLayoutById(1); // set this to the field layout you want
$fieldLayout->type = ElementType::Asset;

$source->setFieldLayout($fieldLayout);

if (craft()->assetSources->saveSource($source)) {
    // Success saving
}
else {
    // Error saving
}

You can look at the AssetSources controller actionSaveSource for reference.

There are some areas I'm not sure about such as the settings and the field layout, as they get set by POST data, but this should lead you in the right direction.

| improve this answer | |
  • Thanks Jay. It was the "asset source" I wanted to save. Your snippet is exactly what I was after. Cheers. – Mark Notton Apr 12 '16 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.