Answers.Body: <p>First of all I would like to leave a few links for you. </p>
  
  <ol>
  <li><a href="https://pluginfactory.io/" rel="nofollow noreferrer">Pluginfactory.io</a> is a page where you can create your "starting" files. It creates an empty Plugin, with all the required files/functions with example codes for routing and services....</li>
  <li><a href="https://craftcms.com/docs/plugins/introduction" rel="nofollow noreferrer">The Docs</a> about Craft Plugin developement</li>
  </ol>
  
  <p>Then to the code: you'll need to insert a function like this in your <a href="https://craftcms.com/docs/plugins/setting-things-up#your-primary-plugin-class" rel="nofollow noreferrer">Plugin's primary class</a></p>
  
  <pre><code>craft()-&gt;templates-&gt;hook('cp.entries.edit', function(&amp;$context) {
      if ($context['sectionHandle'] === 'course') {
          // you can get these from a config file or you could create
          // plugin settings, I'll just insert an array here, it's faster
          $config = [
              'entryTypeOne'     =&gt; [
                  'title'         =&gt; 'A title',
                  'body'          =&gt; 'some information',
                  'relationField' =&gt; [1, 2, 4] //&lt;-- array of element ids
              ],
              'anotherEntryType' =&gt; [
                  'title'         =&gt; 'Another title',
                  'body'          =&gt; 'some more information',
                  'relationField' =&gt; [] //&lt;-- array of element ids
              ]
          ];
  
          /** @var \Craft\EntryModel $entry */
          $entry = $context['entry'];
          $entry-&gt;getContent()-&gt;setAttributes($config[$entry-&gt;getType()-&gt;handle]);
      }
  }
  </code></pre>
  
  <p>However: unfortunately you can't set default values when the user changes the entry type with this method, thus we'll need another "hack".
  As you can see in the <a href="https://github.com/benjamminf/craft-relabel/blob/master/relabel/resources/js/Relabel.js#L120" rel="nofollow noreferrer">Relabel Plugin</a> Craft provides ways to change default functions. You can insert this javascript</p>
  
  <pre><code>Craft.EntryTypeSwitcher.prototype.onTypeChange =  function(ev)
  {
      this.$spinner.removeClass('hidden');
  
      Craft.postActionRequest('entries/switchEntryType', Craft.cp.$container.serialize(), $.proxy(function(response, textStatus) {
          this.$spinner.addClass('hidden');
  
          if (textStatus == 'success')
          {
              var fieldsPane = this.$fields.data('pane');
              fieldsPane.deselectTab();
              this.$fields.html(response.paneHtml);
              fieldsPane.destroy();
              this.$fields.pane();
              Craft.initUiElements(this.$fields);
  
              Craft.appendHeadHtml(response.headHtml);
              Craft.appendFootHtml(response.footHtml);
  
              // Update the slug generator with the new title input
              if (typeof slugGenerator != "undefined")
              {
                  slugGenerator.setNewSource('#title');
              }
          }
      }, this));
  }
  </code></pre>
  
  <p>To change the default behavior when someone changes the entry type. All you have to do is to change the destination of the script 
  from </p>
  
  <pre><code>Craft.postActionRequest('entries/switchEntryType'
  </code></pre>
  
  <p>to something like</p>
  
  <pre><code>Craft.postActionRequest('yourpluginhandle/controllername/switchEntryType'
  </code></pre>
  
  <p>Then you'll take the default <a href="https://github.com/craftcms/cms/blob/develop-v2/src/controllers/EntriesController.php#L350" rel="nofollow noreferrer">EntriesController</a> function</p>
  
  <pre><code>public function actionSwitchEntryType()
  {
      $this-&gt;requirePostRequest();
      $this-&gt;requireAjaxRequest();
  
      $entry = $this-&gt;_getEntryModel();
      $this-&gt;enforceEditEntryPermissions($entry);
      $this-&gt;_populateEntryModel($entry);
  
      $variables['sectionId'] = $entry-&gt;sectionId;
      $variables['entry'] = $entry;
      $variables['showEntryTypes'] = true;
  
      $this-&gt;_prepEditEntryVariables($variables);
  
      $paneHtml = craft()-&gt;templates-&gt;render('_includes/tabs', $variables) .
          craft()-&gt;templates-&gt;render('entries/_fields', $variables);
  
      $this-&gt;returnJson(array(
          'paneHtml' =&gt; $paneHtml,
          'headHtml' =&gt; craft()-&gt;templates-&gt;getHeadHtml(),
          'footHtml' =&gt; craft()-&gt;templates-&gt;getFootHtml(),
      ));
  }
  </code></pre>
  
  <p>And do exactly the same like in the first function</p>
  
  <pre><code>$entry-&gt;getContent()-&gt;setAttributes($config[$entry-&gt;getType()-&gt;handle]);
  </code></pre>
  
  <p>Unfortunately if you want to take care of all use cases you have to overwrite another function. When someone is trying to create an entry via an <code>ElementEditor</code>(the thing that pops up when you click on "new entry") in a relation field. It works exactly the same like my first method and it's the same like <a href="https://github.com/benjamminf/craft-relabel/blob/master/relabel/resources/js/Relabel.js#L120" rel="nofollow noreferrer">Here</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-01T08:45:26.590Z
Answers.Text: Thanks Robin, thats a huge help - lots of information to get started with.
Answers.UserDisplayName: Sjc
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-01T08:49:00.560Z
Answers.Text: Let me know if you need any help. It's relative easy but it can be hard to get into it the first time. I can also provide help in chat after you reached a certain level here
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-03-01T07:50:35.220Z
Answers.LastActivityDate: 2018-03-01T08:37:18.017Z
Answers.LastEditDate: 2018-03-01T08:37:18.017Z
Answers.UserDisplayName: Robin Schambach
Body: <p>Is it possible to do the following in Craft?</p>
  
  <p>I want to set up course information as follows:</p>
  
  <p>1) have a <strong>courseType</strong> which contains default values for that type of course - name (i.e. 3 hour skills, full day masterclass etc), price, capacity, trainers etc</p>
  
  <p>2) have a <strong>course</strong> which has a <strong>courseType</strong> entry filed among other details such as details of the course, equipment used, etc. The <strong>course</strong> will also have fields for price, trainers &amp; capacity.</p>
  
  <p>Is it possible to have the <strong>course</strong> price, trainers &amp; capacity fields filled with the value of the <strong>courseType</strong> selected when it is chosen in the CP new course page? These <strong>course</strong> fields should then be able to be overridden if required on a course by course basis.   </p>
  
  <p>Any suggestions would be very welcome - I'm new to craft and can find any examples like this.</p>
  
  <p>Thanks in advance.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-28T16:03:55.973Z
Comments.Text: When it's only about the CP you can go into an entry and use the `save as new entry` function. Otherwise it requires a plugin to use certain hooks to overwrite the default values. Is this function mainly for the CP or do you want to be able to show "fallback" values in your frontend as well?
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-28T16:10:45.027Z
Comments.Text: Thanks Robin - It would be CP only. There will be a few course types with several default values as described, the goal it to allow the creation of courses that will 'inherit' these default values when the course type is chosen. It is to speed up course creation for the client as there will be many similar ones created on an ongoing basis.
Comments.UserDisplayName: Sjc
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-28T16:14:56.917Z
Comments.Text: Then you have to decide the way you want to go... the "easy" way would be to create "template" courses and set their status as deactivated so they won't appear in your frontend and tell your customers to use those template entries to create new entries. The other way would be to use the `cp.entries.edit'` hook to insert your values. I can show you how if you want, just let me know
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2018-02-28T17:25:28.317Z
Comments.Text: I would rather go the more involved way if it meant that the client can add new course types without having to have the corresponding course template made. So would I have to implement the functionality as a plugin/module that monitored change events on this specific kind of field and then dynamically update related fields in the course entry page data? If so where would I find documentation on the undocumented cp.entries.edit :-) Thanks for the information so far.
Comments.UserDisplayName: Sjc
Tags: <entries><field><entries-field><default>
Title: Using values from one entry as default values in another?
ThreadText: # Using values from one entry as default values in another?
  
  (Asked by: username_0 on 2018-02-28)
  
  Is it possible to do the following in Craft?
  
  I want to set up course information as follows:
  
  1) have a **courseType** which contains default values for that type of course - name (i.e. 3 hour skills, full day masterclass etc), price, capacity, trainers etc
  
  2) have a **course** which has a **courseType** entry filed among other details such as details of the course, equipment used, etc. The **course** will also have fields for price, trainers & capacity.
  
  Is it possible to have the **course** price, trainers & capacity fields filled with the value of the **courseType** selected when it is chosen in the CP new course page? These **course** fields should then be able to be overridden if required on a course by course basis.
  
  Any suggestions would be very welcome - I'm new to craft and can find any examples like this.
  
  Thanks in advance.
  
  **Tags:** entries, field, entries-field, default
  
  > **Comments:**
  > **username_1**: When it's only about the CP you can go into an entry and use the `save as new entry` function. Otherwise it requires a plugin to use certain hooks to overwrite the default values. Is this function mainly for the CP or do you want to be able to show "fallback" values in your frontend as well?  
  > **username_0**: Thanks username_1 - It would be CP only. There will be a few course types with several default values as described, the goal it to allow the creation of courses that will 'inherit' these default values when the course type is chosen. It is to speed up course creation for the client as there will be many similar ones created on an ongoing basis.  
  > **username_1**: Then you have to decide the way you want to go... the "easy" way would be to create "template" courses and set their status as deactivated so they won't appear in your frontend and tell your customers to use those template entries to create new entries. The other way would be to use the `cp.entries.edit'` hook to insert your values. I can show you how if you want, just let me know  
  > **username_0**: I would rather go the more involved way if it meant that the client can add new course types without having to have the corresponding course template made. So would I have to implement the functionality as a plugin/module that monitored change events on this specific kind of field and then dynamically update related fields in the course entry page data? If so where would I find documentation on the undocumented cp.entries.edit :-) Thanks for the information so far.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-03-01. Score: 1 | Accepted answer
  
  First of all I would like to leave a few links for you.
  
  1. [Pluginfactory.io](https://pluginfactory.io/) is a page where you can create your "starting" files. It creates an empty Plugin, with all the required files/functions with example codes for routing and services....
  2. [The Docs](https://craftcms.com/docs/plugins/introduction) about Craft Plugin developement
  
  Then to the code: you'll need to insert a function like this in your [Plugin's primary class](https://craftcms.com/docs/plugins/setting-things-up#your-primary-plugin-class)
  
  ```
  craft()->templates->hook('cp.entries.edit', function(&$context) {
      if ($context['sectionHandle'] === 'course') {
          // you can get these from a config file or you could create
          // plugin settings, I'll just insert an array here, it's faster
          $config = [
              'entryTypeOne'     => [
                  'title'         => 'A title',
                  'body'          => 'some information',
                  'relationField' => [1, 2, 4] //<-- array of element ids
              ],
              'anotherEntryType' => [
                  'title'         => 'Another title',
                  'body'          => 'some more information',
                  'relationField' => [] //<-- array of element ids
              ]
          ];
  
          /** @var \Craft\EntryModel $entry */
          $entry = $context['entry'];
          $entry->getContent()->setAttributes($config[$entry->getType()->handle]);
      }
  }
  
  ```
  
  However: unfortunately you can't set default values when the user changes the entry type with this method, thus we'll need another "hack".
  As you can see in the [Relabel Plugin](https://github.com/benjamminf/craft-relabel/blob/master/relabel/resources/js/Relabel.js#L120) Craft provides ways to change default functions. You can insert this javascript
  
  ```
  Craft.EntryTypeSwitcher.prototype.onTypeChange =  function(ev)
  {
      this.$spinner.removeClass('hidden');
  
      Craft.postActionRequest('entries/switchEntryType', Craft.cp.$container.serialize(), $.proxy(function(response, textStatus) {
          this.$spinner.addClass('hidden');
  
          if (textStatus == 'success')
          {
              var fieldsPane = this.$fields.data('pane');
              fieldsPane.deselectTab();
              this.$fields.html(response.paneHtml);
              fieldsPane.destroy();
              this.$fields.pane();
              Craft.initUiElements(this.$fields);
  
              Craft.appendHeadHtml(response.headHtml);
              Craft.appendFootHtml(response.footHtml);
  
              // Update the slug generator with the new title input
              if (typeof slugGenerator != "undefined")
              {
                  slugGenerator.setNewSource('#title');
              }
          }
      }, this));
  }
  
  ```
  
  To change the default behavior when someone changes the entry type. All you have to do is to change the destination of the script
  from
  
  ```
  Craft.postActionRequest('entries/switchEntryType'
  
  ```
  
  to something like
  
  ```
  Craft.postActionRequest('yourpluginhandle/controllername/switchEntryType'
  
  ```
  
  Then you'll take the default [EntriesController](https://github.com/craftcms/cms/blob/develop-v2/src/controllers/EntriesController.php#L350) function
  
  ```
  public function actionSwitchEntryType()
  {
      $this->requirePostRequest();
      $this->requireAjaxRequest();
  
      $entry = $this->_getEntryModel();
      $this->enforceEditEntryPermissions($entry);
      $this->_populateEntryModel($entry);
  
      $variables['sectionId'] = $entry->sectionId;
      $variables['entry'] = $entry;
      $variables['showEntryTypes'] = true;
  
      $this->_prepEditEntryVariables($variables);
  
      $paneHtml = craft()->templates->render('_includes/tabs', $variables) .
          craft()->templates->render('entries/_fields', $variables);
  
      $this->returnJson(array(
          'paneHtml' => $paneHtml,
          'headHtml' => craft()->templates->getHeadHtml(),
          'footHtml' => craft()->templates->getFootHtml(),
      ));
  }
  
  ```
  
  And do exactly the same like in the first function
  
  ```
  $entry->getContent()->setAttributes($config[$entry->getType()->handle]);
  
  ```
  
  Unfortunately if you want to take care of all use cases you have to overwrite another function. When someone is trying to create an entry via an `ElementEditor`(the thing that pops up when you click on "new entry") in a relation field. It works exactly the same like my first method and it's the same like [Here](https://github.com/benjamminf/craft-relabel/blob/master/relabel/resources/js/Relabel.js#L120)
  
  > **Comments:**
  > **username_0**: Thanks username_1, thats a huge help - lots of information to get started with.  
  > **username_1**: Let me know if you need any help. It's relative easy but it can be hard to get into it the first time. I can also provide help in chat after you reached a certain level here  
  
  ---
  
  