/**
* Returns [route name, route params] for url generation, or null for various reasons.
*
* @return array|null
*/
public function getRouteNameAndParams()
{
if (empty($this->app)) {
$this->app = ResourceManager::getApp();
}
if (empty($this->id)) {
return null;
}
// No links for records that are 'viewless'
if (isset($this->contenttype['viewless']) && $this->contenttype['viewless'] == true) {
return null;
}
list($name, $config) = $this->getRouteConfig();
if (!$config) {
return null;
}
$slug = $this->getLinkSlug();
$availableParams = array_filter(array_merge($config['defaults'] ?: [], $this->getRouteRequirementParams($config), ['contenttypeslug' => $this->contenttype['singular_slug'], 'id' => $this->id, 'slug' => $slug]));
/** @var Route|null $route */
$route = $this->app['routes']->get($name);
if (!$route) {
return null;
}
// Needed params as array keys
$pathVars = $route->compile()->getPathVariables();
$neededKeys = array_flip($pathVars);
// Set the values of neededKeys from the availableParams.
// This removes extra parameters that are not needed for url generation.
$params = array_replace($neededKeys, array_intersect_key($availableParams, $neededKeys));
return [$name, $params];
}