From 77b26b36fdfc28538f8050e877b8a46ec0b00478 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Wed, 18 Oct 2017 12:32:08 +1300 Subject: [PATCH] BUG Fix page icons in vendor modules --- code/Controllers/CMSMain.php | 16 ++++----- .../LeftAndMainPageIconsExtension.php | 32 ++++++------------ code/Model/SiteTree.php | 29 ++++++++++++++++ .../LeftAndMainPageIconsExtensionTest.php | 30 ++++++++++++++++ .../ModuleIconA.php | 11 ++++++ .../ModuleIconB.php | 11 ++++++ .../ModuleIconC.php | 13 +++++++ .../ModuleIconExtension.php | 21 ++++++++++++ .../icon_b.jpg | Bin 0 -> 2292 bytes .../icon_c.jpg | Bin 0 -> 2292 bytes 10 files changed, 132 insertions(+), 31 deletions(-) create mode 100644 tests/php/Controllers/LeftAndMainPageIconsExtensionTest.php create mode 100644 tests/php/Controllers/LeftAndMainPageIconsExtensionTest/ModuleIconA.php create mode 100644 tests/php/Controllers/LeftAndMainPageIconsExtensionTest/ModuleIconB.php create mode 100644 tests/php/Controllers/LeftAndMainPageIconsExtensionTest/ModuleIconC.php create mode 100644 tests/php/Controllers/LeftAndMainPageIconsExtensionTest/ModuleIconExtension.php create mode 100644 tests/php/Controllers/LeftAndMainPageIconsExtensionTest/icon_b.jpg create mode 100644 tests/php/Controllers/LeftAndMainPageIconsExtensionTest/icon_c.jpg diff --git a/code/Controllers/CMSMain.php b/code/Controllers/CMSMain.php index 6ff07e63..6a99f745 100644 --- a/code/Controllers/CMSMain.php +++ b/code/Controllers/CMSMain.php @@ -24,6 +24,7 @@ use SilverStripe\Control\HTTPResponse_Exception; use SilverStripe\Core\Convert; use SilverStripe\Core\Environment; use SilverStripe\Core\Injector\Injector; +use SilverStripe\Core\Manifest\ModuleResource; use SilverStripe\Core\Manifest\ModuleResourceLoader; use SilverStripe\Forms\DateField; use SilverStripe\Forms\DropdownField; @@ -1056,20 +1057,17 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr } // skip this type if it is restricted - if ($instance->config()->get('need_permission') && !$this->can(singleton($class)->config()->get('need_permission'))) { + $needPermissions = $instance->config()->get('need_permission'); + if ($needPermissions && !$this->can($needPermissions)) { continue; } - $singularName = $instance->i18n_singular_name(); - $description = $instance->i18n_classDescription(); - $result->push(new ArrayData(array( 'ClassName' => $class, - 'AddAction' => $singularName, - 'Description' => $description, - // TODO Sprite support - 'IconURL' => ModuleResourceLoader::resourceURL($instance->config()->get('icon')), - 'Title' => $singularName, + 'AddAction' => $instance->i18n_singular_name(), + 'Description' => $instance->i18n_classDescription(), + 'IconURL' => $instance->getPageIconURL(), + 'Title' => $instance->i18n_singular_name(), ))); } diff --git a/code/Controllers/LeftAndMainPageIconsExtension.php b/code/Controllers/LeftAndMainPageIconsExtension.php index aa95dc61..9ea68f60 100644 --- a/code/Controllers/LeftAndMainPageIconsExtension.php +++ b/code/Controllers/LeftAndMainPageIconsExtension.php @@ -3,11 +3,11 @@ namespace SilverStripe\CMS\Controllers; use SilverStripe\CMS\Model\SiteTree; -use SilverStripe\Core\Convert; -use SilverStripe\View\Requirements; use SilverStripe\Core\ClassInfo; -use SilverStripe\Control\Director; +use SilverStripe\Core\Config\Config; +use SilverStripe\Core\Convert; use SilverStripe\Core\Extension; +use SilverStripe\View\Requirements; /** * Extension to include custom page icons @@ -32,28 +32,16 @@ class LeftAndMainPageIconsExtension extends Extension $classes = ClassInfo::subclassesFor(SiteTree::class); foreach ($classes as $class) { - $obj = singleton($class); - $iconSpec = $obj->config()->get('icon'); - - if (!$iconSpec) { + $icon = Config::inst()->get($class, 'icon'); + if (!$icon) { continue; } - // Legacy support: We no longer need separate icon definitions for folders etc. - $iconFile = (is_array($iconSpec)) ? $iconSpec[0] : $iconSpec; - - // Legacy support: Add file extension if none exists - if (!pathinfo($iconFile, PATHINFO_EXTENSION)) { - $iconFile .= '-file.gif'; - } - - $class = Convert::raw2htmlid($class); - $selector = ".page-icon.class-$class, li.class-$class > a .jstree-pageicon"; - if (Director::fileExists($iconFile)) { - $css .= "$selector { background: transparent url('$iconFile') 0 0 no-repeat; }\n"; - } else { - // Support for more sophisticated rules, e.g. sprited icons - $css .= "$selector { $iconFile }\n"; + $cssClass = Convert::raw2htmlid($class); + $selector = ".page-icon.class-$cssClass, li.class-$cssClass > a .jstree-pageicon"; + $iconURL = SiteTree::singleton($class)->getPageIconURL(); + if ($iconURL) { + $css .= "$selector { background: transparent url('$iconURL') 0 0 no-repeat; }\n"; } } diff --git a/code/Model/SiteTree.php b/code/Model/SiteTree.php index 0d7d42b2..a6d01ce6 100755 --- a/code/Model/SiteTree.php +++ b/code/Model/SiteTree.php @@ -17,6 +17,8 @@ use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Convert; use SilverStripe\Core\Injector\Injector; +use SilverStripe\Core\Manifest\ModuleResource; +use SilverStripe\Core\Manifest\ModuleResourceLoader; use SilverStripe\Core\Resettable; use SilverStripe\Dev\Deprecation; use SilverStripe\Forms\CheckboxField; @@ -2841,6 +2843,33 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi return parent::plural_name(); } + /** + * Generate link to this page's icon + * + * @return string + */ + public function getPageIconURL() + { + $icon = $this->config()->get('icon'); + if (!$icon) { + return null; + } + + // Icon is relative resource + $iconResource = ModuleResourceLoader::singleton()->resolveResource($icon); + if ($iconResource instanceof ModuleResource) { + return $iconResource->getURL(); + } + + // Full path to file + if (Director::fileExists($icon)) { + return ModuleResourceLoader::resourceURL($icon); + } + + // Skip invalid files + return null; + } + /** * Get description for this page type * diff --git a/tests/php/Controllers/LeftAndMainPageIconsExtensionTest.php b/tests/php/Controllers/LeftAndMainPageIconsExtensionTest.php new file mode 100644 index 00000000..eb57dff7 --- /dev/null +++ b/tests/php/Controllers/LeftAndMainPageIconsExtensionTest.php @@ -0,0 +1,30 @@ +generatePageIconsCss(); + $this->assertNotContains('some invalid string', $css); + $this->assertContains( + 'tests/php/Controllers/LeftAndMainPageIconsExtensionTest/icon_b.jpg?m=', + $css + ); + $this->assertContains( + 'tests/php/Controllers/LeftAndMainPageIconsExtensionTest/icon_c.jpg?m=', + $css + ); + } +} diff --git a/tests/php/Controllers/LeftAndMainPageIconsExtensionTest/ModuleIconA.php b/tests/php/Controllers/LeftAndMainPageIconsExtensionTest/ModuleIconA.php new file mode 100644 index 00000000..3b503a34 --- /dev/null +++ b/tests/php/Controllers/LeftAndMainPageIconsExtensionTest/ModuleIconA.php @@ -0,0 +1,11 @@ + $path, + ]; + } +} diff --git a/tests/php/Controllers/LeftAndMainPageIconsExtensionTest/icon_b.jpg b/tests/php/Controllers/LeftAndMainPageIconsExtensionTest/icon_b.jpg new file mode 100644 index 0000000000000000000000000000000000000000..beb5a91b0aba93d3b54dadc12cff0104307d718e GIT binary patch literal 2292 zcmbV~c|6qJ9>;%UHjExS$z>*csbTDTlq4fO5~U_1OJm>1u4{=xiK_^q9w8LDu4RZU z8557($~v-CGowgKGnQ!>GxN;U>viux_m6w;=e)k(*IB;jygu)9j^K@e2FN&K9k2ih z1OOmm0|b2FS6eqbn=>wMhfslj2ScL#15xo9v>{3_E+#l45ar-#k8(U^XOD_EGun3m zMU3+e3yw`f9Y$H2N($xxTR;pXk|>Lb1ObaO6aoQ5U{DzBN5Cb-VQ>jJ3?_~cmyi@m z*pos?N{I%dLw;@*gMz_ONjMDtqvU@|f@grNIA8=Afk7I8m@Eh^3ldNOq!1GN!=8}z zr$Az02owev+CT^kNHRkEV6f0MROnb(og!QZAhJ-oy%-yqysIxV&@@|zq+B&*=`}UidnweWz9W@cqyyPi`}c)O^$RrQ0K+Pa62>RVddNbMa@I=jdNl;?v( ze+<7EpO~DYPQRI%r85>jd|X`mwEUU1w!X2+{?6Ij7IA?9@J}pZ{*PR;LM}1U8^A?e zAh85tfMp@jy%?CBjVs)jD6e6ZF0OFwR%LU)gr>0@Q}JT-n52@H30<2dLi<7X-+^WP zUu6FR`#0AtAO!{q9}g@GSOYtk7S!~}8_$uB+tPfA6|UScm#}l9_X_1u&tL^TQrcdj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TJ0MLk F^Cu<`6}$id literal 0 HcmV?d00001 diff --git a/tests/php/Controllers/LeftAndMainPageIconsExtensionTest/icon_c.jpg b/tests/php/Controllers/LeftAndMainPageIconsExtensionTest/icon_c.jpg new file mode 100644 index 0000000000000000000000000000000000000000..beb5a91b0aba93d3b54dadc12cff0104307d718e GIT binary patch literal 2292 zcmbV~c|6qJ9>;%UHjExS$z>*csbTDTlq4fO5~U_1OJm>1u4{=xiK_^q9w8LDu4RZU z8557($~v-CGowgKGnQ!>GxN;U>viux_m6w;=e)k(*IB;jygu)9j^K@e2FN&K9k2ih z1OOmm0|b2FS6eqbn=>wMhfslj2ScL#15xo9v>{3_E+#l45ar-#k8(U^XOD_EGun3m zMU3+e3yw`f9Y$H2N($xxTR;pXk|>Lb1ObaO6aoQ5U{DzBN5Cb-VQ>jJ3?_~cmyi@m z*pos?N{I%dLw;@*gMz_ONjMDtqvU@|f@grNIA8=Afk7I8m@Eh^3ldNOq!1GN!=8}z zr$Az02owev+CT^kNHRkEV6f0MROnb(og!QZAhJ-oy%-yqysIxV&@@|zq+B&*=`}UidnweWz9W@cqyyPi`}c)O^$RrQ0K+Pa62>RVddNbMa@I=jdNl;?v( ze+<7EpO~DYPQRI%r85>jd|X`mwEUU1w!X2+{?6Ij7IA?9@J}pZ{*PR;LM}1U8^A?e zAh85tfMp@jy%?CBjVs)jD6e6ZF0OFwR%LU)gr>0@Q}JT-n52@H30<2dLi<7X-+^WP zUu6FR`#0AtAO!{q9}g@GSOYtk7S!~}8_$uB+tPfA6|UScm#}l9_X_1u&tL^TQrcdj z>+Q>0tL3s|#Fe0ldmx9gHPI4kJ)~!b1g|z(yjN8XLXuJS` zbkD^*z5G3tsmzY%^l%+H!&dUleDZ1USK^@~9a{$<->ldybWF6F1{a6<+N$d#8AAgp zF*E@{=}DkRUa!e;;(^bU4KCsXbS(Fn>-RVSUwuF-0BsG#=yT*B;m`NAB6s9h`d>5U z*+I!zwFZ}WM9j}@ilS9JY$m9{% zsipE$tY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