Skip to content

Commit

Permalink
Merge pull request #392 from danskernesdigitalebibliotek/react-app-co…
Browse files Browse the repository at this point in the history
…nfig-handling

Improve React app config handling
  • Loading branch information
kasperg authored Oct 4, 2023
2 parents 8d923bd + 487d821 commit c4e2b4a
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 19 deletions.
13 changes: 13 additions & 0 deletions .phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,18 @@
https://www.drupal.org/project/coder/issues/3253472#comment-14508517
-->
<exclude name="Drupal.Commenting.VariableComment.IncorrectVarType"/>
<exclude name="Drupal.Commenting.VariableComment.IncorrectVarType"/>
<!--
Disable deprecation related sniffs that are seem to Drupal Core development.
-->
<!--
Do not require specifying when deprecated function will be removed.
We do not enforce this whin the project.
-->
<exclude name="Drupal.Commenting.Deprecated.IncorrectTextLayout"/>
<!--
Do not require a @see tag with link to Drupal.org for deprecation.
-->
<exclude name="Drupal.Commenting.Deprecated.DeprecatedMissingSeeTag"/>
</rule>
</ruleset>
7 changes: 7 additions & 0 deletions web/modules/custom/dpl_dashboard/src/DplDashboardSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function getConfigKey(): string {
return 'dpl_dashboard.settings';
}

/**
* {@inheritdoc}
*/
public function getConfig(): array {
return $this->legacyConfig();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function getConfigKey(): string {
return 'dpl_favorites_list.settings';
}

/**
* {@inheritdoc}
*/
public function getConfig(): array {
return $this->legacyConfig();
}

}
7 changes: 7 additions & 0 deletions web/modules/custom/dpl_fbs/src/DplFbsSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function getConfigKey(): string {
return 'dpl_fbs.settings';
}

/**
* {@inheritdoc}
*/
public function getConfig(): array {
return $this->legacyConfig();
}

}
7 changes: 7 additions & 0 deletions web/modules/custom/dpl_fees/src/DplFeesSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function getConfigKey(): string {
return 'dpl_fees.settings';
}

