Guest User

Untitled

a guest
Jan 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. public function safeUp()
  2. {
  3.  
  4. $fieldGroupName = 'myFieldGroup';
  5. $fieldService = Craft::$app->getFields();
  6. $fieldGroup = new FieldGroup();
  7.  
  8. $fieldGroup->name = $fieldGroupName;
  9.  
  10. $fieldGroupSuccess = $fieldService->saveGroup($fieldGroup);
  11.  
  12.  
  13. if ($fieldGroupSuccess) {
  14. $fieldGroupId = null;
  15.  
  16. // FIXME: Couldn't find a method to search for a field group by name
  17. $fieldGroups = $fieldService->getAllGroups();
  18. foreach ($fieldGroups as $_group) {
  19. if ($_group->name == $fieldGroupName) {
  20. $fieldGroupId = $_group->id;
  21. }
  22. }
  23. if (!$fieldGroupId) {
  24. echo "Couldn't find ID for field group, aborting";
  25. return false;
  26. }
  27. $field = $fieldService->createField([
  28. 'type' => 'craft\fields\PlainText',
  29. 'groupId' => $fieldGroupId,
  30. 'name' => 'fieldName',
  31. 'handle' => 'fieldNameHandle'
  32. ]);
  33. }
  34. return $fieldService->saveField($field);
  35. }
Add Comment
Please, Sign In to add comment