Add complex page link handling to ComponentBase
This commit is contained in:
parent
8ec4cd6ea3
commit
9b542efe6f
|
|
@ -152,6 +152,9 @@ class CmsObject implements ArrayAccess
|
|||
if (!FileHelper::validatePath($fileName, static::getMaxAllowedPathNesting()))
|
||||
throw new SystemException(Lang::get('cms::lang.cms_object.invalid_file', ['name'=>$fileName]));
|
||||
|
||||
if (!strlen(File::extension($fileName)))
|
||||
$fileName .= '.htm';
|
||||
|
||||
$fullPath = static::getFilePath($theme, $fileName);
|
||||
|
||||
if (!File::isFile($fullPath))
|
||||
|
|
|
|||
|
|
@ -54,6 +54,11 @@ abstract class ComponentBase extends Extendable
|
|||
*/
|
||||
protected $page;
|
||||
|
||||
/**
|
||||
* @var array Cache of linked Component objects, used for page links.
|
||||
*/
|
||||
protected $pageLinkCache = [];
|
||||
|
||||
/**
|
||||
* Component constructor. Takes in the page or layout code section object
|
||||
* and properties set by the page or layout.
|
||||
|
|
@ -146,4 +151,37 @@ abstract class ComponentBase extends Extendable
|
|||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a page link to another page. Allows mapping to the other page's
|
||||
* component properties for the purpose of extracting URL routing parameters.
|
||||
* @param string $page Page name or page file name
|
||||
* @param string $class Component class name
|
||||
* @param array $mappings ['componentProperty' => 'routed value']
|
||||
* @return string
|
||||
*/
|
||||
protected function makePageLink($page, $class, $mappings = [])
|
||||
{
|
||||
if (!isset($this->pageLinkCache[$page.$class])) {
|
||||
$this->pageLinkCache[$page.$class] = $this->getOtherPageComponent($page, $class);
|
||||
}
|
||||
|
||||
if (!$component = $this->pageLinkCache[$page.$class])
|
||||
return null;
|
||||
|
||||
$params = [];
|
||||
foreach ($mappings as $property => $value) {
|
||||
|
||||
if (!$param = $component->property($property))
|
||||
continue;
|
||||
|
||||
if (substr($param, 0, 1) == ':')
|
||||
$param = substr($param, 1);
|
||||
|
||||
$params[$param] = $value;
|
||||
}
|
||||
|
||||
return $this->pageUrl($page, $params);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -162,7 +162,7 @@ class ComponentManager
|
|||
public function hasComponent($name)
|
||||
{
|
||||
$className = $this->resolve($name);
|
||||
if (!$className)
|
||||
if (!$className)
|
||||
return false;
|
||||
|
||||
return isset($this->classMap[$className]);
|
||||
|
|
|
|||
Loading…
Reference in New Issue