function PermissionsPerBundle::apply

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/PermissionsPerBundle.php \Drupal\Core\Config\Action\Plugin\ConfigAction\PermissionsPerBundle::apply()

Applies the config action.

Parameters

string $configName: The name of the config to apply the action to.

mixed $value: The value for the action to use.

Overrides ConfigActionPluginInterface::apply

File

core/lib/Drupal/Core/Config/Action/Plugin/ConfigAction/PermissionsPerBundle.php, line 52

Class

PermissionsPerBundle
@internal This API is experimental.

Namespace

Drupal\Core\Config\Action\Plugin\ConfigAction

Code

public function apply(string $configName, mixed $value) : void {
  $role = $this->configManager
    ->loadConfigEntityByName($configName);
  if (!$role instanceof RoleInterface) {
    throw new ConfigActionException(sprintf("Cannot determine role from %s", $configName));
  }
  assert(is_string($value) || is_array($value));
  [
    $permissions,
    $except_bundles,
  ] = self::parseValue($value);
  if (empty($permissions) || !Inspector::assertAllMatch('%bundle', $permissions, TRUE)) {
    throw new ConfigActionException(sprintf("The permissions provided %s must be an array of strings that contain '%%bundle'.", var_export($value, TRUE)));
  }
  $bundles = $this->entityTypeBundleInfo
    ->getBundleInfo($this->targetEntityType);
  foreach (array_keys($bundles) as $bundle_id) {
    if (in_array($bundle_id, $except_bundles, TRUE)) {
      continue;
    }
    /** @var string[] $actual_permissions */
    $actual_permissions = str_replace('%bundle', $bundle_id, $permissions);
    array_walk($actual_permissions, $role->grantPermission(...));
  }
  $role->save();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.