Merge pull request #1996 from open-sausages/pulls/4.0/fix-page-icons

BUG Fix page icons in vendor modules
This commit is contained in:
Chris Joe 2017-10-19 16:19:46 +13:00 committed by GitHub
commit b22ea39ffd
10 changed files with 132 additions and 31 deletions

View File

@ -24,6 +24,7 @@ use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\Core\Environment; use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\ModuleResource;
use SilverStripe\Core\Manifest\ModuleResourceLoader; use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Forms\DateField; use SilverStripe\Forms\DateField;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
@ -1056,20 +1057,17 @@ class CMSMain extends LeftAndMain implements CurrentPageIdentifier, PermissionPr
} }
// skip this type if it is restricted // 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; continue;
} }
$singularName = $instance->i18n_singular_name();
$description = $instance->i18n_classDescription();
$result->push(new ArrayData(array( $result->push(new ArrayData(array(
'ClassName' => $class, 'ClassName' => $class,
'AddAction' => $singularName, 'AddAction' => $instance->i18n_singular_name(),
'Description' => $description, 'Description' => $instance->i18n_classDescription(),
// TODO Sprite support 'IconURL' => $instance->getPageIconURL(),
'IconURL' => ModuleResourceLoader::resourceURL($instance->config()->get('icon')), 'Title' => $instance->i18n_singular_name(),
'Title' => $singularName,
))); )));
} }

View File

@ -3,11 +3,11 @@
namespace SilverStripe\CMS\Controllers; namespace SilverStripe\CMS\Controllers;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Core\Convert;
use SilverStripe\View\Requirements;
use SilverStripe\Core\ClassInfo; use SilverStripe\Core\ClassInfo;
use SilverStripe\Control\Director; use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Extension; use SilverStripe\Core\Extension;
use SilverStripe\View\Requirements;
/** /**
* Extension to include custom page icons * Extension to include custom page icons
@ -32,28 +32,16 @@ class LeftAndMainPageIconsExtension extends Extension
$classes = ClassInfo::subclassesFor(SiteTree::class); $classes = ClassInfo::subclassesFor(SiteTree::class);
foreach ($classes as $class) { foreach ($classes as $class) {
$obj = singleton($class); $icon = Config::inst()->get($class, 'icon');
$iconSpec = $obj->config()->get('icon'); if (!$icon) {
if (!$iconSpec) {
continue; continue;
} }
// Legacy support: We no longer need separate icon definitions for folders etc. $cssClass = Convert::raw2htmlid($class);
$iconFile = (is_array($iconSpec)) ? $iconSpec[0] : $iconSpec; $selector = ".page-icon.class-$cssClass, li.class-$cssClass > a .jstree-pageicon";
$iconURL = SiteTree::singleton($class)->getPageIconURL();
// Legacy support: Add file extension if none exists if ($iconURL) {
if (!pathinfo($iconFile, PATHINFO_EXTENSION)) { $css .= "$selector { background: transparent url('$iconURL') 0 0 no-repeat; }\n";
$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";
} }
} }

View File

@ -17,6 +17,8 @@ use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Convert; use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\ModuleResource;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Core\Resettable; use SilverStripe\Core\Resettable;
use SilverStripe\Dev\Deprecation; use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\CheckboxField; use SilverStripe\Forms\CheckboxField;
@ -2841,6 +2843,33 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
return parent::plural_name(); 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 * Get description for this page type
* *

View File

@ -0,0 +1,30 @@
<?php
namespace SilverStripe\CMS\Tests\Controllers;
use SilverStripe\CMS\Controllers\LeftAndMainPageIconsExtension;
use SilverStripe\Dev\SapphireTest;
class LeftAndMainPageIconsExtensionTest extends SapphireTest
{
protected static $extra_dataobjects = [
LeftAndMainPageIconsExtensionTest\ModuleIconA::class,
LeftAndMainPageIconsExtensionTest\ModuleIconB::class,
LeftAndMainPageIconsExtensionTest\ModuleIconC::class,
];
public function testGenerateIconCSS()
{
$extension = new LeftAndMainPageIconsExtension();
$css = $extension->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
);
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace SilverStripe\CMS\Tests\Controllers\LeftAndMainpageIconsExtensionTest;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Dev\TestOnly;
class ModuleIconA extends SiteTree implements TestOnly
{
private static $icon = 'some invalid string';
}

View File

@ -0,0 +1,11 @@
<?php
namespace SilverStripe\CMS\Tests\Controllers\LeftAndMainpageIconsExtensionTest;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Dev\TestOnly;
class ModuleIconB extends SiteTree implements TestOnly
{
private static $icon = 'silverstripe/cms:tests/php/Controllers/LeftAndMainPageIconsExtensionTest/icon_b.jpg';
}

View File

@ -0,0 +1,13 @@
<?php
namespace SilverStripe\CMS\Tests\Controllers\LeftAndMainpageIconsExtensionTest;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Dev\TestOnly;
class ModuleIconC extends SiteTree implements TestOnly
{
private static $extensions = [
ModuleIconExtension::class,
];
}

View File

@ -0,0 +1,21 @@
<?php
namespace SilverStripe\CMS\Tests\Controllers\LeftAndMainpageIconsExtensionTest;
use SilverStripe\CMS\Model\SiteTreeExtension;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Dev\TestOnly;
class ModuleIconExtension extends SiteTreeExtension implements TestOnly
{
public static function get_extra_config()
{
// Mock a "fixed" path, but use a non-fixed resource url
$path = ModuleResourceLoader::resourcePath(
'silverstripe/cms:tests/php/Controllers/LeftAndMainPageIconsExtensionTest/icon_c.jpg'
);
return [
'icon' => $path,
];
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB