Jump to content

Create Field from API and give read-access only to superusers


markus_blue_tomato
 Share

Recommended Posts

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

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.

  • Like 5
Link to comment
Share on other sites

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;

image.thumb.png.9b2b726b40af76ce1c412b691a1ccaf4.png

Link to comment
Share on other sites

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:

ha2P3ar.png

@tiefenbacher_bluetomato Greetings from Vienna to Schladming(?) 😉  Looking forward to the winter 🙂 

  • Like 1
Link to comment
Share on other sites

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:

ha2P3ar.png

@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 🙂

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...