Skip to content

Commit

Permalink
Used a helper to get the default site slug.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-KM committed Aug 12, 2018
1 parent 0f02ea1 commit 4ce8388
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
1 change: 1 addition & 0 deletions application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,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,
'media' => Service\ViewHelper\MediaFactory::class,
'navigationLink' => Service\ViewHelper\NavigationLinkFactory::class,
Expand Down
33 changes: 33 additions & 0 deletions application/src/Service/ViewHelper/DefaultSiteSlugFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Omeka\Service\ViewHelper;

use Interop\Container\ContainerInterface;
use Omeka\View\Helper\DefaultSiteSlug;
use Zend\ServiceManager\Factory\FactoryInterface;

/**
* Service factory to get the default site slug, or the first site slug.
*
* @todo Store the default site as slug instead of id?
* @todo Set a setting for the default site of the user?
*/
class DefaultSiteSlugFactory implements FactoryInterface
{
/**
* Create and return the DefaultSiteSlug view helper.
*
* @return DefaultSiteSlug
*/
public function __invoke(ContainerInterface $services, $requestedName, array $options = null)
{
$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 = (string) reset($slugs);
return new DefaultSiteSlug($defaultSiteSlug);
}
}
16 changes: 4 additions & 12 deletions application/src/Service/ViewHelper/PublicResourceUrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
35 changes: 35 additions & 0 deletions application/src/View/Helper/DefaultSiteSlug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace Omeka\View\Helper;

use Zend\View\Helper\AbstractHelper;

/**
* View helper to get the default site slug, or the first one.
*/
class DefaultSiteSlug extends AbstractHelper
{
/**
* @var string
*/
protected $defaultSiteSlug;

/**
* Construct the helper.
*
* @param string|null $defaultSiteSlug
*/
public function __construct($defaultSiteSlug)
{
$this->defaultSiteSlug = $defaultSiteSlug;
}

/**
* Return the default site slug, or the first one.
*
* @return string|null
*/
public function __invoke()
{
return $this->defaultSiteSlug;
}
}

0 comments on commit 4ce8388

Please sign in to comment.