Template-icon within modx.panel.resource.js

Hello everybody!

I’ts a nice feature to aply custom icons to the resource templates but IMHO confusing to show them in the resource tree. Should be good to have them somewhere in the resource update form.

First I took the "MODXROOT/core/model/modx/processors/resourcegetnodes.class.php"
and skipped the line
// Check for an icon class on the resource template
$tplIcon = $resource->Template ? $resource->Template->icon : ‘’;
My result: In the resource tree only the standard icons are displayed - fine for me!

Now my problem:
I found in "MODXROOT/manager/assets/modext/widgets/resource/modx.panel.resource.js"
arround line #608 the following section:

,getMainRightFields: function(config) {
config = config || {};
return [{
xtype: ‘modx-combo-template’
,fieldLabel: _(‘resource_template’)

This section is responsible for displaying the combobox with all available templates
in the resource update panel.

I found that the pointer “config.record.template” returns the ID of the current selected template.
… and now I stuck! Is there a way in this panel environment to get the icon class
(and may be other details) of the selected template by it’s ID somehow??

Thank you for your quick reply - and have a nice day!
Greatings from Dresden (Saxonia) from “ejomi”

you shouldn’t modify core-files. The changes are overridden with the next upgrade.
Maybe, you could solve it by creating a custom Resource Class

https://docs.modx.com/current/en/extending-modx/custom-resources

The value for the icon refers to a CSS class. E.g: icon icon-home

Yes, you’r absolute right. I’m talking about my local installed (mostly offline) version for my personal usage where there is no need for updates and disturbing stuff. The template icons are very helpful for me to overview my several project drafts - that’s the reason.
However: Creating a new Resource Class seems too much.

Is there may be a global available “variable-class-array-construct-whatever-thing” which I can just call by ID??

I don’t have much skills with client script JSON code - perefer clear structured server CGI/PHP instead, where globals are very usefull sometimes. The JS-code instead looks very strange to me with all it’s nested and totally overloaded parameters - puh! So, is there a kind of “global” which I can easily use?

Thanks in advance - yours: ejomi

You could add the template-icon to config.record by adding a value to $this->resourceArray in the file manager/controllers/default/resource/update.class.php. Something like:

$tpl = $this->modx->getObject('modTemplate', $this->resourceArray['template']);
if ($tpl) {
  $this->resourceArray['template_icon'] = $tpl->get('icon');
}

Then you could use this value in modx.panel.resource.js for example by adding this to the return-array in the getMainRightFields-function.

{
  id: 'modx-resource-template-icon'
  ,html: '<i class="icon ' + config.record.template_icon + '"></i>'
  ,border: false
}, ...

YES - this is exactly what I was looking for! This soloution doesn’t scramble the core files to much and can easily be reproduced in possible MODX updates in the future.

Below my reworked code for the “update.class.php” which considers also none defined icons and unused templates:

// Catch the template object:
$tplobj = $this->modx->getObject('modTemplate', $this->resourceArray['template']);
if ($tplobj) {
  $tplico = trim($tplobj->get('icon'));
    if ($tplico) {
    // Custom icon found - get it:
    $this->resourceArray['template_icon'] = $tplico;
  } else {
    // No icon defined - assign the default icon:
    $this->resourceArray['template_icon'] = 'icon-columns';
  }
} else {
  // No template in use - assign the "empty page" icon:
  $this->resourceArray['template_icon'] = 'icon-file-o';
}

Special feature:
For having the total freedom of displaying custom icons or standard icons in the resource tree, you may add a new switch named “force_default_icons” to the system settings. Then you must adapt the “MODXROOT/core/model/modx/processors/resource/getnodes.class.php”. Find the comment “// Check for an icon class …” arround line #424 (talking about MODx version 2.7.2) and replace the following single code line there with this little code block below. (Note: My little extension on the standard behavior considers also none defined icons and unused templates!)

if ($this->modx->getOption('force_default_icons') && $this->modx->getOption('force_default_icons') == true) {
  // Special system switch found (= show always the default tree icons of MODX and nothing else):
  $tplIcon = "";
} else {
  // Standard behavior (extended):
  // - If no template resource exists, there was no template defined for this page (= show a special icon for "no template"):
  $tplIcon = $resource->Template ? $resource->Template->icon : 'icon-file-o';
  // - If still no icon assigned, a template resource is in use but no custom icon was defined (= show a special default icon for general templates):
  if ($tplIcon == "") $tplIcon = 'icon-columns';
}

Have fun!

I marked ths thread as “solved” - many thanks for your help!

Greetings from Dresden (Saxonia) where it is very sweltering in theese days (abbout 35°C = ca. 95°F, which is very unusual for middle Germany at this time of year)

  • yours: ejomi

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.