Limit a poll to a specific role

Created on 15 December 2024, about 1 month ago

In the configuration of a survey, it is necessary to select the roles that can:

- Answer the survey.
- View the survey.
- View the results of the survey.

Currently it is possible all surveys for a role, but not for a particular survey.

Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇫🇷France zenimagine

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @zenimagine
  • 🇫🇷France Tritof

    I join you in this request @zenimage.
    In the meantime, I've used this piece of code that responded to the need. There is probably a better way to do it, but this works.
    Basically, it compares current user roles with an array of roles that aren't allowed to vote. If there is any match, it removes the poll and just exposes the results instead.

    /**
     * Implements hook_form_FORM_ID_alter for polls.
     */
    function customer_specifics_form_poll_view_form_alter(&$form, FormStateInterface $form_state, $form_id): void {
      $currentUserRoles = \Drupal::currentUser()->getRoles();
      $roles = ['role_1', 'role_2'];
      $diff = array_intersect($currentUserRoles, $roles);
      if(!empty($diff)) {
        $poll = \Drupal::entityTypeManager()->getStorage('poll')->load(substr($form_id, -1));
        $pollViewForm = \Drupal::service('class_resolver')->getInstanceFromDefinition(PollViewForm::class);
        $results = $pollViewForm->showPollResults($poll);
        $form['choice'] = [];
        $form['actions'] = [];
        $form['results'] =  $results;
      }
    }
Production build 0.71.5 2024