Commit 3e4974e2 authored by Nikolay Lobachev's avatar Nikolay Lobachev
Browse files

Issue #3304797: Replace GroupEnabler with Group Relation plugin. Create...

Issue #3304797: Replace GroupEnabler with Group Relation plugin. Create necessary handler for the plugin.
parent 5074565b
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -2,3 +2,24 @@ services:
  grequest.membership_request_manager:
    class: 'Drupal\grequest\MembershipRequestManager'
    arguments: ['@entity_type.manager', '@current_user']

  # Specific group relation handlers.
  group.relation_handler.entity_reference.group_membership_request:
    class: 'Drupal\grequest\Plugin\Group\RelationHandler\GroupMembershipEntityReference'
    arguments: [ '@group.relation_handler.entity_reference' ]
    shared: false

  group.relation_handler.operation_provider.group_membership_request:
    class: 'Drupal\grequest\Plugin\Group\RelationHandler\GroupMembershipOperationProvider'
    arguments: [ '@group.relation_handler.operation_provider', '@current_user', '@string_translation' ]
    shared: false

  group.relation_handler.permission_provider.group_membership_request:
    class: 'Drupal\grequest\Plugin\Group\RelationHandler\GroupMembershipPermissionProvider'
    arguments: [ '@group.relation_handler.permission_provider' ]
    shared: false

  group.relation_handler.post_install.group_membership_request:
    class: 'Drupal\grequest\Plugin\Group\RelationHandler\GroupMembershipPostInstall'
    arguments: [ '@group.relation_handler.post_install', '@entity_type.manager', '@string_translation' ]
    shared: false
+5 −96
Original line number Diff line number Diff line
<?php

namespace Drupal\grequest\Plugin\GroupContentEnabler;
namespace Drupal\grequest\Plugin\Group\Relation;

use Drupal\Core\Form\FormStateInterface;
use Drupal\group\Access\GroupAccessResult;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Entity\GroupContentInterface;
use Drupal\group\Plugin\GroupContentEnablerBase;
use Drupal\group\Plugin\Group\Relation\GroupRelationBase;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Session\AccountInterface;

/**
 * Provides a content enabler for users.
 * Provides a group relation for users as group membership request.
 *
 * @GroupContentEnabler(
 *   id = "group_membership_request",
@@ -23,13 +23,10 @@ use Drupal\Core\Session\AccountInterface;
 *   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",
 *   },
 *   admin_permission = "administer membership requests"
 * )
 */
class GroupMembershipRequest extends GroupContentEnablerBase {
class GroupMembershipRequest extends GroupRelationBase {

  /**
   * Transition id for approval.
@@ -71,26 +68,6 @@ class GroupMembershipRequest extends GroupContentEnablerBase {
   */
  const REQUEST_REJECTED = 'rejected';

  /**
   * {@inheritdoc}
   */
  public function getGroupOperations(GroupInterface $group) {
    $account = \Drupal::currentUser();
    $operations = [];

    $entity_instances = $group->getContentByEntityId($this->getPluginId(), $account->id());
    $url = $group->toUrl('group-request-membership');
    if($url->access($account) && count($entity_instances) == 0){
      $operations['group-request-membership'] = [
        'title' => $this->t('Request group membership'),
        'url' => $url,
        'weight' => 99,
      ];
    }

    return $operations;
  }

