Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ddfher 145 dev setup adjustments #1937

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ x-environment:
LAGOON_ENVIRONMENT: 'local'
LAGOON_ENVIRONMENT_TYPE: ${LAGOON_ENVIRONMENT_TYPE:-local}
WEBROOT: web
GRAPHQL_USER_NAME: graphql_consumer
GRAPHQL_USER_PASSWORD: test
BNF_SERVER_BASE_ENDPOINT: "https://dpl-cms.local"
# Uncomment if you like to have the system behave like in production
#LAGOON_ENVIRONMENT_TYPE: production

Expand Down
1 change: 1 addition & 0 deletions web/modules/custom/bnf/bnf.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
autowire: true
arguments:
$logger: '@logger.channel.bnf'
Drupal\bnf\Services\BnfImporter: '@bnf.importer'

logger.channel.bnf:
parent: logger.channel_base
Expand Down
7 changes: 7 additions & 0 deletions web/modules/custom/bnf/bnf_client/bnf_client.links.menu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ bnf_client.import_content:
parent: system.admin_content
route_name: bnf_client.import_form
weight: 10

bnf_client.login:
title: 'Login to BNF'
parent: system.admin_content
route_name: bnf_client.server_redirect
weight: 11

bnf_client.settings_form:
title: 'BNF configuration'
route_name: bnf_client.settings
description: 'Configure BNF client.'
parent: system.admin_config_services
10 changes: 10 additions & 0 deletions web/modules/custom/bnf/bnf_client/bnf_client.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ bnf_client.server_redirect:
_title: 'Login to BNF'
requirements:
_permission: 'bnf client import nodes'

bnf_client.settings:
path: '/admin/config/services/bnf'
defaults:
_form: '\Drupal\bnf_client\Form\SettingsForm'
_title: 'BNF configuration'
requirements:
_permission: 'administer site configuration'
options:
_admin_route: TRUE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
base_url: 'http://bibliotekernesnationaleformidling.dk/'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Schema for the configuration files of the BNF Client module.

bnf_client.settings:
type: config_object
label: 'BNF client configuration'
mapping:
base_url:
type: uri
label: 'BNF server URL'
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Drupal\bnf_client\Controller;

use Drupal\bnf_client\Form\SettingsForm;
use Drupal\Core\Controller\ControllerBase;

