Skip to content
Snippets Groups Projects
Commit 308cc7ef authored by Yazan Majadba's avatar Yazan Majadba Committed by Qusai Taha
Browse files

Issue #3406765: Add enabling mode

parent 69033763
Branches 1.0.x
1 merge request!1Issue #3406765: Add enabling mode
......@@ -34,13 +34,40 @@ class GleapConfiguration extends ConfigFormBase {
// Default settings.
$config = $this->config('gleap.gleap_configuration');
// Gleap enable.
$form['gleap_enable'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable Gleap'),
'#default_value' => $config->get('gleap_enable'),
'#description' => $this->t('Check this box if you want to enable Gleap. Uncheck to disable Gleap.'),
];
$states = [
'visible' => [':input[name=' . 'gleap_enable' . ']' => ['checked' => TRUE]],
];
// Gleap API key.
$form['gleap_api_key'] = [
'#type' => 'textfield',
'#title' => $this->t('Gleap API key'),
'#default_value' => $config->get('gleap_api_key'),
'#description' => $this->t('Give your gleap API key.'),
'#states' => $states,
'#required' => TRUE,
];
$roles = [];
foreach ($roles = Role::loadMultiple() as $role) {
$roles[$role->id()] = $role->get('label');
}
$form['gleap_roles'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Enable Gleap for roles:'),
'#options' => $roles,
'#default_value' => $config->get('gleap_roles'),
'#required' => TRUE,
'#states' => $states,
];
return parent::buildForm($form, $form_state);
......@@ -52,6 +79,8 @@ class GleapConfiguration extends ConfigFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config('gleap.gleap_configuration');
$config->set('gleap_api_key', $form_state->getValue('gleap_api_key'));
$config->set('gleap_enable', $form_state->getValue('gleap_enable'));
$config->set('gleap_roles', $form_state->getValue('gleap_roles'));
$config->save();
return parent::submitForm($form, $form_state);
}
......
......@@ -13,6 +13,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Access\AccessResult;
/**
* Provides a Gleap block.
......@@ -88,6 +89,10 @@ class GleapBlock extends BlockBase implements ContainerFactoryPluginInterface {
* {@inheritdoc}
*/
public function build(): array {
if (!$this->configFactory->get('gleap_enable')) {
return [];
}
if (empty($this->configFactory->get('gleap_api_key')) && ($this->currentUser->hasPermission('administer gleap'))) {
$url = Url::fromUri('route:gleap_configuration');
$link = new Link($this->t('here'), $url);
......@@ -102,6 +107,18 @@ class GleapBlock extends BlockBase implements ContainerFactoryPluginInterface {
];
}
/**
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
$current_roles = $this->currentUser->getRoles();
if (!empty(array_intersect($current_roles, $this->configFactory->get('gleap_roles')))) {
return AccessResult::allowed();
} else {
return AccessResult::forbidden();
}
}
/**
* {@inheritdoc}
*/
......
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