Skip to content

Commit

Permalink
Added a view helper to get the default site slug (to be used in admin).
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-KM committed Dec 16, 2018
1 parent 6b485ec commit 23bc95c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'userBar' => View\Helper\UserBar::class,
],
'factories' => [
'defaultSiteSlug' => Service\ViewHelper\DefaultSiteSlugFactory::class,
'logger' => Service\ViewHelper\LoggerFactory::class,
],
],
Expand Down
33 changes: 33 additions & 0 deletions src/Service/ViewHelper/DefaultSiteSlugFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Next\Service\ViewHelper;

use Interop\Container\ContainerInterface;
use Next\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);
}
}
35 changes: 35 additions & 0 deletions src/View/Helper/DefaultSiteSlug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace Next\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 23bc95c

Please sign in to comment.