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

More refactoring of tab handling for easier reuse.

parent 677da214
No related merge requests found
......@@ -587,6 +587,24 @@ class AbstractRecord extends AbstractBase
return $cfg['vufind']['recorddriver_tabs'];
}
/**
* Support method to load tab information from the RecordTabPluginManager.
*
* @return void
*/
protected function loadTabDetails()
{
$driver = $this->loadRecord();
$request = $this->getRequest();
$rtpm = $this->getServiceLocator()->get('VuFind\RecordTabPluginManager');
$details = $rtpm->getTabDetailsForRecord(
$driver, $this->getTabConfiguration(), $request,
$this->fallbackDefaultTab
);
$this->allTabs = $details['tabs'];
$this->defaultTab = $details['default'] ? $details['default'] : false;
}
/**
* Get default tab for a given driver
*
......@@ -596,26 +614,8 @@ class AbstractRecord extends AbstractBase
{
// Load default tab if not already retrieved:
if (null === $this->defaultTab) {
// Load record driver tab configuration:
$driver = $this->loadRecord();
$this->defaultTab = $this->getServiceLocator()
->get('VuFind\RecordTabPluginManager')
->getDefaultTabForRecord($driver, $this->getTabConfiguration());
// Missing/invalid record driver configuration? Fall back to configured
// default:
$tabs = $this->getAllTabs();
if (empty($this->defaultTab) || !isset($tabs[$this->defaultTab])) {
$this->defaultTab = $this->fallbackDefaultTab;
}
// Is configured tab also invalid? If so, pick first existing tab:
if (empty($this->defaultTab) || !isset($tabs[$this->defaultTab])) {
$keys = array_keys($tabs);
$this->defaultTab = isset($keys[0]) ? $keys[0] : '';
}
$this->loadTabDetails();
}
return $this->defaultTab;
}
......@@ -627,11 +627,7 @@ class AbstractRecord extends AbstractBase
protected function getAllTabs()
{
if (null === $this->allTabs) {
$driver = $this->loadRecord();
$request = $this->getRequest();
$this->allTabs = $this->getServiceLocator()
->get('VuFind\RecordTabPluginManager')
->getTabsForRecord($driver, $this->getTabConfiguration(), $request);
$this->loadTabDetails();
}
return $this->allTabs;
}
......
......@@ -99,16 +99,56 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
* Get a default tab by looking up the provided record driver in the tab
* configuration array.
*
* @param AbstractRecordDriver $driver Record driver
* @param array $config Tab configuration (map of
* @param AbstractRecordDriver $driver Record driver
* @param array $config Tab configuration (map of
* driver class => tab configuration)
* @param array $tabs Details on available tabs (returned
* from getTabsForRecord()).
* @param string $fallback Fallback to use if no tab specified
* or matched.
*
* @return string
*/
public function getDefaultTabForRecord(AbstractRecordDriver $driver,
array $config
array $config, array $tabs, $fallback = null
) {
// Load default from module configuration:
$default = $this->getConfigByClass($driver, $config, 'defaultTab', null);
// Missing/invalid record driver configuration? Fall back to provided
// default:
if ((!$default || !isset($tabs[$default])) && isset($tabs[$fallback])) {
$default = $fallback;
}
// Is configured tab still invalid? If so, pick first existing tab:
if ((!$default || !isset($tabs[$default])) && !empty($tabs)) {
$keys = array_keys($tabs);
$default = $keys[0];
}
return $default;
}
/**
* Convenience method to load tab information, including default, in a
* single pass. Returns an associative array with 'tabs' and 'default' keys.
*
* @param AbstractRecordDriver $driver Record driver
* @param array $config Tab configuration (map of
* driver class => tab configuration)
* @param \Zend\Http\Request $request User request (optional)
* @param string $fallback Fallback default tab to use if no
* tab specified or matched.
*
* @return array
*/
public function getTabDetailsForRecord(AbstractRecordDriver $driver,
array $config, $request = null, $fallback = null
) {
return $this->getConfigByClass($driver, $config, 'defaultTab', null);
$tabs = $this->getTabsForRecord($driver, $config, $request);
$default = $this->getDefaultTabForRecord($driver, $config, $tabs, $fallback);
return compact('tabs', 'default');
}
/**
......
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