Answers.Body: <p>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.</p>
  
  <p>Unless you mean create an asset source using an existing AssetSourceType programmatically?</p>
  
  <p>If you want an asset source saved, I would guess it is similar to below:</p>
  
  <pre><code>$source = new AssetSourceModel();
  $source-&gt;name   = 'Source name';
  $source-&gt;handle = 'Source handle';
  $source-&gt;type   = 'Local';
  // Not sure about the settings here
  $source-&gt;settings = array('path' =&gt; 'path/to/','url' =&gt; 'url/path/to', 'publicURLs' =&gt; '1');
  
  $fieldLayout = craft()-&gt;fields-&gt;getLayoutById(1); // set this to the field layout you want
  $fieldLayout-&gt;type = ElementType::Asset;
  
  $source-&gt;setFieldLayout($fieldLayout);
  
  if (craft()-&gt;assetSources-&gt;saveSource($source)) {
      // Success saving
  }
  else {
      // Error saving
  }
  </code></pre>
  
  <p>You can look at the AssetSources controller actionSaveSource for reference.</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-12T12:10:59.143Z
Answers.Text: Thanks Jay. It was the "asset source" I wanted to save. Your snippet is exactly what I was after. Cheers.
Answers.UserDisplayName: Mark Notton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-12T09:26:56.340Z
Answers.LastActivityDate: 2016-07-25T04:33:14.707Z
Answers.LastEditDate: 2016-07-25T04:33:14.707Z
Answers.UserDisplayName: Jay
Body: <p>I've taken a look at <code>app/services/InstallService.php</code> for some inspiration, and I'm not great at navigating/understanding the <a href="https://craftcms.com/docs/plugins/introduction" rel="nofollow">documentation</a>. I really don't know where to begin with this.</p>
  
  <p>Could I get a little push in the right direction on <strong>how to create an Asset Source programmatically via a plugin</strong> please?</p>
  
  <p>I'm hoping there's a way I can generate an asset source <code>onBeforeInstall</code>. I'll probably need a method to remove it too should  the plugin be uninstalled. </p>
  
  <p>Thanks.</p>
  
Tags: <plugin-development><assets>
Title: How to create an Asset Source programmatically via a plugin?
ThreadText: # How to create an Asset Source programmatically via a plugin?
  
  (Asked by: username_0 on 2016-04-12)
  
  I've taken a look at `app/services/InstallService.php` for some inspiration, and I'm not great at navigating/understanding the [documentation](https://craftcms.com/docs/plugins/introduction). 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.
  
  **Tags:** plugin-development, assets
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-04-12. Score: 5 | Accepted answer
  
  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.
  
  > **Comments:**
  > **username_0**: Thanks Jay. It was the "asset source" I wanted to save. Your snippet is exactly what I was after. Cheers.  
  
  ---
  
  