markus_blue_tomato Posted October 30, 2018 Share Posted October 30, 2018 I want to create a field, which I add later to a template. How can I set the access-rights only to superusers? I want, that this field is only visible to superusers. And my second question: Is it possible to make the field also read-only in the frontend but write-able from the API? $field = new Field(); $field->type = $modules->get("FieldtypeText"); $field->name = "my_field"; $field->label = "My Field Label"; $field->value = ''; $field->save(); Link to comment Share on other sites More sharing options...
elabx Posted October 30, 2018 Share Posted October 30, 2018 Both are possible: https://processwire.com/blog/posts/field-permissions-overrides-and-more-2.6.2/ Sorry to not bring more info right now, don't really know how to set it through the API, but surely there is a way. Link to comment Share on other sites More sharing options...
adrian Posted October 30, 2018 Share Posted October 30, 2018 6 hours ago, tiefenbacher_bluetomato said: How can I set the access-rights only to superusers? I want, that this field is only visible to superusers. I have just added access rights to TracyDebugger's RequestInfo > Field Code section. It now contains: [ 'type' => 'FieldtypeText', 'flags' => '32800', 'name' => 'my_field_2', 'label' => 'My Field 2 Label', 'collapsed' => '0', 'minlength' => '0', 'maxlength' => '2048', 'showCount' => '0', 'size' => '0', 'tags' => '', 'editRoles' => [ 'editor', ], 'viewRoles' => [ 'author', 'editor', ], 'textformatters' => '', 'showIf' => '', 'themeInputSize' => '', 'themeInputWidth' => '', 'themeOffset' => '', 'themeBorder' => '', 'themeColor' => '', 'themeBlank' => '', 'columnWidth' => '100', 'required' => '', 'requiredAttr' => '', 'requiredIf' => '', 'stripTags' => '', 'placeholder' => '', 'pattern' => '', ] which is everything to need to create a field via the API including the view and edit restrictions. You can then simply wrap this output in: $f = new Field(); foreach() $f->save(); so that you have: $f = new Field(); foreach([ 'type' => 'FieldtypeText', 'flags' => '32800', 'name' => 'my_field', 'label' => 'My Field Label', 'collapsed' => '0', 'minlength' => '0', 'maxlength' => '2048', 'showCount' => '0', 'size' => '0', 'tags' => '', 'editRoles' => [ 'editor', ], 'viewRoles' => [ 'author', 'editor', ], 'textformatters' => '', 'showIf' => '', 'themeInputSize' => '', 'themeInputWidth' => '', 'themeOffset' => '', 'themeBorder' => '', 'themeColor' => '', 'themeBlank' => '', 'columnWidth' => '100', 'required' => '', 'requiredAttr' => '', 'requiredIf' => '', 'stripTags' => '', 'placeholder' => '', 'pattern' => '', ] as $k => $v) { $f->$k = $v; } $f->save(); So to get this to work, create a field via the PW admin and set it up exactly how you want with the access restrictions, then go to the RequestInfo panel and copy the array from the "Field Code" section. Note that this section is not turned on by default when you install Tracy, so you will need to go to the config settings for the RequestInfo panel and turn it on. 5 Link to comment Share on other sites More sharing options...
adrian Posted October 30, 2018 Share Posted October 30, 2018 @tiefenbacher_bluetomato - Just a quick note to say that I just pushed another revised version that fixes a bug in the previous one, so please be sure to update if you want to use this feature. Link to comment Share on other sites More sharing options...
markus_blue_tomato Posted October 30, 2018 Author Share Posted October 30, 2018 Thanks! I tried this and it works great (example): $field->useRoles = 1; $field->editRoles = array('author'); $field->viewRoles = array('translator', 'author'); But I decided to solve it in an other way - because I have an hook where "translator" have also edit right to the field. The user only should not have write-access in the GUI to it. I solved this with the "Visibility" setting. Namend "collapsed in the API: $field->collapsed = 7; Link to comment Share on other sites More sharing options...
bernhard Posted October 31, 2018 Share Posted October 31, 2018 15 hours ago, adrian said: So to get this to work, create a field via the PW admin and set it up exactly how you want with the access restrictions, then go to the RequestInfo panel and copy the array from the "Field Code" section. That's exactly why I built it - thanks for improving it further @adrian ? 14 hours ago, tiefenbacher_bluetomato said: $field->collapsed = 7; Just a quick tip you might find helpful. It makes the code more readable if you use constants instead of integer variables. You'll get them from intellisense in your IDE so it's also easier to look them up like this than heading over to the docs:@tiefenbacher_bluetomato Greetings from Vienna to Schladming(?) ? Looking forward to the winter ? 1 Link to comment Share on other sites More sharing options...
markus_blue_tomato Posted November 6, 2018 Author Share Posted November 6, 2018 On 10/31/2018 at 10:19 AM, bernhard said: Just a quick tip you might find helpful. It makes the code more readable if you use constants instead of integer variables. You'll get them from intellisense in your IDE so it's also easier to look them up like this than heading over to the docs:@tiefenbacher_bluetomato Greetings from Vienna to Schladming(?) ? Looking forward to the winter ? Thx! Our IT and Web-Dev Department is located in Graz - so greeting to vienna ? 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now