You are here

class MembersPlugin in Opigno Learning path 3.x

Same name and namespace in other branches
  1. 8 src/Plugin/LearningPathMembers/MembersPlugin.php \Drupal\opigno_learning_path\Plugin\LearningPathMembers\MembersPlugin

Class MembersPlugin.

Plugin annotation


@LearningPathMembers(
  id="members_plugin",
)

Hierarchy

Expanded class hierarchy of MembersPlugin

File

src/Plugin/LearningPathMembers/MembersPlugin.php, line 16

Namespace

Drupal\opigno_learning_path\Plugin\LearningPathMembers
View source
class MembersPlugin extends LearningPathMembersPluginBase {

  /**
   * {@inheritdoc}
   */
  public function getMembersForm(array &$form, FormStateInterface $form_state, User $current_user) {
    $storage = $form_state
      ->getStorage();

    // If user can add any other users or only from his groups.
    $show_all = $current_user
      ->hasPermission('add any members to calendar event') ? TRUE : FALSE;
    $storage['show_all'] = $show_all;

    // Add filters for the members field.
    $form['members'] = [
      '#type' => 'container',
      '#weight' => 100,
    ];
    $form['members']['title'] = [
      '#type' => 'label',
      '#title' => t('Members'),
    ];

    // Get the users for the specific group.
    $users = opigno_messaging_get_all_recipients($show_all);
    $allowed_uids = [];
    foreach ($users as $user) {
      $allowed_uids[] = $user
        ->id();
    }
    if ($allowed_uids) {
      $allowed_uids = array_unique($allowed_uids);

      // Save to form storage.
      $storage['allowed_uids'] = $allowed_uids;

      // Filter allowed users.
      if ($options = $form["field_calendar_event_members"]["widget"]["#options"]) {
        foreach ($options as $key => $option) {
          if (!in_array($key, $allowed_uids)) {
            unset($form["field_calendar_event_members"]["widget"]["#options"][$key]);
          }
        }
      }
    }
    $form['members']['field_calendar_event_members'] = $form['field_calendar_event_members'];
    unset($form['field_calendar_event_members']);
    $members =& $form['members']['field_calendar_event_members'];
    $members["widget"]['#type'] = 'entity_selector';
    $members['#prefix'] = '<div id="members">';
    $members['#suffix'] = '</div>';
    unset($members['widget']['#title']);
    $form_state
      ->setStorage($storage);
    if (!$current_user
      ->hasPermission('add members to calendar event')) {

      // Hide calendar events members field.
      if (!empty($form["field_calendar_event_members"])) {
        $form["field_calendar_event_members"]["#access"] = FALSE;
      }
      if (!empty($form['members'])) {
        $form['members']['#access'] = FALSE;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LearningPathMembersPluginBase::getGroupMembersIds public function Returns group members uids.
LearningPathMembersPluginBase::getId public function Get plugin id. Overrides LearningPathMembersPluginInterface::getId
LearningPathMembersPluginBase::__construct public function LearningPathMembersPluginBase constructor. Overrides PluginBase::__construct
MembersPlugin::getMembersForm public function Get members form. Overrides LearningPathMembersPluginBase::getMembersForm
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.