Commit 3e86b3ab authored by Nikolay Lobachev's avatar Nikolay Lobachev Committed by Nikolay Lobachev
Browse files

Issue #3165797 by LOBsTerr: Request with Role

parent 05627942
Loading
Loading
Loading
Loading
+42 −4
Changes for src/Entity/Form/GroupMembershipApproveForm.php: 42 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -49,6 +49,25 @@ class GroupMembershipApproveForm extends ContentEntityConfirmFormBase {
    return $this->t('Approve');
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    $roles = $this->getGroupRoles();
    if (!empty($roles)) {
      $form['roles'] = [
        '#type' => 'checkboxes',
        '#title' => $this->t('Roles'),
        '#description' => $this->t('These roles will be assigned to user when membership request is approved'),
        '#options' => $roles
      ];
    }

    return $form;
  }

  /**
   * {@inheritdoc}
   */
@@ -63,10 +82,7 @@ class GroupMembershipApproveForm extends ContentEntityConfirmFormBase {

    if ($result) {
      $this->messenger()->addStatus($this->t('Membership request approved'));

      // Add roles if we have them in plugin configuration.
      $configuration = $group_content->getContentPlugin()->getConfiguration();
      $group_roles = !empty($configuration['roles']) ? array_filter($configuration['roles']) : [];
      $group_roles = $form_state->getValue('roles');
        // Adding user to a group.
      $group_content->getGroup()->addMember($group_content->getEntity(), [
        'group_roles' => array_values($group_roles)
@@ -84,4 +100,26 @@ class GroupMembershipApproveForm extends ContentEntityConfirmFormBase {
    $form_state->setRedirectUrl($this->getCancelUrl());
  }

  /**
   * Get roles.
   *
   * @return array|null
   *   List of group type's custom roles.
   */
  protected function getGroupRoles() {
    $options = [];
    $properties = [
      'group_type' => $this->getEntity()->getGroup()->getGroupType()->id(),
      'internal' => FALSE,
    ];

    $roles = $this->entityTypeManager
      ->getStorage('group_role')
      ->loadByProperties($properties);
    foreach ($roles as $role) {
      $options[$role->id()] = $role->label();
    }
    return $options;
  }

}
+5 −82
Changes for src/Plugin/GroupContentEnabler/GroupMembershipRequest.php: 5 added lines, 82 removed lines.
Original line number Diff line number Diff line
@@ -2,10 +2,7 @@

namespace Drupal\grequest\Plugin\GroupContentEnabler;

use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\group\Access\GroupAccessResult;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Entity\GroupContentInterface;
@@ -14,7 +11,6 @@ use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a content enabler for users.
@@ -26,14 +22,15 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
 *   entity_type_id = "user",
 *   pretty_path_key = "request",
 *   reference_label = @Translation("Username"),
 *   reference_description = @Translation("The name of the user you want to make a member"),
 *   handlers = {
 *     "permission_provider" = "Drupal\grequest\Plugin\GroupMembershipRequestPermissionProvider",
 *   reference_description = @Translation("The name of the user you want to
 *   make a member"), handlers = {
 *     "permission_provider" =
 *   "Drupal\grequest\Plugin\GroupMembershipRequestPermissionProvider",
 *   },
 *   admin_permission = "administer members"
 * )
 */
class GroupMembershipRequest extends GroupContentEnablerBase implements ContainerFactoryPluginInterface {
class GroupMembershipRequest extends GroupContentEnablerBase {

  /**
   * Request created and waiting for administrator's response.
@@ -50,47 +47,6 @@ class GroupMembershipRequest extends GroupContentEnablerBase implements Containe
   */
  const REQUEST_REJECTED = 2;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a GroupMembershipRequest object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   * @param array $configuration
   * @param string $plugin_id
   * @param mixed $plugin_definition
   *
   * @return static
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity_type.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
@@ -225,40 +181,7 @@ class GroupMembershipRequest extends GroupContentEnablerBase implements Containe
    $form['entity_cardinality']['#disabled'] = TRUE;
    $form['entity_cardinality']['#description'] .= '<br /><em>' . $info . '</em>';

    $roles = $this->getGroupRoles();
    if (!empty($roles)) {
      $form['roles'] = [
        '#type' => 'checkboxes',
        '#title' => $this->t('Roles'),
        '#description' => $this->t('These roles will be assigned to user when membership request is approved'),
        '#options' => $roles,
        '#default_value' => $this->configuration['roles'],
      ];
    }

    return $form;
  }

  /**
   * Get roles.
   *
   * @return array|null
   *   List of group type's custom roles.
   */
  protected function getGroupRoles() {
    $options = [];
    $properties = [
      'group_type' => $this->getGroupTypeId(),
      'internal' => FALSE,
    ];

    $roles = $this->entityTypeManager
      ->getStorage('group_role')
      ->loadByProperties($properties);
    foreach ($roles as $role) {
      $options[$role->id()] = $role->label();
    }
    return $options;
  }

}