diff --git a/app/Drupal/laszlo/src/Hook/Theme/PreprocessLaszloPageHeader.php b/app/Drupal/laszlo/src/Hook/Theme/PreprocessLaszloPageHeader.php index 80dbeba2..eb2df2c5 100644 --- a/app/Drupal/laszlo/src/Hook/Theme/PreprocessLaszloPageHeader.php +++ b/app/Drupal/laszlo/src/Hook/Theme/PreprocessLaszloPageHeader.php @@ -34,7 +34,7 @@ public static function create(ContainerInterface $container): self { private function prepareBrandingVariables(array &$variables): void { $cache = CacheableMetadata::createFromRenderArray($variables); - $site_settings = $this->configFactory->getEditable('system.site'); + $site_settings = $this->configFactory->get('system.site'); $cache->addCacheableDependency($site_settings); $logo_path = $this->themeManager->getActiveTheme()->getPath() . '/logo.svg'; diff --git a/app/Drupal/niklan/niklan.routing.yml b/app/Drupal/niklan/niklan.routing.yml index ba722c39..5fa279f0 100644 --- a/app/Drupal/niklan/niklan.routing.yml +++ b/app/Drupal/niklan/niklan.routing.yml @@ -9,8 +9,8 @@ niklan.about: niklan.about.settings: path: '/admin/niklan/about' defaults: - _title_callback: '\Drupal\niklan\Form\AboutSettingsForm::title' - _form: '\Drupal\niklan\Form\AboutSettingsForm' + _title: 'About settings' + _form: '\Drupal\niklan\Form\Settings\AboutSettingsForm' requirements: _permission: 'administer site configuration' @@ -26,7 +26,7 @@ niklan.support.settings: path: '/admin/niklan/support' defaults: _title: 'Support settings' - _form: '\Drupal\niklan\Form\SupportSettingsForm' + _form: '\Drupal\niklan\Form\Settings\SupportSettingsForm' requirements: _permission: 'administer site configuration' @@ -34,7 +34,7 @@ niklan.contact.settings: path: '/admin/niklan/contact' defaults: _title: 'Contact settings' - _form: '\Drupal\niklan\Form\ContactSettingsForm' + _form: '\Drupal\niklan\Form\Settings\ContactSettingsForm' requirements: _permission: 'administer site configuration' @@ -42,7 +42,7 @@ niklan.services.settings: path: '/admin/niklan/services' defaults: _title: 'Services settings' - _form: '\Drupal\niklan\Form\ServicesSettingsForm' + _form: '\Drupal\niklan\Form\Settings\ServicesSettingsForm' requirements: _permission: 'administer site configuration' diff --git a/app/Drupal/niklan/niklan.services.yml b/app/Drupal/niklan/niklan.services.yml index 5b5ce659..1ebf9e15 100644 --- a/app/Drupal/niklan/niklan.services.yml +++ b/app/Drupal/niklan/niklan.services.yml @@ -14,10 +14,10 @@ services: Drupal\niklan\Utility\DatabaseTagUsageStatistics: {} Drupal\niklan\Contract\Utility\TagUsageStatistics: '@Drupal\niklan\Utility\DatabaseTagUsageStatistics' - Drupal\niklan\Factory\KeyValue\ServiceContainerLanguageAwareFactory: {} - keyvalue.language_aware: '@Drupal\niklan\Factory\KeyValue\ServiceContainerLanguageAwareFactory' - Drupal\niklan\Factory\KeyValue\DatabaseLanguageAwareFactory: {} - keyvalue.language_aware.database: '@Drupal\niklan\Factory\KeyValue\DatabaseLanguageAwareFactory' + Drupal\niklan\Repository\KeyValue\ServiceContainerLanguageAwareFactory: {} + keyvalue.language_aware: '@Drupal\niklan\Repository\KeyValue\ServiceContainerLanguageAwareFactory' + Drupal\niklan\Repository\KeyValue\DatabaseLanguageAwareFactory: {} + keyvalue.language_aware.database: '@Drupal\niklan\Repository\KeyValue\DatabaseLanguageAwareFactory' Drupal\niklan\EventSubscriber\LanguageAwareSettingsRoutes: tags: diff --git a/app/Drupal/niklan/src/Contract/Factory/KeyValue/LanguageAwareFactory.php b/app/Drupal/niklan/src/Contract/Repository/KeyValue/LanguageAwareFactory.php similarity index 84% rename from app/Drupal/niklan/src/Contract/Factory/KeyValue/LanguageAwareFactory.php rename to app/Drupal/niklan/src/Contract/Repository/KeyValue/LanguageAwareFactory.php index 5331e112..7dae30ba 100644 --- a/app/Drupal/niklan/src/Contract/Factory/KeyValue/LanguageAwareFactory.php +++ b/app/Drupal/niklan/src/Contract/Repository/KeyValue/LanguageAwareFactory.php @@ -2,10 +2,9 @@ declare(strict_types=1); -namespace Drupal\niklan\Contract\Factory\KeyValue; +namespace Drupal\niklan\Contract\Repository\KeyValue; use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; -use Drupal\niklan\Contract\Repository\KeyValue\LanguageAwareStore; /** * Defines an interface for language-aware key/value stores. diff --git a/app/Drupal/niklan/src/Controller/BlogController.php b/app/Drupal/niklan/src/Controller/BlogController.php index 872688b4..eaafb455 100644 --- a/app/Drupal/niklan/src/Controller/BlogController.php +++ b/app/Drupal/niklan/src/Controller/BlogController.php @@ -6,6 +6,7 @@ use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Render\RendererInterface; use Drupal\node\NodeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -17,6 +18,7 @@ final class BlogController implements ContainerInjectionInterface { public function __construct( protected EntityTypeManagerInterface $entityTypeManager, protected RendererInterface $renderer, + protected LanguageManagerInterface $languageManager, ) {} #[\Override] @@ -24,6 +26,7 @@ public static function create(ContainerInterface $container): self { return new self( $container->get('entity_type.manager'), $container->get('renderer'), + $container->get(LanguageManagerInterface::class), ); } @@ -99,6 +102,7 @@ protected function getEntityIds(): array { ->accessCheck(FALSE) ->condition('type', 'blog_entry') ->condition('status', NodeInterface::PUBLISHED) + ->condition('langcode', $this->languageManager->getCurrentLanguage()->getId(), '=') ->sort('created', 'DESC'); if ($this->limit) { diff --git a/app/Drupal/niklan/src/Controller/ContactController.php b/app/Drupal/niklan/src/Controller/ContactController.php index 9631902b..31970902 100644 --- a/app/Drupal/niklan/src/Controller/ContactController.php +++ b/app/Drupal/niklan/src/Controller/ContactController.php @@ -6,8 +6,7 @@ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; -use Drupal\niklan\Repository\AboutSettings; -use Drupal\niklan\Repository\ContactSettings; +use Drupal\niklan\Repository\KeyValue\ContactSettings; use Symfony\Component\DependencyInjection\ContainerInterface; final readonly class ContactController implements ContainerInjectionInterface { @@ -29,7 +28,7 @@ public function __invoke(): array { '#description' => [ '#type' => 'processed_text', '#text' => $this->settings->getDescription(), - '#format' => AboutSettings::TEXT_FORMAT, + '#format' => $this->settings::TEXT_FORMAT, ], '#email' => $this->settings->getEmail(), '#telegram' => $this->settings->getTelegram(), diff --git a/app/Drupal/niklan/src/Controller/ServicesController.php b/app/Drupal/niklan/src/Controller/ServicesController.php index 2ab2c7ea..17980e8a 100644 --- a/app/Drupal/niklan/src/Controller/ServicesController.php +++ b/app/Drupal/niklan/src/Controller/ServicesController.php @@ -6,8 +6,7 @@ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; -use Drupal\niklan\Repository\AboutSettings; -use Drupal\niklan\Repository\ServicesSettings; +use Drupal\niklan\Repository\KeyValue\ServicesSettings; use Symfony\Component\DependencyInjection\ContainerInterface; final readonly class ServicesController implements ContainerInjectionInterface { @@ -29,7 +28,7 @@ public function __invoke(): array { '#description' => [ '#type' => 'processed_text', '#text' => $this->settings->getDescription(), - '#format' => AboutSettings::TEXT_FORMAT, + '#format' => $this->settings::TEXT_FORMAT, ], '#hourly_rate' => $this->settings->getHourlyRate(), ]; diff --git a/app/Drupal/niklan/src/Controller/SupportController.php b/app/Drupal/niklan/src/Controller/SupportController.php index a4ab6fa8..7b36c5bb 100644 --- a/app/Drupal/niklan/src/Controller/SupportController.php +++ b/app/Drupal/niklan/src/Controller/SupportController.php @@ -6,7 +6,7 @@ use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; -use Drupal\niklan\Repository\SupportSettings; +use Drupal\niklan\Repository\KeyValue\SupportSettings; use Symfony\Component\DependencyInjection\ContainerInterface; final readonly class SupportController implements ContainerInjectionInterface { @@ -28,7 +28,7 @@ public function __invoke(): array { '#description' => [ '#type' => 'processed_text', '#text' => $this->settings->getDescription(), - '#format' => SupportSettings::TEXT_FORMAT, + '#format' => $this->settings::TEXT_FORMAT, ], '#donate_url' => $this->settings->getDonateUrl(), ]; diff --git a/app/Drupal/niklan/src/EventSubscriber/LanguageAwareSettingsRoutes.php b/app/Drupal/niklan/src/EventSubscriber/LanguageAwareSettingsRoutes.php index 6b835011..70628e97 100644 --- a/app/Drupal/niklan/src/EventSubscriber/LanguageAwareSettingsRoutes.php +++ b/app/Drupal/niklan/src/EventSubscriber/LanguageAwareSettingsRoutes.php @@ -18,6 +18,9 @@ public const array ROUTES_TO_ENHANCE = [ 'niklan.about.settings', + 'niklan.contact.settings', + 'niklan.services.settings', + 'niklan.support.settings', ]; public function __construct( diff --git a/app/Drupal/niklan/src/Form/ContactSettingsForm.php b/app/Drupal/niklan/src/Form/ContactSettingsForm.php deleted file mode 100644 index b138a61a..00000000 --- a/app/Drupal/niklan/src/Form/ContactSettingsForm.php +++ /dev/null @@ -1,101 +0,0 @@ -get(ContactSettings::class), - $container->get(MessengerInterface::class), - $container->get(CacheTagsInvalidatorInterface::class), - ); - } - - #[\Override] - public function getFormId(): string { - return 'niklan_contact_settings_form'; - } - - #[\Override] - public function buildForm(array $form, FormStateInterface $form_state): array { - $form['#tree'] = TRUE; - - $form['email'] = [ - '#type' => 'email', - '#title' => new TranslatableMarkup('Email'), - '#default_value' => $this->settings->getEmail(), - '#required' => TRUE, - ]; - - $form['telegram'] = [ - '#type' => 'url', - '#title' => new TranslatableMarkup('Telegram'), - '#default_value' => $this->settings->getTelegram(), - '#required' => TRUE, - ]; - - $form['description'] = [ - '#type' => 'text_format', - '#title' => new TranslatableMarkup('Description'), - '#description' => new TranslatableMarkup('The description of the about page.'), - '#default_value' => $this->settings->getDescription(), - '#allowed_formats' => [ContactSettings::TEXT_FORMAT], - '#rows' => 3, - '#required' => TRUE, - ]; - - $form['actions']['#type'] = 'actions'; - $form['actions']['save'] = [ - '#type' => 'submit', - '#value' => new TranslatableMarkup('Save'), - '#button_type' => 'primary', - ]; - - return $form; - } - - #[\Override] - public function validateForm(array &$form, FormStateInterface $form_state): void { - // Not needed. - } - - #[\Override] - public function submitForm(array &$form, FormStateInterface $form_state): void { - $this - ->settings - ->setEmail($form_state->getValue('email')) - ->setTelegram($form_state->getValue('telegram')) - ->setDescription($form_state->getValue(['description', 'value'])); - - $this - ->messenger - ->addStatus(new TranslatableMarkup('Settings successfully saved.')); - - $this - ->cacheTagsInvalidator - ->invalidateTags($this->settings->getCacheTags()); - } - -} diff --git a/app/Drupal/niklan/src/Form/ServicesSettingsForm.php b/app/Drupal/niklan/src/Form/ServicesSettingsForm.php deleted file mode 100644 index 324aea63..00000000 --- a/app/Drupal/niklan/src/Form/ServicesSettingsForm.php +++ /dev/null @@ -1,94 +0,0 @@ -get(ServicesSettings::class), - $container->get(MessengerInterface::class), - $container->get(CacheTagsInvalidatorInterface::class), - ); - } - - #[\Override] - public function getFormId(): string { - return 'niklan_services_settings'; - } - - #[\Override] - public function buildForm(array $form, FormStateInterface $form_state): array { - $form['#tree'] = TRUE; - - $form['description'] = [ - '#type' => 'text_format', - '#title' => new TranslatableMarkup('Body'), - '#description' => new TranslatableMarkup('The description of service page.'), - '#default_value' => $this->settings->getDescription(), - '#allowed_formats' => [SupportSettings::TEXT_FORMAT], - '#rows' => 3, - '#required' => TRUE, - ]; - - $form['hourly_rate'] = [ - '#type' => 'textfield', - '#title' => new TranslatableMarkup('Hourly rate'), - '#default_value' => $this->settings->getHourlyRate(), - '#required' => TRUE, - ]; - - $form['actions']['#type'] = 'actions'; - $form['actions']['save'] = [ - '#type' => 'submit', - '#value' => new TranslatableMarkup('Save'), - '#button_type' => 'primary', - ]; - - return $form; - } - - #[\Override] - public function submitForm(array &$form, FormStateInterface $form_state): void { - $this - ->settings - ->setDescription($form_state->getValue(['description', 'value'])) - ->setHourlyRate($form_state->getValue(['hourly_rate'])); - - $this - ->messenger - ->addStatus(new TranslatableMarkup('Settings successfully saved.')); - - $this - ->cacheTagsInvalidator - ->invalidateTags($this->settings->getCacheTags()); - } - - #[\Override] - public function validateForm(array &$form, FormStateInterface $form_state): void { - // Not needed. - } - -} diff --git a/app/Drupal/niklan/src/Form/AboutSettingsForm.php b/app/Drupal/niklan/src/Form/Settings/AboutSettingsForm.php similarity index 64% rename from app/Drupal/niklan/src/Form/AboutSettingsForm.php rename to app/Drupal/niklan/src/Form/Settings/AboutSettingsForm.php index d0f5d357..dc57a86c 100644 --- a/app/Drupal/niklan/src/Form/AboutSettingsForm.php +++ b/app/Drupal/niklan/src/Form/Settings/AboutSettingsForm.php @@ -2,49 +2,32 @@ declare(strict_types=1); -namespace Drupal\niklan\Form; +namespace Drupal\niklan\Form\Settings; -use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Messenger\MessengerInterface; -use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\niklan\Repository\KeyValue\AboutSettings; use Drupal\niklan\Repository\KeyValue\LanguageAwareSettingsStore; -use Symfony\Component\DependencyInjection\ContainerInterface; final class AboutSettingsForm extends SettingsForm { - public function __construct( - protected AboutSettings $settings, - protected MessengerInterface $messenger, - protected CacheTagsInvalidatorInterface $cacheTagsInvalidator, - protected RouteMatchInterface $routeMatch, - ) {} - - #[\Override] - public static function create(ContainerInterface $container): self { - return new self( - $container->get(AboutSettings::class), - $container->get(MessengerInterface::class), - $container->get(CacheTagsInvalidatorInterface::class), - $container->get(RouteMatchInterface::class), - ); - } - #[\Override] public function getFormId(): string { return 'niklan_about_settings'; } #[\Override] - public function doBuildForm(array &$form, FormStateInterface $form_state): void { + public function buildForm(array $form, FormStateInterface $form_state): array { + $form = parent::buildForm($form, $form_state); + $this->buildPhotoSettings($form, $form_state); $this->buildContentSettings($form, $form_state); + + return $form; } #[\Override] - public function doSubmitForm(array &$form, FormStateInterface $form_state): void { + public function submitForm(array &$form, FormStateInterface $form_state): void { $this ->getSettings() ->setPhotoMediaId($form_state->getValue('media_id')) @@ -52,6 +35,8 @@ public function doSubmitForm(array &$form, FormStateInterface $form_state): void ->setSubtitle($form_state->getValue(['subtitle', 'value'])) ->setSummary($form_state->getValue(['summary', 'value'])) ->setDescription($form_state->getValue(['description', 'value'])); + + parent::submitForm($form, $form_state); } public function buildContentSettings(array &$form, FormStateInterface $form_state): void { @@ -74,7 +59,7 @@ public function buildContentSettings(array &$form, FormStateInterface $form_stat '#title' => new TranslatableMarkup('Subtitle'), '#description' => new TranslatableMarkup('The subtitle of the about page.'), '#default_value' => $this->getSettings()->getSubtitle(), - '#allowed_formats' => [AboutSettings::TEXT_FORMAT], + '#allowed_formats' => [$this->getSettings()::TEXT_FORMAT], '#required' => TRUE, ]; @@ -83,7 +68,7 @@ public function buildContentSettings(array &$form, FormStateInterface $form_stat '#title' => new TranslatableMarkup('Summary'), '#description' => new TranslatableMarkup('The summary of the about page.'), '#default_value' => $this->getSettings()->getSummary(), - '#allowed_formats' => [AboutSettings::TEXT_FORMAT], + '#allowed_formats' => [$this->getSettings()::TEXT_FORMAT], '#rows' => 3, '#required' => TRUE, ]; @@ -93,31 +78,26 @@ public function buildContentSettings(array &$form, FormStateInterface $form_stat '#title' => new TranslatableMarkup('Description'), '#description' => new TranslatableMarkup('The description of the about page.'), '#default_value' => $this->getSettings()->getDescription(), - '#allowed_formats' => [AboutSettings::TEXT_FORMAT], + '#allowed_formats' => [$this->getSettings()::TEXT_FORMAT], '#rows' => 3, '#required' => TRUE, ]; } - public static function title(): TranslatableMarkup { - return new TranslatableMarkup('About settings'); - } - - protected function getMessenger(): MessengerInterface { - return $this->messenger; - } - - protected function getCacheTagsInvalidator(): CacheTagsInvalidatorInterface { - return $this->cacheTagsInvalidator; - } + #[\Override] + protected function getSettings(): AboutSettings { + $settings = parent::getSettings(); + \assert($settings instanceof AboutSettings); - protected function getSettings(): LanguageAwareSettingsStore { - return $this->settings; + return $settings; } #[\Override] - protected function getRouteMatch(): RouteMatchInterface { - return $this->routeMatch; + protected function loadSettings(): LanguageAwareSettingsStore { + $settings = $this->getContainer()->get(AboutSettings::class); + \assert($settings instanceof AboutSettings); + + return $settings; } private function buildPhotoSettings(array &$form, FormStateInterface $form_state): void { diff --git a/app/Drupal/niklan/src/Form/Settings/ContactSettingsForm.php b/app/Drupal/niklan/src/Form/Settings/ContactSettingsForm.php new file mode 100644 index 00000000..0ebba2db --- /dev/null +++ b/app/Drupal/niklan/src/Form/Settings/ContactSettingsForm.php @@ -0,0 +1,76 @@ + 'email', + '#title' => new TranslatableMarkup('Email'), + '#default_value' => $this->getSettings()->getEmail(), + '#required' => TRUE, + ]; + + $form['telegram'] = [ + '#type' => 'url', + '#title' => new TranslatableMarkup('Telegram'), + '#default_value' => $this->getSettings()->getTelegram(), + '#required' => TRUE, + ]; + + $form['description'] = [ + '#type' => 'text_format', + '#title' => new TranslatableMarkup('Description'), + '#description' => new TranslatableMarkup('The description of the about page.'), + '#default_value' => $this->getSettings()->getDescription(), + '#allowed_formats' => [$this->getSettings()::TEXT_FORMAT], + '#rows' => 3, + '#required' => TRUE, + ]; + + $form['actions']['#type'] = 'actions'; + $form['actions']['save'] = [ + '#type' => 'submit', + '#value' => new TranslatableMarkup('Save'), + '#button_type' => 'primary', + ]; + + return $form; + } + + #[\Override] + public function submitForm(array &$form, FormStateInterface $form_state): void { + $this + ->getSettings() + ->setEmail($form_state->getValue('email')) + ->setTelegram($form_state->getValue('telegram')) + ->setDescription($form_state->getValue(['description', 'value'])); + + parent::submitForm($form, $form_state); + } + + #[\Override] + protected function loadSettings(): LanguageAwareSettingsStore { + $settings = $this->getContainer()->get(ContactSettings::class); + \assert($settings instanceof ContactSettings); + + return $settings; + } + +} diff --git a/app/Drupal/niklan/src/Form/Settings/ServicesSettingsForm.php b/app/Drupal/niklan/src/Form/Settings/ServicesSettingsForm.php new file mode 100644 index 00000000..3053ba92 --- /dev/null +++ b/app/Drupal/niklan/src/Form/Settings/ServicesSettingsForm.php @@ -0,0 +1,68 @@ + 'text_format', + '#title' => new TranslatableMarkup('Body'), + '#description' => new TranslatableMarkup('The description of service page.'), + '#default_value' => $this->getSettings()->getDescription(), + '#allowed_formats' => [$this->getSettings()::TEXT_FORMAT], + '#rows' => 3, + '#required' => TRUE, + ]; + + $form['hourly_rate'] = [ + '#type' => 'textfield', + '#title' => new TranslatableMarkup('Hourly rate'), + '#default_value' => $this->getSettings()->getHourlyRate(), + '#required' => TRUE, + ]; + + $form['actions']['#type'] = 'actions'; + $form['actions']['save'] = [ + '#type' => 'submit', + '#value' => new TranslatableMarkup('Save'), + '#button_type' => 'primary', + ]; + + return $form; + } + + #[\Override] + public function submitForm(array &$form, FormStateInterface $form_state): void { + $this + ->settings + ->setDescription($form_state->getValue(['description', 'value'])) + ->setHourlyRate($form_state->getValue(['hourly_rate'])); + + parent::submitForm($form, $form_state); + } + + #[\Override] + protected function loadSettings(): LanguageAwareSettingsStore { + $settings = $this->getContainer()->get(ServicesSettings::class); + \assert($settings instanceof ServicesSettings); + + return $settings; + } + +} diff --git a/app/Drupal/niklan/src/Form/SettingsForm.php b/app/Drupal/niklan/src/Form/Settings/SettingsForm.php similarity index 51% rename from app/Drupal/niklan/src/Form/SettingsForm.php rename to app/Drupal/niklan/src/Form/Settings/SettingsForm.php index f37fddc7..0fc59cf4 100644 --- a/app/Drupal/niklan/src/Form/SettingsForm.php +++ b/app/Drupal/niklan/src/Form/Settings/SettingsForm.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Drupal\niklan\Form; +namespace Drupal\niklan\Form\Settings; use Drupal\Core\Cache\CacheTagsInvalidatorInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; @@ -13,22 +13,35 @@ use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\niklan\Repository\KeyValue\LanguageAwareSettingsStore; +use Symfony\Component\DependencyInjection\ContainerInterface; +/** + * @see \Drupal\niklan\EventSubscriber\LanguageAwareSettingsRoutes + */ abstract class SettingsForm implements FormInterface, ContainerInjectionInterface { use DependencySerializationTrait; - abstract protected function getMessenger(): MessengerInterface; + protected ?LanguageAwareSettingsStore $settings = NULL; - abstract protected function getCacheTagsInvalidator(): CacheTagsInvalidatorInterface; + abstract protected function loadSettings(): LanguageAwareSettingsStore; - abstract protected function getSettings(): LanguageAwareSettingsStore; + final public function __construct( + protected ContainerInterface $container, + protected MessengerInterface $messenger, + protected CacheTagsInvalidatorInterface $cacheTagsInvalidator, + protected RouteMatchInterface $routeMatch, + ) {} - abstract protected function doBuildForm(array &$form, FormStateInterface $form_state): void; - - abstract protected function doSubmitForm(array &$form, FormStateInterface $form_state): void; - - abstract protected function getRouteMatch(): RouteMatchInterface; + #[\Override] + public static function create(ContainerInterface $container): self { + return new static( + $container->get(ContainerInterface::class), + $container->get(MessengerInterface::class), + $container->get(CacheTagsInvalidatorInterface::class), + $container->get(RouteMatchInterface::class), + ); + } #[\Override] public function validateForm(array &$form, FormStateInterface $form_state): void { @@ -42,8 +55,6 @@ public function buildForm(array $form, FormStateInterface $form_state): array { ->getParameter('key_value_language_aware_code'); $this->getSettings()->changeLanguageCode($language_code); - $this->doBuildForm($form, $form_state); - $form['actions']['#type'] = 'actions'; $form['actions']['save'] = [ '#type' => 'submit', @@ -56,8 +67,6 @@ public function buildForm(array $form, FormStateInterface $form_state): array { #[\Override] public function submitForm(array &$form, FormStateInterface $form_state): void { - $this->doSubmitForm($form, $form_state); - $this ->getMessenger() ->addStatus(new TranslatableMarkup('Settings successfully saved.')); @@ -67,4 +76,30 @@ public function submitForm(array &$form, FormStateInterface $form_state): void { ->invalidateTags($this->getSettings()->getCacheTags()); } + protected function getRouteMatch(): RouteMatchInterface { + return $this->routeMatch; + } + + protected function getMessenger(): MessengerInterface { + return $this->messenger; + } + + protected function getCacheTagsInvalidator(): CacheTagsInvalidatorInterface { + return $this->cacheTagsInvalidator; + } + + protected function getSettings(): LanguageAwareSettingsStore { + if ($this->settings) { + return $this->settings; + } + + $this->settings = $this->loadSettings(); + + return $this->settings; + } + + protected function getContainer(): ContainerInterface { + return $this->container; + } + } diff --git a/app/Drupal/niklan/src/Form/Settings/SupportSettingsForm.php b/app/Drupal/niklan/src/Form/Settings/SupportSettingsForm.php new file mode 100644 index 00000000..db2da2e8 --- /dev/null +++ b/app/Drupal/niklan/src/Form/Settings/SupportSettingsForm.php @@ -0,0 +1,69 @@ + 'text_format', + '#title' => new TranslatableMarkup('Body'), + '#description' => new TranslatableMarkup('The description of support page.'), + '#default_value' => $this->getSettings()->getDescription(), + '#allowed_formats' => [$this->getSettings()::TEXT_FORMAT], + '#rows' => 3, + '#required' => TRUE, + ]; + + $form['donate_url'] = [ + '#type' => 'url', + '#title' => new TranslatableMarkup('Donate URL'), + '#description' => new TranslatableMarkup('The URL of the donate page.'), + '#default_value' => $this->getSettings()->getDonateUrl(), + '#required' => TRUE, + ]; + + $form['actions']['#type'] = 'actions'; + $form['actions']['save'] = [ + '#type' => 'submit', + '#value' => new TranslatableMarkup('Save'), + '#button_type' => 'primary', + ]; + + return $form; + } + + #[\Override] + public function submitForm(array &$form, FormStateInterface $form_state): void { + $this + ->settings + ->setDescription($form_state->getValue(['description', 'value'])) + ->setDonateUrl($form_state->getValue(['donate_url'])); + + parent::submitForm($form, $form_state); + } + + #[\Override] + protected function loadSettings(): LanguageAwareSettingsStore { + $settings = $this->getContainer()->get(SupportSettings::class); + \assert($settings instanceof SupportSettings); + + return $settings; + } + +} diff --git a/app/Drupal/niklan/src/Form/SupportSettingsForm.php b/app/Drupal/niklan/src/Form/SupportSettingsForm.php deleted file mode 100644 index 145a4ed7..00000000 --- a/app/Drupal/niklan/src/Form/SupportSettingsForm.php +++ /dev/null @@ -1,94 +0,0 @@ -get(SupportSettings::class), - $container->get(MessengerInterface::class), - $container->get(CacheTagsInvalidatorInterface::class), - ); - } - - #[\Override] - public function getFormId(): string { - return 'niklan_support_settings'; - } - - #[\Override] - public function buildForm(array $form, FormStateInterface $form_state): array { - $form['#tree'] = TRUE; - - $form['description'] = [ - '#type' => 'text_format', - '#title' => new TranslatableMarkup('Body'), - '#description' => new TranslatableMarkup('The description of support page.'), - '#default_value' => $this->settings->getDescription(), - '#allowed_formats' => [SupportSettings::TEXT_FORMAT], - '#rows' => 3, - '#required' => TRUE, - ]; - - $form['donate_url'] = [ - '#type' => 'url', - '#title' => new TranslatableMarkup('Donate URL'), - '#description' => new TranslatableMarkup('The URL of the donate page.'), - '#default_value' => $this->settings->getDonateUrl(), - '#required' => TRUE, - ]; - - $form['actions']['#type'] = 'actions'; - $form['actions']['save'] = [ - '#type' => 'submit', - '#value' => new TranslatableMarkup('Save'), - '#button_type' => 'primary', - ]; - - return $form; - } - - #[\Override] - public function submitForm(array &$form, FormStateInterface $form_state): void { - $this - ->settings - ->setDescription($form_state->getValue(['description', 'value'])) - ->setDonateUrl($form_state->getValue(['donate_url'])); - - $this - ->messenger - ->addStatus(new TranslatableMarkup('Settings successfully saved.')); - - $this - ->cacheTagsInvalidator - ->invalidateTags($this->settings->getCacheTags()); - } - - #[\Override] - public function validateForm(array &$form, FormStateInterface $form_state): void { - // Not needed. - } - -} diff --git a/app/Drupal/niklan/src/Factory/KeyValue/DatabaseLanguageAwareFactory.php b/app/Drupal/niklan/src/Repository/KeyValue/DatabaseLanguageAwareFactory.php similarity index 85% rename from app/Drupal/niklan/src/Factory/KeyValue/DatabaseLanguageAwareFactory.php rename to app/Drupal/niklan/src/Repository/KeyValue/DatabaseLanguageAwareFactory.php index c4d1449c..54fb9f7a 100644 --- a/app/Drupal/niklan/src/Factory/KeyValue/DatabaseLanguageAwareFactory.php +++ b/app/Drupal/niklan/src/Repository/KeyValue/DatabaseLanguageAwareFactory.php @@ -2,15 +2,14 @@ declare(strict_types=1); -namespace Drupal\niklan\Factory\KeyValue; +namespace Drupal\niklan\Repository\KeyValue; use Drupal\Component\Assertion\Inspector; use Drupal\Component\Serialization\SerializationInterface; use Drupal\Core\Database\Connection; use Drupal\Core\Language\LanguageManagerInterface; -use Drupal\niklan\Contract\Factory\KeyValue\LanguageAwareFactory; +use Drupal\niklan\Contract\Repository\KeyValue\LanguageAwareFactory; use Drupal\niklan\Contract\Repository\KeyValue\LanguageAwareStore; -use Drupal\niklan\Repository\KeyValue\DatabaseLanguageAwareStore; use Symfony\Component\DependencyInjection\Attribute\Autowire; /** @@ -23,7 +22,7 @@ final class DatabaseLanguageAwareFactory implements LanguageAwareFactory { /** * The list of initialized storages. * - * @var array> + * @var \Drupal\niklan\Contract\Repository\KeyValue\LanguageAwareStore */ private array $storages = []; diff --git a/app/Drupal/niklan/src/Repository/KeyValue/LanguageAwareSettingsStore.php b/app/Drupal/niklan/src/Repository/KeyValue/LanguageAwareSettingsStore.php index e8c2ab78..66d55ea3 100644 --- a/app/Drupal/niklan/src/Repository/KeyValue/LanguageAwareSettingsStore.php +++ b/app/Drupal/niklan/src/Repository/KeyValue/LanguageAwareSettingsStore.php @@ -7,7 +7,7 @@ use Drupal\Core\Cache\CacheableDependencyInterface; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\KeyValueStore\KeyValueStoreInterface; -use Drupal\niklan\Contract\Factory\KeyValue\LanguageAwareFactory; +use Drupal\niklan\Contract\Repository\KeyValue\LanguageAwareFactory; use Symfony\Component\DependencyInjection\Attribute\Autowire; abstract class LanguageAwareSettingsStore implements CacheableDependencyInterface { @@ -31,7 +31,7 @@ public function changeLanguageCode(?string $language_code): self { #[\Override] public function getCacheContexts(): array { - return []; + return ['languages:language_interface']; } #[\Override] diff --git a/app/Drupal/niklan/src/Factory/KeyValue/ServiceContainerLanguageAwareFactory.php b/app/Drupal/niklan/src/Repository/KeyValue/ServiceContainerLanguageAwareFactory.php similarity index 90% rename from app/Drupal/niklan/src/Factory/KeyValue/ServiceContainerLanguageAwareFactory.php rename to app/Drupal/niklan/src/Repository/KeyValue/ServiceContainerLanguageAwareFactory.php index 81350830..f3302bd4 100644 --- a/app/Drupal/niklan/src/Factory/KeyValue/ServiceContainerLanguageAwareFactory.php +++ b/app/Drupal/niklan/src/Repository/KeyValue/ServiceContainerLanguageAwareFactory.php @@ -2,11 +2,11 @@ declare(strict_types=1); -namespace Drupal\niklan\Factory\KeyValue; +namespace Drupal\niklan\Repository\KeyValue; use Drupal\Component\Assertion\Inspector; use Drupal\Core\Language\LanguageManagerInterface; -use Drupal\niklan\Contract\Factory\KeyValue\LanguageAwareFactory; +use Drupal\niklan\Contract\Repository\KeyValue\LanguageAwareFactory; use Drupal\niklan\Contract\Repository\KeyValue\LanguageAwareStore; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -31,7 +31,7 @@ final class ServiceContainerLanguageAwareFactory implements LanguageAwareFactory /** * The list of initialized storages. * - * @var array> + * @var \Drupal\niklan\Contract\Repository\KeyValue\LanguageAwareStore */ private array $stores = []; diff --git a/app/Drupal/niklan/tests/src/Functional/Form/AboutSettingsFormTest.php b/app/Drupal/niklan/tests/src/Functional/Form/AboutSettingsFormTest.php index b0958391..3f9da079 100644 --- a/app/Drupal/niklan/tests/src/Functional/Form/AboutSettingsFormTest.php +++ b/app/Drupal/niklan/tests/src/Functional/Form/AboutSettingsFormTest.php @@ -9,7 +9,7 @@ use Drupal\file\Entity\File; use Drupal\media\Entity\Media; use Drupal\media\MediaInterface; -use Drupal\niklan\Form\AboutSettingsForm; +use Drupal\niklan\Form\Settings\AboutSettingsForm; use Drupal\niklan\Repository\AboutSettingsInterface; use Drupal\responsive_image\Entity\ResponsiveImageStyle; use Drupal\Tests\media\Traits\MediaTypeCreationTrait; @@ -20,7 +20,7 @@ /** * Provides a test for about settings form. * - * @coversDefaultClass \Drupal\niklan\Form\AboutSettingsForm + * @coversDefaultClass \Drupal\niklan\Form\Settings\AboutSettingsForm */ final class AboutSettingsFormTest extends NiklanTestBase {