use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -17,8 +17,8 @@ class BnfRedirecter extends ControllerBase {
* Logging in the editor on BNF, allowing them to browse available content.
*/
public function login(Request $request): TrustedRedirectResponse {
$bnfServer = (string) getenv('BNF_SERVER_BASE_ENDPOINT');
$loginUrl = "$bnfServer/bnf/login";
$bnfServer = $this->config(SettingsForm::CONFIG_NAME)->get('base_url');
$loginUrl = "{$bnfServer}bnf/login";

$url = Url::fromUri($loginUrl, [
'query' => [
Expand Down
28 changes: 13 additions & 15 deletions web/modules/custom/bnf/bnf_client/src/Form/BnfImportConfirmForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\bnf\Exception\AlreadyExistsException;
use Drupal\bnf\Services\BnfImporter;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormInterface;
Expand All @@ -12,15 +13,20 @@
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

/**
* Displaying an import preview, and allowing editor to import.
*/
class BnfImportConfirmForm implements FormInterface, ContainerInjectionInterface {
use StringTranslationTrait;

use AutowireTrait;
use StringTranslationTrait;

/**
* The BNF site base URL.
*/
protected string $baseUrl;

/**
* {@inheritDoc}
Expand All @@ -29,19 +35,11 @@ public function __construct(
protected RouteMatchInterface $routeMatch,
protected MessengerInterface $messenger,
protected BnfImporter $bnfImporter,
#[Autowire(service: 'logger.channel.bnf')]
protected LoggerInterface $logger,
) {}

/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container): static {
return new static(
$container->get('current_route_match'),
$container->get('messenger'),
$container->get('bnf.importer'),
$container->get('logger.channel.bnf'),
);
ConfigFactoryInterface $configFactory,
) {
$this->baseUrl = $configFactory->get(SettingsForm::CONFIG_NAME)->get('base_url');
}

/**
Expand All @@ -58,7 +56,7 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
$form['#title'] = $this->t('Confirm import of BNF content', [], ['context' => 'BNF']);

$uuid = $this->routeMatch->getParameter('uuid');
$bnfServer = (string) getenv('BNF_SERVER_BASE_ENDPOINT') . '/graphql';
$bnfServer = $this->baseUrl . 'graphql';

$form_state->set('uuid', $uuid);
$form_state->set('bnfServer', $bnfServer);
Expand Down
90 changes: 90 additions & 0 deletions web/modules/custom/bnf/bnf_client/src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Drupal\bnf_client\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use function Safe\preg_match;

/**
* BNF client configuration form.
*/
class SettingsForm extends ConfigFormBase {

const CONFIG_NAME = 'bnf_client.settings';

/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return [
self::CONFIG_NAME,
];
}

/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'bnf_client_settings';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {

$form['bnf_client'] = [
'#type' => 'fieldset',
'#title' => $this->t('BNF configuration'),
'#tree' => FALSE,
];

$form['bnf_client']['base_url'] = [
'#type' => 'textfield',
'#size' => 100,
'#title' => $this->t('BNF server URL'),
'#description' => $this->t('For example <em>https://bibliotekernesnationaleformidling.dk/</em>.'),
'#default_value' => $this->config(self::CONFIG_NAME)->get('base_url'),
'#required' => TRUE,
];

return parent::buildForm($form, $form_state);
}

/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
$url = $form_state->getValue('base_url');
$element = $form['bnf_client']['base_url'];

// This doesn't support IDN names like `jøsses.dk`, but we'll live with that
// for the moment being.
if (!filter_var($url, FILTER_VALIDATE_URL)) {
$form_state->setError($element, $this->t('Please enter a valid URL.'));
}

if (!preg_match('/^https:/', $url)) {
$form_state->setError($element, $this->t('Only HTTPS is supported.'));
}

if (mb_substr($url[-1], -1) !== '/') {
$form_state->setError($element, $this->t('URL must end with a /.'));
}
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
parent::submitForm($form, $form_state);

$this->config(self::CONFIG_NAME)
->set('base_url', $form_state->getValue('base_url'))
->save();
}

}
16 changes: 13 additions & 3 deletions web/modules/custom/bnf/bnf_client/src/Services/BnfExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Drupal\bnf\BnfStateEnum;
use Drupal\bnf\Exception\AlreadyExistsException;
use Drupal\bnf_client\Form\SettingsForm;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\node\NodeInterface;
Expand All @@ -20,6 +22,11 @@
*/
class BnfExporter {

/**
* The BNF site base URL.
*/
protected string $baseUrl;

/**
* Constructor.
*/
Expand All @@ -28,7 +35,10 @@ public function __construct(
protected UrlGeneratorInterface $urlGenerator,
protected TranslationInterface $translation,
protected LoggerInterface $logger,
) {}
ConfigFactoryInterface $configFactory,
) {
$this->baseUrl = $configFactory->get(SettingsForm::CONFIG_NAME)->get('base_url');
}

/**
* Requesting BNF server to import the supplied node.
Expand All @@ -55,7 +65,7 @@ public function exportNode(NodeInterface $node): void {
GRAPHQL;

try {
$bnfServer = (string) getenv('BNF_SERVER_BASE_ENDPOINT') . '/graphql';
$bnfServer = $this->baseUrl . 'graphql';

if (!filter_var($bnfServer, FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException('The provided BNF server URL is not valid.');
Expand All @@ -72,7 +82,7 @@ public function exportNode(NodeInterface $node): void {
'headers' => [
'Content-Type' => 'application/json',
],
'auth' => [getenv('GRAPHQL_USER_NAME'), getenv('GRAPHQL_USER_PASSWORD')],
'auth' => ['bnf_graphql', getenv('BNF_GRAPHQL_CONSUMER_USER_PASSWORD')],
'json' => [
'query' => $mutation,
],
Expand Down
2 changes: 1 addition & 1 deletion web/modules/custom/bnf/src/Services/BnfImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function loadNodeData(string $uuid, string $endpointUrl, string $nodeType
'headers' => [
'Content-Type' => 'application/json',
],
'auth' => [getenv('GRAPHQL_USER_NAME'), getenv('GRAPHQL_USER_PASSWORD')],
'auth' => ['bnf_graphql', getenv('BNF_GRAPHQL_CONSUMER_USER_PASSWORD')],
'json' => [
'query' => $query,
],
Expand Down
Loading