Issue #2143Opened July 16, 2019by chauhankiran0 reactions

Question - How can I create an existing image as link

Question

I have an existing template that is currently displaying the image in the middle of the page. Now, I want to make it link or clickable so that if the user clicks on the image it should open given href. How can do that? Currently, we have link block and inside of it, we can place an image. But, I'm asking for the reverse - Existing image to link.

Answers (3)

miky9585July 24, 20190 reactions
Code SnippetTEXT
I've solved in this way:
on backend script:

	var originalImage = editor.DomComponents.getType('image');
	editor.DomComponents.addType('image', {
		model: originalImage.model.extend({
			defaults: Object.assign({},
	            originalImage.model.prototype.defaults, { 
	                traits:[{
		                	type: "text",
		                	label: "ID",
		                	name: "id"
	                	},
	                	{
		                	type: "text",
		                	label: "URL",
		                	name: "data-img-url"	
	                	},
	                	{
		                	type: 'select',
				            label: 'Target',
				            name: 'data-img-target',
				            value: "_top",
				            options: [
				              {value: '_top', name: 'Same Page'},
				              {value: '_blank', name: 'New Page'}, 
				            ]	
	                	}
	                ]
	            }), 
	        
			
		},
		{
			isComponent: function(el) {
				if("IMG"==el.tagName) return {
                    type: "image"
                }

			},
		}),
		view: originalImage.view
	});

When show page (I've stored on db)

	
	if( $('[data-img-url]').length > 0 ){  
		$.each($('[data-img-url]'), function(i,e){ 
			if( typeof $(e).data('img-url') != 'undefined' && $(e).data('img-url').length > 0){
				var href = $(e).data('img-url'), target = $(e).data('img-target') || '_top';
				var a = $('<a/>').attr('href', href).attr('target', target);
				$(e).wrap(a);
				$(e).removeAttr('data-img-url').removeAttr('data-img-target'); // KEEP CODE CLEAN
			} 
		})
	}
miky9585July 24, 20190 reactions
Code SnippetTEXT
SEO Friendly backend (added Alt attr)

	var originalImage = editor.DomComponents.getType('image');
	editor.DomComponents.addType('image', {
		model: originalImage.model.extend({
			defaults: Object.assign({},
	            originalImage.model.prototype.defaults, { 
	                traits:[{
		                	type: "text",
		                	label: "ID",
		                	name: "id"
	                	},
	                	{
		                	type: "text",
		                	label: "URL",
		                	name: "data-img-url"	
	                	},
	                	{
		                	type: "text",
		                	label: "Alternative Text",
		                	name: "alt"	
	                	},
	                	{
		                	type: 'select',
				            label: 'Target',
				            name: 'data-img-target',
				            value: "_top",
				            options: [
				              {value: '_top', name: 'This window'},
				              {value: '_blank', name: 'New window'}, 
				            ]	
	                	}
	                ]
	            }), 
	        
			
		},
		{
			isComponent: function(el) {
				if("IMG"==el.tagName) return {
                    type: "image"
                }

			},
		}),
		view: originalImage.view
	});
artfJuly 31, 20190 reactions

@chauhankiran the correct step would be to create a custom component, kind of link-image which would replace the image one. The root of this component will be the <a> element and the inner one will be the image.

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.