Skip to content
Snippets Groups Projects
Commit 2067d00b authored by Eirik Morland's avatar Eirik Morland
Browse files

Issue #3426403 by eiriksm: Add a team settings page

parent 82d8807d
No related branches found
No related tags found
1 merge request!35Initial settings
Pipeline #113851 passed with warnings
<?php
namespace Drupal\violinist_teams\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\violinist_teams\TeamNode;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Controller that currently only controls the access.
*/
class TeamSettingsController extends ControllerBase {
/**
* Construct this controller.
*/
public function __construct(AccountProxyInterface $currentUser) {
$this->currentUser = $currentUser;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user')
);
}
/**
* Access callback for a couple things.
*/
public function accessTeamSettings(TeamNode $team) {
// Admins on the site should be able to access it.
if ($team->access('update', $this->currentUser)) {
return AccessResult::allowed();
}
return AccessResult::allowedIf(in_array($this->currentUser->id(), $team->getAdministratorIds()));
}
}
<?php
declare(strict_types=1);
namespace Drupal\violinist_teams\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Url;
use Drupal\violinist_teams\TeamNode;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a Violinist teams form.
*/
final class TeamSettingsForm extends FormBase {
/**
* Team node.
*
* @var \Drupal\violinist_teams\TeamNode
*/
protected TeamNode $team;
/**
* The constructor.
*/
public function __construct(CurrentRouteMatch $current_route_match) {
$this->routeMatch = $current_route_match;
$team = $this->routeMatch->getParameter('team');
if ($team instanceof TeamNode) {
$this->team = $team;
}
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_route_match')
);
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'violinist_teams_team_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$team = $this->team;
if (!$team instanceof TeamNode) {
return $form;
}
$form['slack'] = [
'#type' => 'details',
'#title' => $this->t('Slack settings'),
'#open' => TRUE,
];
$form['slack']['enable_slack_notifications'] = [
'#type' => 'checkbox',
'#default_value' => $team->isNotificationsEnabledForSlack(),
'#title' => $this->t('Enable Slack notifications'),
];
if (!$team->getTeamSlackWebhook()) {
$form['slack']['enable_slack_notifications']['#disabled'] = TRUE;
$form['slack']['enable_slack_notifications']['#description'] = $this->getSlackDescription();
}
$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
/**
* Helper to get that description.
*/
protected function getSlackDescription() {
return t('You must <a href="@url" class="underline">connect your user to slack</a> to enable this feature', [
'@url' => Url::fromRoute('league_oauth_login.login_controller_link', [
'provider_id' => 'slack',
], [
'query' => [
'team' => $this->team?->id() ?? NULL,
],
])->toString(),
]);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$team = $this->team;
if (!$team instanceof TeamNode) {
return;
}
$team
->set(TeamNode::SLACK_NOTIFICATION_ENABLED_FIELD, $form_state->getValue('enable_slack_notifications'))
->save();
}
}
......@@ -21,3 +21,15 @@ violinist_teams.billing:
type: entity:node
requirements:
_permission: 'access content'
violinist_teams.team_settings:
path: '/teams/{team}/settings'
defaults:
_title: 'Team Settings'
_form: 'Drupal\violinist_teams\Form\TeamSettingsForm'
options:
parameters:
team:
type: entity:node
requirements:
_custom_access: '\Drupal\violinist_teams\Controller\TeamSettingsController::accessTeamSettings'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment