Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Plugins

Admin: Show user a subset of page templates when creating new page

admin plugins

Solved by pamtbaau View solution

Started by Kit 5 years ago · 9 replies · 1041 views
5 years ago

Hi,

Until the permissions evolve

on admin, where merge class "Groups" on body -> {{ body_classes }} with php plugin
{{ body_classes }} : github.com / grav-plugin-admin / ... /templates/partials/base-root.html.twig#L48

access level|690x94
Admin v1.7.x > Accounts > Access Levels > Groups

with bodyClassFunc( string/string[] $classes ) ???
class-grav-common-twig-twigextension

Why ?
In css :

  • hide pages in page selection lists
  • Add color groups
  • and more

add page|613x500


I can override admin theme
disadvantage
If admin update ... 😬 🥳 😬

thx for help

5 years ago

Hi @Kit I was wondering if it used in conjunction with the admin panel

/admin/config/system

for using and overriding controlling some of the display aspects, the one that jumps to mind will be,

default page count
where it reduces how many items are shown in the list

also under pages/advanced you will see a field for body classes, which injects this in to templates, is states for quark at present, so you might just want see if your template supports it

Its quite specific question on Gravs inner workings and might be worth asking on the discord server https://chat.getgrav.org/ or logging the question on the getgrav github page, where the developers hang out.

Sorry I cant be more specific

last edited 05/19/21 by Spam Hater
5 years ago

Hi @spamhater

thank you for the reply

discord, I hate it :D
I'll go to GitHub then, just take the time ;)

👍 1
5 years ago

@spamhater Yes of course ! 👍

5 years ago

@Kit, I'm sure you've tried to create a clear and succinct as possible question. I've read the question several times over the past 7 days, but I'm afraid I still do not understand the questions/issue, nor the desired goal...

Would you mind providing a tad more information to clarify the issue and explaining what you are trying to achieve?

5 years ago

Hi @pamtbaau,
Sorry :disappointed_relieved:

currently, they see all the pages
for all groups

add page|613x500, 75%


I'm trying to achieve

Add class on body (grav admin plugin)

grav-plugin-admin-base-root-html-twig-at-develop-·-getgrav-grav-plugin-admin-·-GitHub|690x68

the user's group
admin-access-grp-on-body|690x292

and with css
.grp_1 #new-page .option[data-value="form"] {
display: none;
}

Resultat
page sort|690x453, 75%

From 11 pages to 3 pages
The user will be less confused by all these options


Once this option is unlocked
We could imagine, one colour per group and more


Sincerely
Kit

5 years ago Solution

@Kit, I'll try to answer my own questions...

Issue: When Admin user creates a new page, a (possibly) large list of templates is shown in the Add Page modal. This might be confusing to the user.

Goal: When Admin user creates a new page, show a brief list of templates where the content of the list depends on a configuration setting.

Note:

  • I don't like the (mis)use of the Groups field for this, since it is being used for setting access permissions on pages. I therefor chose a different approach.
  • I don't like the idea of adding group-names in css/scss files as class selectors, nor the idea of adding coloured borders around the template select field. I skipped this.

Having said that, I've taken the following route to implement the goal as formulated by myself. And that might not be your goal...

Prototype:

  • Add a 'template' field to the User blueprint

    • Create file /user/blueprints/user/account.yaml to override existing blueprint and add field 'Templates`.
      Add the following:
      TXT
      
      extends@:
      type: account
      context: blueprints://user
      

    form:
    validation: loose

    YAML
    fields:
      templates:
        ordering@: -2
        security@: admin.super
        type: select
        multiple: true
        size: large
        label: Available templates
        data-options@: '\Grav\Common\Page\Pages::pageTypes'
        data-default@: '\Grav\Plugin\Admin\Admin::getLastPageName'
        classes: fancy
        help: Create simple list of templates to be used by user
        validate:
          type: commalist
    
    TXT
    
    
  • Create new plugin to alter the template list in modal Add Page:

    • $ bin/plugin devtools new-plugin
    • In generated plugin update function onPluginsInitialized as follows:

      PHP
      if ($this->isAdmin()) {
      $this->enable([
        // Put your main events here
        'onAdminPageTypes' => ['onAdminPageTypes', 0],
      ]);
      
      return;
      }
      
    • Create function onAdminPageTypes as follows:

      PHP
      public function onAdminPageTypes(Event $event)
      {
      /** @var User */
      $user = $this->grav['user'];
      
      if (!$user->get('username')) {
          return;
      }
      
      $allTypes = $event['types'];
      $templates = $user->get('templates', null);
      
      if (!$templates) {
          // Show user list of all available templates
          return;
      }
      
      $types = [];
      
      foreach (array_values($templates) as $template) {
          $types[$template] = $allTypes[$template];
      }
      
      $event['types'] = $types;
      }
      

Result:

  • The User config form:
    Untitled|690x329
  • Create new page: Add Page modal:
    Untitled|690x265

Notes:

  • No changes to Admin
  • No hardcoded group selectors in css/scss
  • A change of Available templates in User config, will only be effective after next login of user.
  • Once page has been created, field Page Template in tab Advanced will still show all available templates. But you didn't mention that field, so it should be ok...
  • Does it leave room for improvements? It sure does...
👍 1
last edited 05/25/21 by pamtbaau
5 years ago

@pamtbaau
sorry for the late reply and thank you for this very complete answer
I'll look into it as soon as possible ;)

5 years ago

@pamtbaau

I have just tested it and it works perfectly, many thanks

And above all

  • No changes made to the administrator
  • No hard-coded group selectors in css/scss

I made some improvements to be able to use it on groups and all works.
Why are you going to tell me

Issue : I need to add an option for all users and I need to open them one by one.
If 1-6 users ok,
if more ... outch, I'm going to drink coffees ☕ and come back :D

Goal : To be able to add an option in "Add page" on several users at the same time

Maybe one day we can find it here
grav option group create page|690x411

Sincerely
Kit

Suggested topics

Topic Participants Replies Views Activity
Plugins · by Rene, 1 week ago
2 44 1 week ago
Plugins · by Xavier, 4 weeks ago
2 54 4 weeks ago
Plugins · by Luka Prinčič, 7 years ago
3 1181 1 month ago
Plugins · by Sebastian van de Meer, 1 month ago
1 48 1 month ago
Plugins · by PIERROT Alain, 2 months ago
3 73 2 months ago