- Issue created by @darkdim
- Issue was unassigned.
- First commit to issue fork.
- Merge request !8PSPRO01-786: 3420179 adds access role for generator button. → (Merged) created by kenyoOwen
- Status changed to Needs review
7 months ago 6:59am 23 May 2024 - 🇮🇳India dev16.addweb
Looks good @kenyoowen,
If we need to restrict access by role, we can use the following method. However, using permissions (as shown in #4) is more convenient because it allows us to give access to any user based on their permissions.
metatag_ai.admin_settings: path: '/admin/config/content/metatag-ai' defaults: _form: '\Drupal\metatag_ai\Form\MetatagAIConfigForm' _title: 'Metatag AI Settings' requirements: _role: 'administrator' metatag_ai.content_settings: path: '/admin/config/content/metatag-ai/content-type' defaults: _form: '\Drupal\metatag_ai\Form\MetatagAISettingsForm' _title: 'Metatag Content Type Settings' requirements: _role: 'administrator+content_editor'
- Status changed to Needs work
6 months ago 9:11am 21 June 2024 - 🇮🇳India ankitv18
Please check below code: If we check at initial level of having a permission then only it could be useful for any further logic implementation.
/** * Implements hook_form_alter() for node form alter to add the Generator button. */ function metatag_ai_form_alter(&$form, FormStateInterface $form_state, $form_id) { if (\Drupal::currentUser()->hasPermission('administer metatag content')) { $form_object = $form_state->getFormObject(); if ($form_object instanceof NodeForm) { $content_type = $form_object->getEntity()->getType(); $selected_content_types = \Drupal::config('metatag_ai.content_settings')->get('metatag_ai.metadata_content_types'); if (!empty($selected_content_types) && in_array($content_type, $selected_content_types)) { $form['actions']['generate_metadata'] = [ '#type' => 'button', '#value' => t('Generate Metatag'), '#weight' => 4, '#ajax' => [ 'callback' => 'metatag_ai_generate_submit_form', 'event' => 'click', ], ]; } } } }
- 🇵🇭Philippines kenyoOwen
Hi ankitv18
Thank you for the review. I tested your code in comment #8 and it works the same as the current change, but I think we stick to the current change since it has better readability and is also more maintainable for me.
Thanks.
- 🇮🇳India ankitv18
@kenyoOwen you have added hasPermission check at low-level of code but if you add that check at starting of the method then it make more more meaningful rather initialising and iterating the lines of code and do nothing at end if user don't have that permission.
Right now the code block is having Code redundancy and code complexity.Rest is the maintainer call on this.