Issue #2527✓ SolvedOpened January 24, 2020by csb3463 reactions

Custom components based on Link block with a default href

快速解答by pouyamiralayi3

@csb346 you can define a new custom component type: and then use it inside a block: just remember you must define your custom component type inside a plugin Cheers!

Read full answer below ↓

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)

👍 Most helpfulpouyamiralayiJanuary 25, 2020

@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...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.

Browse premium plugins →

Related tutorials

In-depth guides on the same topic.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.