Commit f8e2088d authored by Carsten Logemann's avatar Carsten Logemann Committed by Carsten Logemann
Browse files

Issue #3318149 by C-Logemann: Add UI for grant role invitations, first steps.

parent 008fdd9c
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
administer grant:
  title: 'Administer grant configuration'
  restrict access: true

access grant invite:
  title: 'Access grant invite'
  restrict access: true
+11 −1
Original line number Diff line number Diff line
@@ -31,9 +31,19 @@ entity.grant_config.delete_form:
    _permission: 'administer grant'

entity.grant.settings:
  path: 'admin/structure/grant'
  path: '/admin/structure/grant'
  defaults:
    _form: '\Drupal\grant\Form\GrantSettingsForm'
    _title: 'Grant'
  requirements:
    _permission: 'administer grant'

grant.grant_invite:
  path: '/grant/invite/{type}/{uuid}'
  defaults:
    _title: 'Grant invite'
    _form: 'Drupal\grant\Form\GrantInviteForm'
  requirements:
    _grant_access_check: 'access grant invite'
  options:
    no_cache: TRUE
+109 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\grant\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\Role;

/**
 * Provides a Grant form.
 */
class GrantInviteForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'grant_grant_invite';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $type = '', $uuid='') {
    $parameter = [];

    $current_user_id = \Drupal::currentUser()->id();
    $current_user = \Drupal\user\Entity\User::load($current_user_id);
    $current_user_uuid = $current_user->uuid();

    $parameter['entity_type'] = $type;
    $parameter['entity_uuid'] = $uuid;
    $parameter['user'] =  $current_user_uuid;

    $roles = Role::loadMultiple();

    foreach ($roles as $key => $role) {
       $value = $role->label();
       $roles_select[$key] = $value;
    }

    $form['roles'] = array(
      '#title' => 'Grant Role',
      '#type' => 'select',
      '#key_type' => 'associative',
      '#options' => $roles_select,
      '#description' => $this->t('Which role you want to assign.'),
       '#weight' => 1,
    );

    $form['email'] = [
      '#type' => 'email',
      '#title' => $this->t('Grant email address'),
      '#description' => $this->t('Email address of a person you want to invite.'),
      '#weight' => 0,
    ];

    $form['note'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Note'),
      '#description' => $this->t('Optional adminitration note.'),
    ];

    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Invite user'),
    ];

    $form_state->setStorage($parameter);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $parameter = $form_state->getStorage();
    // ToDo: Do validation
//    if (mb_strlen($form_state->getValue('message')) < 10) {
//      $form_state->setErrorByName('message', $this->t('Message should be at least 10 characters.'));
//    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $parameter = $form_state->getStorage();
    $form_values = $form_state->getValues();

    $form_values['mail'];
    $grant = \Drupal\grant\Entity\Grant::create([
        'status' => 0,
        'user' => $current_user_uuid,
        'email' => $form_values['email'],
        'entity_type' => $parameter['entity_type'],
        'entity_uuid' => $parameter['entity_uuid'],
        'timer' => 1,
      ]);

    $grant->save();
    $this->messenger()->addStatus($this->t('The grant invitation has been saved.'));
    $form_state->setRedirect('<front>');
  }

}