Skip to content
Snippets Groups Projects
Commit 1070f959 authored by Demian Katz's avatar Demian Katz
Browse files

Move related record to view helper from record driver.

- More logical separation of concerns
- Fixes compatibility with Search2 backend
parent 7e879105
No related merge requests found
...@@ -32,6 +32,7 @@ skip_numeric = true ...@@ -32,6 +32,7 @@ skip_numeric = true
[Record] [Record]
next_prev_navigation = false next_prev_navigation = false
related[] = Similar
; ---------- searches.ini settings ---------- ; ---------- searches.ini settings ----------
......
...@@ -296,41 +296,6 @@ abstract class AbstractBase implements \VuFind\Db\Table\DbTableAwareInterface, ...@@ -296,41 +296,6 @@ abstract class AbstractBase implements \VuFind\Db\Table\DbTableAwareInterface,
return $this->sourceIdentifier; return $this->sourceIdentifier;
} }
/**
* Return an array of related record suggestion objects (implementing the
* \VuFind\Related\RelatedInterface) based on the current record.
*
* @param \VuFind\Related\PluginManager $factory Related module plugin factory
* @param array $types Array of relationship types to
* load; each entry should be a service name (i.e. 'Similar' or 'Editions')
* optionally followed by a colon-separated list of parameters to pass to the
* constructor. If the parameter is set to null instead of an array, default
* settings will be loaded from config.ini.
*
* @return array
*/
public function getRelated(\VuFind\Related\PluginManager $factory, $types = null)
{
if (null === $types) {
$types = isset($this->recordConfig->Record->related) ?
$this->recordConfig->Record->related : [];
}
$retVal = [];
foreach ($types as $current) {
$parts = explode(':', $current);
$type = $parts[0];
$params = $parts[1] ?? null;
if ($factory->has($type)) {
$plugin = $factory->get($type);
$plugin->init($params, $this);
$retVal[] = $plugin;
} else {
throw new \Exception("Related module {$type} does not exist.");
}
}
return $retVal;
}
/** /**
* Returns true if the record supports real-time AJAX status lookups. * Returns true if the record supports real-time AJAX status lookups.
* *
......
...@@ -27,6 +27,10 @@ ...@@ -27,6 +27,10 @@
*/ */
namespace VuFind\View\Helper\Root; namespace VuFind\View\Helper\Root;
use VuFind\Config\PluginManager as ConfigManager;
use VuFind\Related\PluginManager as RelatedManager;
use VuFind\Search\Options\PluginManager as OptionsManager;
/** /**
* Related records view helper * Related records view helper
* *
...@@ -38,10 +42,24 @@ namespace VuFind\View\Helper\Root; ...@@ -38,10 +42,24 @@ namespace VuFind\View\Helper\Root;
*/ */
class Related extends AbstractClassBasedTemplateRenderer class Related extends AbstractClassBasedTemplateRenderer
{ {
/**
* Config manager
*
* @var ConfigManager
*/
protected $configManager;
/**
* Plugin manager for search options.
*
* @var OptionsManager
*/
protected $optionsManager;
/** /**
* Plugin manager for related record modules. * Plugin manager for related record modules.
* *
* @var \VuFind\Related\PluginManager * @var RelatedManager
*/ */
protected $pluginManager; protected $pluginManager;
...@@ -51,9 +69,31 @@ class Related extends AbstractClassBasedTemplateRenderer ...@@ -51,9 +69,31 @@ class Related extends AbstractClassBasedTemplateRenderer
* @param \VuFind\Related\PluginManager $pluginManager Plugin manager for related * @param \VuFind\Related\PluginManager $pluginManager Plugin manager for related
* record modules. * record modules.
*/ */
public function __construct(\VuFind\Related\PluginManager $pluginManager) public function __construct(RelatedManager $pluginManager,
{ ConfigManager $cm, OptionsManager $om
) {
$this->pluginManager = $pluginManager; $this->pluginManager = $pluginManager;
$this->configManager = $cm;
$this->optionsManager = $om;
}
/**
* Given a record source ID, return the appropriate related record configuration.
*
* @param string $source Source identifier
*
* @return array
*/
protected function getConfigForSource($source)
{
$options = $this->optionsManager->get($source);
$configName = $options->getSearchIni();
// Special case -- default Solr stores [Record] section in config.ini
if ($configName === 'searches') {
$configName = 'config';
}
$config = $this->configManager->get($configName);
return $config->Record->related ?? [];
} }
/** /**
...@@ -65,7 +105,21 @@ class Related extends AbstractClassBasedTemplateRenderer ...@@ -65,7 +105,21 @@ class Related extends AbstractClassBasedTemplateRenderer
*/ */
public function getList(\VuFind\RecordDriver\AbstractBase $driver) public function getList(\VuFind\RecordDriver\AbstractBase $driver)
{ {
return $driver->getRelated($this->pluginManager); $retVal = [];
$config = $this->getConfigForSource($driver->getSourceIdentifier());
foreach ($config as $current) {
$parts = explode(':', $current);
$type = $parts[0];
$params = $parts[1] ?? null;
if ($this->pluginManager->has($type)) {
$plugin = $this->pluginManager->get($type);
$plugin->init($params, $driver);
$retVal[] = $plugin;
} else {
throw new \Exception("Related module {$type} does not exist.");
}
}
return $retVal;
} }
/** /**
......
...@@ -61,6 +61,10 @@ class RelatedFactory implements FactoryInterface ...@@ -61,6 +61,10 @@ class RelatedFactory implements FactoryInterface
if (!empty($options)) { if (!empty($options)) {
throw new \Exception('Unexpected options sent to factory.'); throw new \Exception('Unexpected options sent to factory.');
} }
return new $requestedName($container->get('VuFind\Related\PluginManager')); return new $requestedName(
$container->get('VuFind\Related\PluginManager'),
$container->get('VuFind\Config\PluginManager'),
$container->get('VuFind\Search\Options\PluginManager')
);
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment