diff --git a/src/Plugin/EntityReferenceSelection/GroupTypeRoleSelection.php b/src/Plugin/EntityReferenceSelection/GroupTypeRoleSelection.php index 0014f3e..2a8af9e 100644 --- a/src/Plugin/EntityReferenceSelection/GroupTypeRoleSelection.php +++ b/src/Plugin/EntityReferenceSelection/GroupTypeRoleSelection.php @@ -3,62 +3,84 @@ namespace Drupal\group\Plugin\EntityReferenceSelection; use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection; +use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Form\FormStateInterface; /** * Only shows the group roles which are available for a group type. * - * The only handler setting is 'group_type_id', a required string that points + * The first handler setting is 'group_type_id', a required string that points * to the ID of the group type for which this handler will be run. * + * The second is 'include_internal_roles', optional boolean flag that defines + * whether to include internal group roles (Member, Outsider etc.) in the list. + * Defaults to 0 - internal roles are not included. + * * @EntityReferenceSelection( * id = "group_type:group_role", * label = @Translation("Group type role selection"), * entity_types = {"group_role"}, * group = "group_type", - * weight = 0, * base_plugin_label = @Translation("Group: Roles"), + * weight = 0, * ) */ class GroupTypeRoleSelection extends DefaultSelection { - /** - * {@inheritdoc} - */ - public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + public function buildConfigurationForm(array $form, FormStateInterface $form_state): array { $form = parent::buildConfigurationForm($form, $form_state); - // Autocreation is not supported. + + // Auto-creation is not supported. unset($form['auto_create']); + $group_types = $this->entityTypeManager->getStorage('group_type')->loadMultiple(); $group_type_options = []; + if ($group_types) { $group_type_options[''] = $this->t('-Select group type-'); + foreach ($group_types as $group_type_id => $group_type) { $group_type_options[$group_type_id] = $group_type->label(); } } + $form['group_type_id'] = [ '#type' => 'select', '#title' => $this->t('Group types'), '#options' => $group_type_options, '#default_value' => $this->configuration['group_type_id'] ?? '', '#access' => count($group_type_options) > 1, - '#weight' => -1, + '#weight' => -2, '#required' => TRUE, ]; + + $form['include_internal_roles'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Include internal group roles'), + '#default_value' => $this->configuration['include_internal_roles'] ?? 0, + '#weight' => -1, + ]; + return $form; } + /** * {@inheritdoc} */ - protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') { + protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS'): QueryInterface { + $query = parent::buildEntityQuery($match, $match_operator); + if (!empty($this->configuration['group_type_id'])) { $group_type_id = $this->configuration['group_type_id']; $query->condition('group_type', $group_type_id, '='); $query->sort('label'); } + if (empty($this->configuration['include_internal_roles'])) { + $query->condition('internal', 0, '='); + } + return $query; }