Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function safeUp()
- {
- $fieldGroupName = 'myFieldGroup';
- $fieldService = Craft::$app->getFields();
- $fieldGroup = new FieldGroup();
- $fieldGroup->name = $fieldGroupName;
- $fieldGroupSuccess = $fieldService->saveGroup($fieldGroup);
- if ($fieldGroupSuccess) {
- $fieldGroupId = null;
- // FIXME: Couldn't find a method to search for a field group by name
- $fieldGroups = $fieldService->getAllGroups();
- foreach ($fieldGroups as $_group) {
- if ($_group->name == $fieldGroupName) {
- $fieldGroupId = $_group->id;
- }
- }
- if (!$fieldGroupId) {
- echo "Couldn't find ID for field group, aborting";
- return false;
- }
- $field = $fieldService->createField([
- 'type' => 'craft\fields\PlainText',
- 'groupId' => $fieldGroupId,
- 'name' => 'fieldName',
- 'handle' => 'fieldNameHandle'
- ]);
- }
- return $fieldService->saveField($field);
- }
Add Comment
Please, Sign In to add comment