From 920dbfaec39d461a4959b1156a2780e34c801cf1 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Mon, 28 May 2018 00:00:00 +0200 Subject: [PATCH] Used a helper to get the default site slug. --- application/config/module.config.php | 1 + .../ViewHelper/DefaultSiteSlugFactory.php | 33 +++++++++++++++++ .../ViewHelper/PublicResourceUrlFactory.php | 16 +++------ .../src/View/Helper/DefaultSiteSlug.php | 35 +++++++++++++++++++ 4 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 application/src/Service/ViewHelper/DefaultSiteSlugFactory.php create mode 100644 application/src/View/Helper/DefaultSiteSlug.php diff --git a/application/config/module.config.php b/application/config/module.config.php index 96b56f7b29..2b0b43805d 100644 --- a/application/config/module.config.php +++ b/application/config/module.config.php @@ -412,6 +412,7 @@ 'blockLayout' => Service\ViewHelper\BlockLayoutFactory::class, 'blockThumbnailTypeSelect' => Service\ViewHelper\BlockThumbnailTypeSelectFactory::class, 'dataType' => Service\ViewHelper\DataTypeFactory::class, + 'defaultSiteSlug' => Service\ViewHelper\DefaultSiteSlugFactory::class, 'i18n' => Service\ViewHelper\I18nFactory::class, 'logger' => Service\ViewHelper\LoggerFactory::class, 'media' => Service\ViewHelper\MediaFactory::class, diff --git a/application/src/Service/ViewHelper/DefaultSiteSlugFactory.php b/application/src/Service/ViewHelper/DefaultSiteSlugFactory.php new file mode 100644 index 0000000000..383c0be988 --- /dev/null +++ b/application/src/Service/ViewHelper/DefaultSiteSlugFactory.php @@ -0,0 +1,33 @@ +get('Omeka\Settings')->get('default_site'); + $api = $services->get('Omeka\ApiManager'); + if ($defaultSiteId) { + $slugs = $api->search('sites', ['id' => $defaultSiteId], ['returnScalar' => 'slug'])->getContent(); + } else { + $slugs = $api->search('sites', ['limit' => 1], ['returnScalar' => 'slug'])->getContent(); + } + $defaultSiteSlug = (string) reset($slugs); + return new DefaultSiteSlug($defaultSiteSlug); + } +} diff --git a/application/src/Service/ViewHelper/PublicResourceUrlFactory.php b/application/src/Service/ViewHelper/PublicResourceUrlFactory.php index b425dcad7e..1a9e5e8375 100644 --- a/application/src/Service/ViewHelper/PublicResourceUrlFactory.php +++ b/application/src/Service/ViewHelper/PublicResourceUrlFactory.php @@ -13,21 +13,13 @@ class PublicResourceUrlFactory implements FactoryInterface { /** - * Create and return the PublicResourceUrlFactory view helper + * Create and return the PublicResourceUrl view helper * - * @return PublicResourceUrlFactory + * @return PublicResourceUrl */ public function __invoke(ContainerInterface $services, $requestedName, array $options = null) { - // Get the slug for the default site, else the first one. - $defaultSiteId = $services->get('Omeka\Settings')->get('default_site'); - $api = $services->get('Omeka\ApiManager'); - if ($defaultSiteId) { - $slugs = $api->search('sites', ['id' => $defaultSiteId], ['returnScalar' => 'slug'])->getContent(); - } else { - $slugs = $api->search('sites', ['limit' => 1], ['returnScalar' => 'slug'])->getContent(); - } - $defaultSiteSlug = reset($slugs); - return new PublicResourceUrl($defaultSiteSlug); + $defaultSiteSlug = $services->get('ViewHelperManager')->get('defaultSiteSlug'); + return new PublicResourceUrl($defaultSiteSlug()); } } diff --git a/application/src/View/Helper/DefaultSiteSlug.php b/application/src/View/Helper/DefaultSiteSlug.php new file mode 100644 index 0000000000..0d729f79ef --- /dev/null +++ b/application/src/View/Helper/DefaultSiteSlug.php @@ -0,0 +1,35 @@ +defaultSiteSlug = $defaultSiteSlug; + } + + /** + * Return the default site slug, or the first one. + * + * @return string|null + */ + public function __invoke() + { + return $this->defaultSiteSlug; + } +}