TinyMCE / Plone 6 Classic default upload folder

Is there any 'changes' with newest Plone / TinyMCE ? I would like to set default upload folder for images (when added from TinyMCE ), or even better: Have different upload folders depending on Content Type

Here is example from one of my projects:

from Acquisition import aq_get
from plone.base.interfaces import IPatternsSettings
from plone.uuid.interfaces import IUUID
from Products.CMFPlone.patterns.settings import PatternSettingsAdapter
from Products.CMFPlone.utils import get_portal
from zope.interface import implementer

import json

class CustomPatternSettingsAdapter(PatternSettingsAdapter):

    def tinymce(self):
        """Use a folder 'bilder' as initial target to upload and select images (and links)."""
        orig_config = super().tinymce()
        images_folder = aq_get(self.context, "bilder", None)
        if not images_folder or images_folder.portal_type != "Folder":
            return orig_config

        config = json.loads(orig_config["data-pat-tinymce"])
        images_path = "/".join(images_folder.getPhysicalPath())
        images_uuid = IUUID(images_folder)
        images_favorite = {"path": images_path, "title": "Bilder"}
        portal = get_portal()
        portal_url = portal.absolute_url()
        images_current_path = images_folder.absolute_url()[len(portal_url) :]

        # We need to set all three for upload to work properly.
        config["upload"]["currentPath"] = images_current_path
        config["relatedItems"]["basePath"] = images_path
        config["upload"]["initialFolder"] = images_uuid
        if "favorites" in config["relatedItems"]:
            config["relatedItems"]["favorites"].insert(0, images_favorite)
        return {"data-pat-tinymce": json.dumps(config)}

Registered like this:

  <!-- Extend TinyCME settings to include image folders (old and new name) -->
      for="* bgp.bgnpolicy.interfaces.IBgnPolicyLayer *"
      for="* bgp.bgnpolicy.interfaces.IBgnPolicyLayer *"

This works in production with Plone 6.0.10

1 Like

Thanks a lot (did you translate 'Bilder' to norwegian (for me) or is it danish ? :slight_smile:

It's german :slight_smile: