Issue #2527Opened January 24, 2020by csb3463 reactions

Custom components based on Link block with a default href

Question

Hi! I have a GrapesJS integration on a laravel project with custom components directly on the page (each block is supposed to be block link to a content). So far I replicated the block link component like that

@foreach($contents as $content)
	editor.BlockManager.add('content_{{ $content->id }}', {
		category: 'Contents',
		label: '<b>{{ $content->title }}</b>',
		attributes: { class:'fa fa-graduation-cap' },
		content: {
			type: 'link',
			editable: false,
			droppable: true,
			style:{
				display: 'inline-block',
				padding: '5px',
				'min-height': '50px',
				'min-width': '50px'
			},
			content: '{{ $content->title }}',
		},
	});
	@endforeach

So far so good. But I need that each of these components has his own href defined by default. No matter if it's editable or not. Something like this:

href: 'http://url.com/content/{{ $content->id }}',

Can't find the solution. Any idea?

Thanks

Answers (1)

pouyamiralayiJanuary 25, 20203 reactions

@csb346 you can define a new custom component type:

editor.DomComponents.addType('content-link', {
            extend: 'link',
            model: {
                defaults: {
	            editable: false,
	            droppable: true,
                    style: {
                        display: 'inline-block',
                        padding: '5px',
                        'min-height': '50px',
                        'min-width': '50px'
                    },
                    attributes:{href:'http://google.com'}
                    content: '{{ $content->title }}',
                }
            }
        });

and then use it inside a block:

editor.BlockManager.add('content-link', {
    label:'Content Link',
    category:'Contents',
    attributes: { class:'fa fa-graduation-cap' },
    content:{
        type:'content-link'
    }
})

just remember you must define your custom component type inside a plugin Cheers!

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.