Answers.Body: <p>Figured it out, the format it expects is <code>'source' =&gt; 'group: &lt;group UID&gt;'</code></p>
  
  <p>So...</p>
  
  <pre><code>    protected function createCategoriesField() {
          // Get the group
          $group = (new \craft\db\Query())
              -&gt;select("id")
              -&gt;from("fieldgroups")
              -&gt;where(["name" =&gt; "common"])
              -&gt;one();
  
          // Get the category group UID
          $catGroup = Craft::$app-&gt;categories-&gt;getGroupByHandle("categories")-&gt;uid;
  
          // Initialize the field
          $field = new \craft\fields\Categories([
              "groupId" =&gt; $group["id"],
              "name" =&gt; "Categories",
              "handle" =&gt; "categories",
              "selectionLabel" =&gt; "Select categories",
              "source" =&gt; "group:" . $catGroup
          ]);
  
          // Save the field
          return (Craft::$app-&gt;getFields()-&gt;saveField($field));
      }
  </code></pre>
  
  <p>This was, and is, going to be a big help with migrations -> <a href="https://github.com/markhuot/craftql/blob/master/src/Console/ToolsController.php" rel="nofollow noreferrer">https://github.com/markhuot/craftql/blob/master/src/Console/ToolsController.php</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-05T20:56:21.483Z
Answers.Text: Does this still work, so far as you are aware? I'm working on a migration and it involves setting the source of a currently existing field to a new category group (still only bound to one group). However, when I try to use `$field->setAttribute(['source' => 'group:'.$groupUid);` and save it, the field's source just sets to 'group:' and then the category group's id, and not the UID, so consequently it fails. Any thoughts on how to get that working?
Answers.UserDisplayName: accalton
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-26T14:23:51.580Z
Answers.LastActivityDate: 2020-05-26T15:20:28.907Z
Answers.LastEditDate: 2020-05-26T15:20:28.907Z
Answers.UserDisplayName: dmatthams
Body: <p>I am writing a plugin that sets up Craft in a particular way and have created a category field, but can't figure out how to assign a category group to the field, I have the following:</p>
  
  <pre><code> protected function createCategoriesField() {
  
          // Get the field group
          $group = (new \craft\db\Query())
              -&gt;select("id")
              -&gt;from("fieldgroups")
              -&gt;where(["name" =&gt; "common"])
              -&gt;one();
  
          // Get the category group
          $catGroup = Craft::$app-&gt;categories-&gt;getGroupByHandle("categories");
  
  
          // Initialize the field
          $field = new \craft\fields\Categories([
              "groupId" =&gt; $group["id"],
              "name" =&gt; "Categories",
              "handle" =&gt; "categories",
              "selectionLabel" =&gt; "Select categories",
              "sources" =&gt; $catGroup
          ]);
  
          // Save the field
          return (Craft::$app-&gt;getFields()-&gt;saveField($field));
      }
  </code></pre>
  
  <p>It seems sources is the correct setting, but it accepts 'source keys' which I'm unfamiliar with </p>
  
Tags: <plugin-development><migration>
Title: Assign a category group to a category field via migration
ThreadText: # Assign a category group to a category field via migration
  
  (Asked by: username_0 on 2020-05-26)
  
  I am writing a plugin that sets up Craft in a particular way and have created a category field, but can't figure out how to assign a category group to the field, I have the following:
  
  ```
   protected function createCategoriesField() {
  
          // Get the field group
          $group = (new \craft\db\Query())
              ->select("id")
              ->from("fieldgroups")
              ->where(["name" => "common"])
              ->one();
  
          // Get the category group
          $catGroup = Craft::$app->categories->getGroupByHandle("categories");
  
          // Initialize the field
          $field = new \craft\fields\Categories([
              "groupId" => $group["id"],
              "name" => "Categories",
              "handle" => "categories",
              "selectionLabel" => "Select categories",
              "sources" => $catGroup
          ]);
  
          // Save the field
          return (Craft::$app->getFields()->saveField($field));
      }
  
  ```
  
  It seems sources is the correct setting, but it accepts 'source keys' which I'm unfamiliar with
  
  **Tags:** plugin-development, migration
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-05-26. Score: 2 | Accepted answer
  
  Figured it out, the format it expects is `'source' => 'group: <group UID>'`
  
  So...
  
  ```
      protected function createCategoriesField() {
          // Get the group
          $group = (new \craft\db\Query())
              ->select("id")
              ->from("fieldgroups")
              ->where(["name" => "common"])
              ->one();
  
          // Get the category group UID
          $catGroup = Craft::$app->categories->getGroupByHandle("categories")->uid;
  
          // Initialize the field
          $field = new \craft\fields\Categories([
              "groupId" => $group["id"],
              "name" => "Categories",
              "handle" => "categories",
              "selectionLabel" => "Select categories",
              "source" => "group:" . $catGroup
          ]);
  
          // Save the field
          return (Craft::$app->getFields()->saveField($field));
      }
  
  ```
  
  This was, and is, going to be a big help with migrations -> <https://github.com/markhuot/craftql/blob/master/src/Console/ToolsController.php>
  
  > **Comments:**
  > **username_1**: Does this still work, so far as you are aware? I'm working on a migration and it involves setting the source of a currently existing field to a new category group (still only bound to one group). However, when I try to use `$field->setAttribute(['source' => 'group:'.$groupUid);` and save it, the field's source just sets to 'group:' and then the category group's id, and not the UID, so consequently it fails. Any thoughts on how to get that working?  
  
  ---
  
  