Skip to content

Commit

Permalink
Improves support for snippets defined in partials
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeTowers committed Jan 4, 2025
1 parent 4310f55 commit b3a0765
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
20 changes: 14 additions & 6 deletions classes/Snippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ public static function processTemplateSettings($template)
foreach ($parsedProperties as $index => &$property) {
$property['id'] = $index;

if (isset($property['options'])) {
if (isset($property['options']) && is_array($property['options'])) {
$property['options'] = self::dropDownOptionsToString($property['options']);
}
}
Expand Down Expand Up @@ -408,18 +408,26 @@ protected static function preprocessPropertyValues($theme, $snippetCode, $compon

/**
* Converts a keyed object to an array, converting the index to the "property" value.
* @return array
*/
protected static function parseIniProperties($properties)
protected static function parseIniProperties($properties): array
{
foreach ($properties as $index => $value) {
$properties[$index]['property'] = $index;
if (!is_array($value['options'])) {
$value['options'] = self::dropDownOptionsToArray($value['options']);
}

if (is_int($index)) {
$properties[$value['property']] = $value;
unset($properties[$index]);
} elseif (is_string($index)) {
$properties[$index]['property'] = $index;
}
}

return array_values($properties);
}

protected static function dropDownOptionsToArray($optionsString)
protected static function dropDownOptionsToArray(string $optionsString): array
{
$options = explode('|', $optionsString);

Expand Down Expand Up @@ -447,7 +455,7 @@ protected static function dropDownOptionsToArray($optionsString)
return $result;
}

protected static function dropDownOptionsToString($optionsArray)
protected static function dropDownOptionsToString(array $optionsArray)
{
$result = [];
$isAssoc = (bool) count(array_filter(array_keys($optionsArray), 'is_string'));
Expand Down
4 changes: 2 additions & 2 deletions classes/SnippetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function removeSnippet(string $snippetCode)
* @param string $code Specifies the snippet code.
* @param string $$componentClass Specifies the snippet component class, if available.
* @param boolean $allowCaching Specifies whether caching is allowed for the call.
* @return array Returns an array of Snippet objects.
* @return Snippet|null Returns the Snippet object if found
*/
public function findByCodeOrComponent($theme, $code, $componentClass, $allowCaching = false)
{
Expand Down Expand Up @@ -185,7 +185,7 @@ protected static function getPartialMapCacheKey($theme)
/**
* Returns a list of partial-based snippets and corresponding partial names.
* @param \Cms\Classes\Theme $theme Specifies a parent theme.
* @return Returns an associative array with the snippet code in keys and partial file names in values.
* @return array Returns an associative array with the snippet code in keys and partial file names in values.
*/
public function getPartialSnippetMap($theme)
{
Expand Down

0 comments on commit b3a0765

Please sign in to comment.