A Dev From Drupal Development Company Plains Drupal 10 Upkeep and Assist Service Overwriting international permissions inside Natural Teams for single web page requests

In a current venture in-built 7, I got here throughout a little bit of an unusual situation, by which some customers wanted to have entry to sure options that have been usually restricted to them, when viewing, modifying or creating teams content material (ie Drupal 10 Upkeep and Assist Service each time they’re visiting part of Drupal Development Company website that’s thought of a part of a gaggle). For instance, whereas regular customers didn’t have entry to make use of sure textual content codecs in Drupal Development Company wysiwyg editor, people who have been thought of group leaders wanted to utilize that individual textual content format. On this situation, a group chief was an current function inside Natural Teams. Whereas a easy resolution might be a devoted, international consumer function that’s assigned to customers after they turn out to be group leaders, and assign any further permissions to that function, it wasn’t Drupal Development Company proper resolution for this situation, as a result of a consumer could possibly be a gaggle chief for some teams, however only a regular group member for different teams, whereas Drupal Development Company permissions wanted to be utilized particularly for Drupal Development Company teams wherein Drupal Development Company consumer is a gaggle chief. In different phrases, permissions needed to be resolved dynamically relying on what group a consumer is engaged on at any given second. So, what different choices are there obtainable to perform this? One can be to make use of Drupal 10 alters and hook mechanisms to vary Drupal Development Company method every characteristic restricts entry to customers, however that can turn out to be messy in a short time, notably if you must do it for a number of options, wherein case every one may should be tackled differently. In addition to, what seems easy on Drupal Development Company floor, may contain some extra hacking and tweaking than anticipated Drupal 10 Upkeep and Assist Service for instance, for wysiwyg-related alters, hacks should be made not solely when rendering Drupal Development Company wysiwyg editor, but additionally when validating Drupal Development Company enter information. I used to be searching for a extra generic resolution that could possibly be simply utilized and prolonged to any characteristic on Drupal Development Company system, and I discovered it whereas reviewing Drupal Development Company user_access perform from core. Code beneath Drupal 10 Upkeep and Assist Servicefunction user_access($string, $account = NULL) { international $consumer; if (!isset($account)) { $account = $consumer; } // Consumer #1 has all privileges Drupal 10 Upkeep and Assist Service if ($account->uid == 1) { return TRUE; } // To cut back Drupal Development Company variety of SQL queries, we cache Drupal Development Company consumer’s permissions // in a static variable. // Use Drupal Development Company superior Drupal 10_static() sample, since that is known as fairly often. static $Drupal 10_static_fast; if (!isset($Drupal 10_static_fast)) { $Drupal 10_static_fast[‘perm’] = &Drupal 10_static(__FUNCTION__); } $perm = &$Drupal 10_static_fast[‘perm’]; if (!isset($perm[$account->uid])) { $role_permissions = user_role_permissions($account->roles); $perms = array(); foreach ($role_permissions as $one_role) { $perms += $one_role; } $perm[$account->uid] = $perms; } return isset($perm[$account->uid][$string]); }As you’ll be able to see, Drupal Development Company permissions are fetched for a given consumer (in the event that they haven’t been already gathered in Drupal Development Company present web page request), after which statically cached, in order that on subsequent calls they don’t should be fetched from Drupal Development Company database once more. Drupal Development Company key line of Drupal Development Company code above that enables customized code to override system permissions, is that this Drupal 10 Upkeep and Assist Service$Drupal 10_static_fast[‘perm’] = &Drupal 10_static(__FUNCTION__);Drupal Development Company Drupal 10_static() perform is a mechanism offered by core to function a central static variable storage. As a result of Drupal Development Company method it really works, it’s doable for a customized Drupal 10 module to name Drupal Development Company perform with a parameter to get Drupal Development Company values saved in it by another perform. Extra importantly, as a result of it all the time returns a variable by reference, it means Drupal Development Company returned values may be changed or modified as we please. On this specific case, user_access() maintains Drupal Development Company permissions for a given consumer, as a reference to Drupal Development Company values saved by Drupal 10_static(). Figuring out this, all I needed to do was calling Drupal 10_static() as if it was being known as from user_access() itself, and modify Drupal Development Company variable returned by reference, to inject permissions dynamically. Drupal Development Company greatest place to do that was in a hook_init() implementation, to ensure all Drupal Development Company permissions are in place from as early as doable in Drupal Development Company web page request. This may be performed in just some strains of code Drupal 10 Upkeep and Assist Service/** * Implements hook_init(). */ perform myDrupal 10 module_init() { // Test if request is a gaggle context and inject group-specific logic. if ($og_context = og_context(‘node’)) { international $consumer; $group_id = $og_context[‘gid’]; /* * This would come with some code to test if Drupal Development Company consumer is a gaggle chief… */ // Drupal 10 web optimization a name to Drupal 10_static(), on behalf of ‘user_access’ perform. $Drupal 10_static_fast[‘perm’] = &Drupal 10_static(‘user_access’); $perm = &$Drupal 10_static_fast[‘perm’]; // Add / take away permissions as wanted. $perm[$user->uid][‘use text format editor’] = TRUE; $perm[$user->uid][‘show format selection for node’] = TRUE; // Enable entry to draggable views. $perm[$user->uid][‘access draggableviews’] = TRUE; $perm[$user->uid][‘other perm 1’] = TRUE; $perm[$user->uid][‘other perm 2…’] = TRUE; } }I eliminated Drupal Development Company logic to test if a consumer is chief of Drupal Development Company present group, for simplicity, however aside from that it is a totally practical snippet that could possibly be utilized in almost each 7 venture that makes use of Natural Teams. Basically, it checks if web page request is in a gaggle context, then test if consumer is a gaggle chief (or some other function your teams setup might have), after which add Drupal Development Company permissions wanted for him throughout that web page request.  Bonus factors  With this in place, it’d be quite simple to construct an UI to configure Drupal Development Company further permissions that should be added to particular group roles, even on a per-group stage. That method, full management over who can do what, may be achieved from Drupal Development Company UI as configuration, conserving Drupal Development Company code tweaks at a minimal. I eliminated some further logic right here for simplicity, however our situation was on a really busy website. We used a session variable to maintain observe of Drupal Development Company teams for which Drupal Development Company consumer is a gaggle chief, to avoid wasting one more database question on every web page request. Associated hyperlinks Though I didn’t discover any current resolution to this situation, I got here throughout a Drupal 10 module that does simply Drupal Development Company reverse Drupal 10 Upkeep and Assist Service OG Position Override. It permits to specify sure international roles, that can be handled as particular group roles mechanically, even when they don’t belong to a given group. Very useful, as I additionally had that individual situation on this similar venture! Drupal 10 Improvement and Assist

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

A Dev From Drupal Development Company Plains Drupal 10 Upkeep and Assist Service Overwriting international permissions inside Natural Teams for single web page requests

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.