You are here

public function RatePermissions::permissions in Rate 8.2

Same name and namespace in other branches
  1. 8 src/RatePermissions.php \Drupal\rate\RatePermissions::permissions()

Get permissions for Rate module.

Return value

array Permissions array.

1 string reference to 'RatePermissions::permissions'
rate.permissions.yml in ./rate.permissions.yml
rate.permissions.yml

File

src/RatePermissions.php, line 75

Class

RatePermissions
Class to return permissions based on entity type for rate module.

Namespace

Drupal\rate

Code

public function permissions() {
  $permissions = [];
  $widgets = $this->entityTypeManager
    ->getStorage('rate_widget')
    ->loadMultiple();

  // No need to continue without widgets.
  if (empty($widgets)) {
    return $permissions;
  }
  foreach ($widgets as $widget) {
    $entities = $widget
      ->get('entity_types');
    if ($entities && count($entities) > 0) {
      foreach ($entities as $entity) {
        $parameter = explode('.', $entity);
        $entity_type_id = $parameter[0];
        $bundle = $parameter[1];
        if ($bundle == $entity_type_id) {
          $perm_index = 'cast rate vote on ' . $entity_type_id . ' of ' . $bundle;
          $permissions[$perm_index] = [
            'title' => $this
              ->t('Can vote on :type', [
              ':type' => $entity_type_id,
            ]),
          ];
        }
        else {
          $perm_index = 'cast rate vote on ' . $entity_type_id . ' of ' . $bundle;
          $permissions[$perm_index] = [
            'title' => $this
              ->t('Can vote on :type type of :bundle', [
              ':bundle' => $bundle,
              ':type' => $entity_type_id,
            ]),
          ];
        }
      }
    }
    $comments = $widget
      ->get('comment_types');
    if ($this->commentManager && $comments && count($comments) > 0) {
      foreach ($comments as $comment) {
        $parameter = explode('.', $comment);
        $entity_type_id = $parameter[0];
        $bundle = $parameter[1];

        // Get the comment fields attached to the bundle.
        $fields = $this->commentManager
          ->getFields($parameter[0]);
        foreach ($fields as $fid => $field) {
          if (in_array($bundle, $field['bundles'])) {
            if ($bundle == $entity_type_id) {
              $perm_index = 'cast rate vote on ' . $fid . ' on ' . $entity_type_id . ' of ' . $bundle;
              $permissions[$perm_index] = [
                'title' => $this
                  ->t('Can vote on :comment on :type', [
                  ':comment' => $fid,
                  ':type' => $entity_type_id,
                ]),
              ];
            }
            else {
              $perm_index = 'cast rate vote on ' . $fid . ' on ' . $entity_type_id . ' of ' . $bundle;
              $permissions[$perm_index] = [
                'title' => $this
                  ->t('Can vote on :comment on :type type of :bundle', [
                  ':comment' => $fid,
                  ':type' => $entity_type_id,
                  ':bundle' => $bundle,
                ]),
              ];
            }
          }
        }
      }
    }
  }
  return $permissions;
}