  /**
   * {@inheritdoc}
   */
@@ -119,74 +96,6 @@ class GroupMembershipRequest extends GroupContentEnablerBase {
    return GroupAccessResult::allowedIfHasGroupPermission($group_content->getGroup(), $account, 'administer membership requests');
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityReferenceSettings() {
    $settings = parent::getEntityReferenceSettings();
    $settings['handler_settings']['include_anonymous'] = FALSE;
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  public function postInstall() {
    if (!\Drupal::isConfigSyncing()) {
      $group_content_type_id = $this->getContentTypeConfigId();

      // Add Status field.
      FieldConfig::create([
        'field_storage' => FieldStorageConfig::loadByName('group_content', GroupMembershipRequest::STATUS_FIELD),
        'bundle' => $group_content_type_id,
        'label' => $this->t('Request status'),
        'required' => TRUE,
        'settings' => [
          'workflow' => 'request',
          'workflow_callback' => '',
        ],
      ])->save();

      // Add "Updated by" field, to save reference to
      // user who approved/denied request.
      FieldConfig::create([
        'field_storage' => FieldStorageConfig::loadByName('group_content', 'grequest_updated_by'),
        'bundle' => $group_content_type_id,
        'label' => $this->t('Approved/Rejected by'),
        'settings' => [
          'handler' => 'default',
          'target_bundles' => NULL,
        ],
      ])->save();

      // Build the 'default' display ID for both the entity form and view mode.
      $default_display_id = "group_content.$group_content_type_id.default";
      // Build or retrieve the 'default' view mode.
      if (!$view_display = EntityViewDisplay::load($default_display_id)) {
        $view_display = EntityViewDisplay::create([
          'targetEntityType' => 'group_content',
          'bundle' => $group_content_type_id,
          'mode' => 'default',
          'status' => TRUE,
        ]);
      }

      // Assign display settings for the 'default' view mode.
      $view_display
        ->setComponent('grequest_status', [
          'type' => 'list_default',
        ])
        ->setComponent('grequest_updated_by', [
          'label' => 'above',
          'type' => 'entity_reference_label',
          'settings' => [
            'link' => 1,
          ],
        ])
        ->save();
    }
  }

  /**
   * {@inheritdoc}
   */
@@ -204,7 +113,7 @@ class GroupMembershipRequest extends GroupContentEnablerBase {

    // Disable the entity cardinality field as the functionality of this module
    // relies on a cardinality of 1. We don't just hide it, though, to keep a UI
    // that's consistent with other content enabler plugins.
    // that's consistent with other group relations.
    $info = $this->t("This field has been disabled by the plugin to guarantee the functionality that's expected of it.");
    $form['entity_cardinality']['#disabled'] = TRUE;
    $form['entity_cardinality']['#description'] .= '<br /><em>' . $info . '</em>';
+56 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\grequest\Plugin\Group\RelationHandler;

use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\group\Entity\GroupInterface;

/**
 * Provides operations for the group_membership_request relation plugin.
 */
class GroupMembershipOperationProvider implements OperationProviderInterface {

  use OperationProviderTrait;

  /**
   * Constructs a new GroupMembershipOperationProvider.
   *
   * @param \Drupal\group\Plugin\Group\RelationHandler\OperationProviderInterface $parent
   *   The default operation provider.
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current user.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(OperationProviderInterface $parent, AccountProxyInterface $current_user, TranslationInterface $string_translation) {
    $this->parent = $parent;
    $this->currentUser = $current_user;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public function getGroupOperations(GroupInterface $group) {
    $operations = $this->parent->getGroupOperations($group);

    $account = $this->currentUser();
    $entity_instances = $group->getContentByEntityId($this->getPluginId(), $account->id());
    $url = $group->toUrl('group-request-membership');
    if ($url->access($account) && count($entity_instances) == 0) {
      $operations['group-request-membership'] = [
        'title' => $this->t('Request group membership'),
        'url' => $url,
        'weight' => 99,
      ];
    }

    // @todo With the new VariationCache, we can use the above context.
    $operations['#cache']['contexts'] = ['user'];

    return $operations;
  }

}
+35 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\group\Plugin\Group\RelationHandler;

use Drupal\Core\Field\BaseFieldDefinition;

/**
 * Configures the entity reference for the group_membership_request relation plugin.
 */
class GroupMembershipRequestEntityReference implements EntityReferenceInterface {

  use EntityReferenceTrait;

  /**
   * Constructs a new GroupMembershipEntityReference.
   *
   * @param \Drupal\group\Plugin\Group\RelationHandler\EntityReferenceInterface $parent
   *   The default entity reference handler.
   */
  public function __construct(EntityReferenceInterface $parent) {
    $this->parent = $parent;
  }

  /**
   * {@inheritdoc}
   */
  public function configureField(BaseFieldDefinition $entity_reference) {
    $this->parent->configureField($entity_reference);

    $handler_settings = $entity_reference->getSetting('handler_settings');
    $handler_settings['include_anonymous'] = FALSE;
    $entity_reference->setSetting('handler_settings', $handler_settings);
  }

}
+116 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\grequest\Plugin\Group\RelationHandler;

use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\grequest\Plugin\Group\Relation\GroupMembershipRequest;
use Drupal\group\Entity\GroupRelationshipTypeInterface;

/**
 * Provides post install tasks for the group_membership relation plugin.
 */
class GroupMembershipRequestPostInstall implements PostInstallInterface {

  use PostInstallTrait;
  use StringTranslationTrait;

  /**
   * Constructs a new GroupMembershipRequestPostInstall.
   *
   * @param \Drupal\group\Plugin\Group\RelationHandler\PostInstallInterface $parent
   *   The default post install handler.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(PostInstallInterface $parent, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
    $this->parent = $parent;
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslation = $string_translation;
  }

  /**
   * {@inheritdoc}
   */
  public function getInstallTasks() {
    $tasks = $this->parent->getInstallTasks();
    $tasks['install-group-roles-field'] = [$this, 'installGroupRolesField'];
    return $tasks;
  }

  /**
   * Installs the group_roles field.
   *
   * @param \Drupal\group\Entity\GroupRelationshipTypeInterface $relationship_type
   *   The GroupRelationshipType created by installing the plugin.
   * @param $is_syncing
   *   Whether config is syncing.
   */
  public function installGroupRolesField(GroupRelationshipTypeInterface $relationship_type, $is_syncing) {

    // Only create config objects while config import is not in progress.
    if ($is_syncing === TRUE) {
      return;
    }

    $relationship_type_id = $relationship_type->id();

    // Add Status field.
    FieldConfig::create([
      'field_storage' => FieldStorageConfig::loadByName('group_content', GroupMembershipRequest::STATUS_FIELD),
      'bundle' => $relationship_type_id,
      'label' => $this->t('Request status'),
      'required' => TRUE,
      'settings' => [
        'workflow' => 'request',
        'workflow_callback' => '',
      ],
    ])->save();

    // Add "Updated by" field, to save reference to
    // user who approved/denied request.
    FieldConfig::create([
      'field_storage' => FieldStorageConfig::loadByName('group_content', 'grequest_updated_by'),
      'bundle' => $relationship_type_id,
      'label' => $this->t('Approved/Rejected by'),
      'settings' => [
        'handler' => 'default',
        'target_bundles' => NULL,
      ],
    ])->save();

    // Build the 'default' display ID for both the entity form and view mode.
    $default_display_id = "group_content.$relationship_type_id.default";
    // Build or retrieve the 'default' view mode.
    if (!$view_display = EntityViewDisplay::load($default_display_id)) {
      $view_display = EntityViewDisplay::create([
        'targetEntityType' => 'group_content',
        'bundle' => $relationship_type_id,
        'mode' => 'default',
        'status' => TRUE,
      ]);
    }

    // Assign display settings for the 'default' view mode.
    $view_display
      ->setComponent('grequest_status', [
        'type' => 'list_default',
      ])
      ->setComponent('grequest_updated_by', [
        'label' => 'above',
        'type' => 'entity_reference_label',
        'settings' => [
          'link' => 1,
        ],
      ])
      ->save();

  }

}