Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to add video_template_callback function code? #368

Open
twkrol opened this issue Aug 24, 2021 · 1 comment
Open

Is it possible to add video_template_callback function code? #368

twkrol opened this issue Aug 24, 2021 · 1 comment

Comments

@twkrol
Copy link

twkrol commented Aug 24, 2021

Hi
I need to remove the default width & height media attributes (they're added automatically when inserting video), so found the only way by overwrite the video_template_callback.
But I can't find a way to provide video_template_callback function, neither via TINYMCE_DEFAULT_CONFIG or mce_attrs.

Any idea how can I change media redering function?

@some1ataplace
Copy link

To remove the default width and height media attributes when inserting a video using django-tinymce, you can create a video_template_callback function in your Django project's settings.py file. Here is an example implementation:

# settings.py
TINYMCE_DEFAULT_CONFIG = {
    'plugins': 'media',
    'media_dimensions': False,
    'video_template_callback': 'myapp.tinymce_video_template_callback',
}

def tinymce_video_template_callback(file, options):
    """
    Custom video template callback to remove default width and height attributes.
    """
    attrs = ''
    for key, value in options.items():
        if key == 'src':
            value = file.url
        attrs += ' {}="{}"'.format(key, value)
    return '<video{}></video>'.format(attrs)

In this example, we set the media_dimensions option to False to disable the default width and height attributes for all media types. Then, we define a custom tinymce_video_template_callback function that generates the HTML for the video element without including the width and height attributes.

Note that you will need to replace myapp with the name of your Django app in the video_template_callback option.


Remove the default width and height attributes from the iframe tag when inserting a video in django-tinymce editor:

from tinymce import HTMLField
from django import forms

class CustomTinyMCEWidget(forms.Textarea):
    def init(self, args, **kwargs):
        super(CustomTinyMCEWidget, self).init(args, **kwargs)
        self.attrs.update({'class': 'tinymce'})

class VideoForm(forms.ModelForm):
    description = HTMLField(widget=CustomTinyMCEWidget())

    class Meta:
        model = Video
        fields = ('title', 'embed_code','description')

class VideoInline(admin.TabularInline):
    model = Video
    fields = ('title', 'embed_code', 'description')
    form = VideoForm
    extra = 0

    def video_template_callback(self, data):
        """
        Remove default media attributes from iframe tag
        """
        template = Template('<iframe src="{{src}}" width="{{width}}" height="{{height}}" frameborder="0" allowfullscreen></iframe>')
        width = data['width'].replace('px', '').strip()
        height = data['height'].replace('px', '').strip()
        src = data['src']
        return template.render(Context({'src': src, 'width': width, 'height': height}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants