Skip to content
Snippets Groups Projects
Commit bbedd516 authored by nterbogt's avatar nterbogt Committed by Adam Bramley
Browse files

Issue #3156692 by nterbogt, acbramley, arvind.kinja, larowlan: Provide...

Issue #3156692 by nterbogt, acbramley, arvind.kinja, larowlan: Provide settings form for managing configuration with UI
parent 5cd66fcb
Branches
Tags
1 merge request!18Resolve #3156692 "Provide settings form"
Pipeline #46967 passed with warnings
......@@ -2,6 +2,7 @@ name: Preview Link
type: module
description: Allows anyone to preview unpublished content with a unique link.
core_version_requirement: ^9.4 || ^10
configure: preview_link.settings
php: 8.0
package: Preview
dependencies:
......
preview_link.settings:
route_name: preview_link.settings
parent: system.admin_config_content
title: Preview link settings
description: 'Manage preview link settings.'
administer preview link settings:
title: 'Administer preview link settings'
description: 'Allows the user to administer the default preview link settings'
restrict access: TRUE
generate preview links:
title: 'Generate preview links'
description: 'Allows the user to generate a preview link for a piece of content'
preview_link.settings:
path: '/admin/config/content/preview_link'
defaults:
_form: '\Drupal\preview_link\Form\PreviewLinkSettingsForm'
_title: 'Preview link settings'
_description: 'Manage Preview link settings.'
requirements:
_permission: 'administer preview link settings'
preview_link.session_tokens.remove:
path: '/preview-link/session-tokens/remove'
defaults:
......
<?php
declare(strict_types = 1);
namespace Drupal\preview_link\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\preview_link\PreviewLinkUtility;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Allow settings to be changed from the UI for preview link.
*/
class PreviewLinkSettingsForm extends ConfigFormBase {
/**
* Constructs a PreviewLinkSettingsForm.
*/
public function __construct(ConfigFactoryInterface $configFactory,
protected EntityTypeBundleInfoInterface $bundleInfo,
protected EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct($configFactory);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('entity_type.bundle.info'),
$container->get('entity_type.manager'),
);
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return [
$this->getConfigName(),
];
}
/**
* A method to get the config name.
*
* @return string
* The config name.
*/
private function getConfigName(): string {
return 'preview_link.settings';
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'preview_link_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->config($this->getConfigName());
$form = parent::buildForm($form, $form_state);
$form['display_message'] = [
'#type' => 'select',
'#title' => $this->t('Display message'),
'#description' => $this->t("When to display a message to a user that the preview link was created."),
'#options' => [
'always' => $this->t('Always'),
'subsequent' => $this->t('Subsequent'),
'never' => $this->t('Never'),
],
'#default_value' => $config->get('display_message') ?: 'subsequent',
];
$form['bundles'] = [
'#type' => 'details',
];
$form['bundles']['help'] = [
'#markup' => $this->t('Enable entity type/bundles for use with preview link.'),
];
$selectedOptions = $this->getSelectedEntityTypeOptions();
$form['bundles']['enabled_entity_types'] = [
'#type' => 'tableselect',
'#header' => [
$this->t('Entity type'),
$this->t('Bundle'),
],
'#options' => $this->getEntityTypeOptions(),
'#default_value' => array_fill_keys($selectedOptions, TRUE),
];
// Collapse the details element if anything is enabled.
$form['bundles']['#title'] = $this->t('Enabled types (@count)', [
'@count' => count($selectedOptions),
]);
$form['bundles']['#open'] = count($selectedOptions) === 0;
$form['multiple_entities'] = [
'#type' => 'checkbox',
'#title' => 'Multiple entities',
'#description' => $this->t('Whether preview links can reference multiple entities.'),
'#default_value' => $config->get('multiple_entities'),
];
$form['expiry_seconds'] = [
'#type' => 'number',
'#title' => 'Expiry seconds',
'#description' => $this->t('The number of seconds before a preview link expires.'),
'#default_value' => $config->get('expiry_seconds') ?: 604800,
'#min' => 1,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config($this->getConfigName());
$config->set('display_message', $form_state->getValue('display_message'));
$config->set('multiple_entities', $form_state->getValue('multiple_entities'));
$config->set('expiry_seconds', $form_state->getValue('expiry_seconds'));
$config->clear('enabled_entity_types');
foreach (array_keys(array_filter($form_state->getValue('enabled_entity_types'))) as $enabledBundle) {
if (strpos($enabledBundle, ':') !== FALSE) {
[$entityTypeId, $bundle] = explode(':', $enabledBundle);
$bundles = $config->get('enabled_entity_types.' . $entityTypeId) ?: [];
$bundles[] = $bundle;
$config->set('enabled_entity_types.' . $entityTypeId, $bundles);
}
else {
$entityTypeId = $enabledBundle;
$config->set('enabled_entity_types.' . $entityTypeId, []);
}
}
$config->save();
parent::submitForm($form, $form_state);
}
/**
* The options available for the user to select for bundle types.
*
* @return array
* A 'entity_id:bundle' style array of possible options.
*/
protected function getEntityTypeOptions(): array {
$options = [];
$entityTypes = $this->entityTypeManager->getDefinitions();
$entityTypes = array_filter($entityTypes, [
PreviewLinkUtility::class,
'isEntityTypeSupported',
]);
foreach ($entityTypes as $entityType => $info) {
$options[$entityType] = [
$info ? ['data' => ['#markup' => '<strong>' . $info->getLabel() . '</strong>']] : '',
['data' => ['#markup' => '<em>' . $this->t('If selected and no bundles are selected, all bundles will be enabled.') . '</em>']],
];
foreach ($this->bundleInfo->getBundleInfo($entityType) as $bundle => $bundleInfo) {
if ($entityType === $bundle) {
continue;
}
$options[sprintf('%s:%s', $entityType, $bundle)] = [
$info?->getLabel() ?: '',
$bundleInfo['label'] ?: '',
];
}
}
return $options;
}
/**
* The enabled entities and bundles for preview link to apply to.
*
* @return array
* A 'entity_id:bundle' style array of selected options.
*/
protected function getSelectedEntityTypeOptions(): array {
$config = $this->config($this->getConfigName());
$configured = $config->get('enabled_entity_types') ?: [];
$selected = [];
foreach ($configured as $entityTypeId => $bundles) {
$selected[] = $entityTypeId;
foreach ($bundles as $bundle) {
$selected[] = sprintf('%s:%s', $entityTypeId, $bundle);
}
}
return $selected;
}
}
<?php
declare(strict_types = 1);
namespace Drupal\Tests\preview_link\Functional;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
/**
* @covers \Drupal\preview_link\Form\PreviewLinkSettingsForm
*
* @group preview_link
*/
class PreviewLinkSettingsFormTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'entity_test',
'preview_link',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* The account used for logging into admin and running test.
*/
protected ?AccountInterface $account;
/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
entity_test_create_bundle('bundle_a', 'Test bundle A', 'entity_test_rev');
entity_test_create_bundle('bundle_b', 'Test bundle A', 'entity_test_rev');
$this->account = $this->drupalCreateUser(['administer preview link settings']);
}
/**
* Tests the preview link settings form.
*/
public function testSettingsForm(): void {
$assert_session = $this->assertSession();
$url = Url::fromRoute('preview_link.settings');
$this->drupalGet($url);
$assert_session->statusCodeEquals(403);
$this->drupalLogin($this->account);
$this->drupalGet($url);
$page = $this->getSession()->getPage();
$page->selectFieldOption('multiple_entities', TRUE);
$page->pressButton('Save configuration');
$assert_session = $this->assertSession();
$assert_session->pageTextContains('The configuration options have been saved.');
$assert_session->fieldValueEquals('multiple_entities', 1);
$page->selectFieldOption('enabled_entity_types[entity_test_rev]', TRUE);
$page->pressButton('Save configuration');
$assert_session = $this->assertSession();
$assert_session->pageTextContains('The configuration options have been saved.');
$assert_session->fieldValueEquals('enabled_entity_types[entity_test_rev]', 'entity_test_rev');
$config = $this->config('preview_link.settings')->get('enabled_entity_types');
$this->assertEquals([
'entity_test_rev' => [],
], $config);
$page->selectFieldOption('enabled_entity_types[entity_test_rev:bundle_a]', TRUE);
$page->pressButton('Save configuration');
$assert_session = $this->assertSession();
$assert_session->pageTextContains('The configuration options have been saved.');
$assert_session->fieldValueEquals('enabled_entity_types[entity_test_rev]', 'entity_test_rev');
$assert_session->fieldValueEquals('enabled_entity_types[entity_test_rev:bundle_a]', 'entity_test_rev:bundle_a');
// Need to clear config cache to update it and test again.
$this->container->get('cache.config')->deleteAll();
$this->container->get('config.factory')->reset('preview_link.settings');
$config = $this->config('preview_link.settings')->get('enabled_entity_types');
$this->assertEquals([
'entity_test_rev' => [
'bundle_a',
],
], $config);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment