You are here

trait FlagPermissionsTrait in Flag 8.4

Trait for programmatically creating Flags.

Hierarchy

6 files declare their use of FlagPermissionsTrait
AjaxLinkNoJsTest.php in tests/src/Functional/AjaxLinkNoJsTest.php
AjaxLinkTest.php in tests/src/FunctionalJavascript/AjaxLinkTest.php
FlagContextualLinksTest.php in tests/src/FunctionalJavascript/FlagContextualLinksTest.php
FlagJsTestBase.php in tests/src/FunctionalJavascript/FlagJsTestBase.php
FlagTestBase.php in tests/src/Functional/FlagTestBase.php

... See full list

File

tests/src/Traits/FlagPermissionsTrait.php, line 12

Namespace

Drupal\Tests\flag\Traits
View source
trait FlagPermissionsTrait {

  /**
   * Grants flag and unflag permission to the given flag.
   *
   * @param \Drupal\flag\FlagInterface $flag
   *   The flag on which to grant permissions.
   * @param array|string $role_id
   *   (optional) The ID of the role to grant permissions. If omitted, the
   *   authenticated role is assumed.
   * @param bool $can_flag
   *   (optional) TRUE to grant the role flagging permission, FALSE to not grant
   *   flagging permission to the role. If omitted, TRUE is assumed.
   * @param bool $can_unflag
   *   Optional TRUE to grant the role unflagging permission, FALSE to not grant
   *   unflagging permission to the role. If omitted, TRUE is assumed.
   */
  protected function grantFlagPermissions(FlagInterface $flag, $role_id = RoleInterface::AUTHENTICATED_ID, $can_flag = TRUE, $can_unflag = TRUE) {

    // Grant the flag permissions to the authenticated role, so that both
    // users have the same roles and share the render cache.
    $role = Role::load($role_id);
    if ($can_flag) {
      $role
        ->grantPermission('flag ' . $flag
        ->id());
    }
    if ($can_unflag) {
      $role
        ->grantPermission('unflag ' . $flag
        ->id());
    }
    $role
      ->grantPermission('access contextual links');
    $role
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlagPermissionsTrait::grantFlagPermissions protected function Grants flag and unflag permission to the given flag.