/**
* {@inheritdoc}
*/
public function getConfig(): array {
return $this->legacyConfig();
}

}
38 changes: 38 additions & 0 deletions web/modules/custom/dpl_instant_loan/src/DplInstantLoanSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,44 @@
*/
class DplInstantLoanSettings extends DplReactConfigBase {

/**
* {@inheritdoc}
*/
public function getConfig(): array {
// Class to match configuration type
// https://github.com/reload/dpl-react/blob/66d01476f9d272b7fea4e83f575550cce4b93bbd/src/core/utils/types/instant-loan.ts
$reactConfig = new class {

/**
* @param bool $enabled
* Whether instant loan functionality is enabled or not.
* @param string|null $threshold
* The number of items that must be available before instant loans are
* shown.
* @param string[]|null $matchStrings
* Strings to match against material group texts to identify items
* available for instant loan.
*/
public function __construct(
public bool $enabled = FALSE,
public ?string $threshold = NULL,
public ?array $matchStrings = NULL
) {}

};

$drupalConfig = $this->loadConfig();
$reactConfig = new $reactConfig();

$reactConfig->enabled = (bool) $drupalConfig->get('enabled');
if ($reactConfig->enabled) {
$reactConfig->threshold = (string) $drupalConfig->get('threshold');
$reactConfig->matchStrings = (array) $drupalConfig->get('match_strings');
}

return (array) $reactConfig;
}

/**
* Gets the configuration key for the instant loan settings.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\dpl_react\DplReactConfigInterface;
use function Safe\preg_split;

/**
* Instant Loan settings form.
Expand Down Expand Up @@ -90,7 +91,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
// Set the number of visible rows for the textarea.
'#rows' => 5,
'#description' => $this->t('Text used to identify materials which are available for instant loans.<br/> You can write multiple strings - each on a spearate line.<br/> To find a match one of the strings must be present in the material group of such materials.', [], ['context' => 'dpl_instant_loan']),
'#default_value' => implode("\n", $config->get('match_strings')),
'#default_value' => implode("\n", $config->get('match_strings') ?? []),
'#states' => $config_field_states,
];

Expand All @@ -115,7 +116,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->config($this->configService->getConfigKey())
->set('enabled', $form_state->getValue('enabled'))
->set('match_strings', explode("\n", $form_state->getValue('match_strings')) ?? [])
->set('match_strings', preg_split("/\s*[\r\n]+\s*/", $form_state->getValue('match_strings')) ?? [])
->set('threshold', $form_state->getValue('threshold'))
->save();

Expand Down
5 changes: 2 additions & 3 deletions web/modules/custom/dpl_library_agency/src/FbsApiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ public function __construct(
* Assemble the API configuration.
*/
protected function getConfiguration(): Configuration {
$config = $this->fbsSettings->getConfig();
$host = $config['baseUrl'];
$config = $this->fbsSettings->loadConfig();
$configuration = (new Configuration())
->setHost($host);
->setHost($config->get('base_url'));

$token = $this->tokenHandler->getToken();
if ($token) {
Expand Down
7 changes: 7 additions & 0 deletions web/modules/custom/dpl_loans/src/DplLoansSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function getConfigKey(): string {
return 'dpl_loan_list.settings';
}

/**
* {@inheritdoc}
*/
public function getConfig(): array {
return $this->legacyConfig();
}

}
7 changes: 7 additions & 0 deletions web/modules/custom/dpl_patron_menu/src/DplMenuSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function getConfigKey(): string {
return 'dpl_patron_menu.settings';
}

/**
* {@inheritdoc}
*/
public function getConfig(): array {
return $this->legacyConfig();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function getConfigKey(): string {
return 'dpl_patron_page.settings';
}

/**
* {@inheritdoc}
*/
public function getConfig(): array {
return $this->legacyConfig();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function getConfigKey(): string {
return 'dpl_patron_reg.settings';
}

/**
* {@inheritdoc}
*/
public function getConfig(): array {
return $this->legacyConfig();
}

}
7 changes: 7 additions & 0 deletions web/modules/custom/dpl_publizon/src/DplPublizonSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function getConfigKey(): string {
return 'dpl_publizon.settings';
}

/**
* {@inheritdoc}
*/
public function getConfig(): array {
return $this->legacyConfig();
}

}
15 changes: 14 additions & 1 deletion web/modules/custom/dpl_react/src/DplReactConfigBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,20 @@ public function getCacheMaxAge() : int {
* @return mixed[]
* The configuration.
*/
public function getConfig() : array {
abstract public function getConfig(): array;

/**
* Return the Drupal configuration as an array with camelCase keys.
*
* Do not use this function for new settings. They should implement
* getConfig() instead!
*
* @return mixed[]
* The formatted configuration.
*
* @deprecated
*/
protected function legacyConfig(): array {
if (!$config = $this->loadConfig()->get()) {
return [];
}
Expand Down
14 changes: 11 additions & 3 deletions web/modules/custom/dpl_react/src/DplReactConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@
use Drupal\Core\Config\Config;

/**
* Class that handles React App Config.
* Interface for instances of Drupal configuration used by React applications.
*/
interface DplReactConfigInterface {

/**
* Get formatted configuration.
* Get formatted configuration for a React application.
*
* This configuration is expected to be converted into a format that can be
* used by a React application. Normally this means that the configuration
* is converted into a JSON string.
*
* Typical activities that are performed during this conversion are:
* - Ensuring types are correct
* - Conversion of names from snake_case to camelCase
*
* @return mixed[]
* The configuration.
*/
public function getConfig(): array;

/**
* Load raw configuration.
* Load the raw Drupal configuration object.
*
* @return \Drupal\Core\Config\Config
* The configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,4 @@ public static function getBlockedSettings(): array {
return $blockedData;
}

/**
* Get the instant loan configuration.
*
* @return mixed[]
* The instant loan configuration.
*/
public static function getInstantLoanConfig(): array {
return \Drupal::configFactory()->get('dpl_instant_loan.settings')->get() ?? [];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function getConfigKey(): string {
return 'dpl_recommender.settings';
}

/**
* {@inheritdoc}
*/
public function getConfig(): array {
return $this->legacyConfig();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function getConfigKey(): string {
return 'dpl_reservation_list.settings';
}

/**
* {@inheritdoc}
*/
public function getConfig(): array {
return $this->legacyConfig();
}

}

0 comments on commit c4e2b4a

Please sign in to comment.