Skip to content

Commit

Permalink
added feature to hide element based on write_permission attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickkaleta committed Oct 21, 2019
1 parent 55ae328 commit 0240a93
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions csis_helpers.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,13 @@ update_step_relations:
- core/drupal
- core/drupal.ajax
- core/drupalSettings

permission_control:
js:
js/csis_permission_control.js:
dependencies:
- core/jquery
- core/jquery.once
- core/drupal
- core/drupal.ajax
- core/drupalSettings
1 change: 1 addition & 0 deletions csis_helpers.module
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function csis_helpers_group_view(array &$build, EntityInterface $entity, EntityV
// DEPRECATED: use studyInfo/datapackageInfo/resourceInfo instead!
$build['#attached']['drupalSettings']['csisHelpers']['entityinfo'] = csis_helpers_group_entity_info($entity);
$build['#attached']['library'][] = 'csis_helpers/entityinfo_helpers';
$build['#attached']['library'][] = 'csis_helpers/permission_control';

$build['#attached']['drupalSettings']['csisHelpers']['studyInfo'] = csis_helpers_group_entity_info($entity);
}
Expand Down
27 changes: 27 additions & 0 deletions js/csis_permission_control.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(function ($, Drupal, drupalSettings) {

if (!drupalSettings.csisHelpers) {
return;
}

Drupal.behaviors.csis_permission_control = {
attach: function (context, settings) {
// search for all elements containing that class (will be buttons or links)
$('.token-field-check-permissions', context).once('checkPermissions').each(function () {
//console.debug('checking permissions');

// get write permissions for current user based on their role in the group
var writePermissions = drupalSettings.csisHelpers.studyInfo.write_permissions

// if user can't write to this group/node then hide the element
// hiding is in this case enough since Drupal itself will also check permissions
// when a form is called and block users without necessary permissions
if (writePermissions == 0) {
$(this).hide();
}

});
}
};
})(jQuery, Drupal, drupalSettings);

0 comments on commit 0240a93

Please sign in to comment.