Problem/Motivation
The generateFromRequest method in the FileNameGenerator class of the ckeditor5_premium_features module throws a warning: Undefined array key 1. This issue occurs when the route name does not contain a dot (.), causing the explode('.', $route_name) function to return an array with fewer than two elements. Consequently, attempting to access $route_param[1] results in a PHP warning. This warning can cause unexpected behavior and errors when generating file names based on route and entity parameters.
Steps to Reproduce
- Use a route name that does not include a dot, such as
protected_pages_login_page.
- Call the
generateFromRequest method in the context where this route name is active.
- Observe the PHP warning:
Undefined array key 1 in FileNameGenerator->generateFromRequest().
Proposed Resolution
- Update the
generateFromRequest method to check if the $route_param array has more than one element before attempting to access $route_param[1].
- Implement fallback logic to handle cases where the route name format does not include a dot, defaulting to a common entity name or another logical fallback.
- Example modification:
public function generateFromRequest(): string {
$route_name = $this->routeMatch->getRouteName();
$route_param = explode('.', $route_name);
// Safely check if $route_param has enough elements
if (count($route_param) > 1) {
$entity = $this->routeMatch->getParameter($route_param[1]);
} else {
// Fallback if the route name doesn't contain a dot
$entity = $this->routeMatch->getParameter('node'); // or use another default parameter
}
try {
if ($entity) {
$alias = $entity->toUrl()->toString();
return $this->convertUrlToFileName($alias);
}
} catch (\Exception $e) {
// Handle exceptions if necessary
}
return self::DEFAULT_FILENAME;
}
- This change will ensure that the function can handle route names without dots and avoid PHP warnings related to undefined array keys.
Comments
Comment #2
meladawy commentedComment #3
meladawy commentedComment #6
salmonek commentedHi Maged
Thank you for reporting the issue. I've proposed a little deeper changes for generateFromRequest()
Instead taking the `node` argument, there is iteration over all arguments and if there is a EntityInterface avaialble it will be selected. So name can be extracted from for example Block.
Secondly instead url a label is loaded as entity can be url-less or pretty urls may not be set.
Lastly change of default filename to "document"
Comment #8
salmonek commentedTested by our QA team. Merged and will be published today in new release.
Comment #9
salmonek commentedFix published in version 1.4.0