diff --git a/packages/block-library/src/image/block.json b/packages/block-library/src/image/block.json
index decd621688cea1..798210cbd5c2f8 100644
--- a/packages/block-library/src/image/block.json
+++ b/packages/block-library/src/image/block.json
@@ -60,8 +60,7 @@
"type": "string"
},
"linkDestination": {
- "type": "string",
- "default": "none"
+ "type": "string"
},
"linkTarget": {
"type": "string",
diff --git a/packages/block-library/src/image/deprecated.js b/packages/block-library/src/image/deprecated.js
index 08f43101618e2a..0a3c2142636c65 100644
--- a/packages/block-library/src/image/deprecated.js
+++ b/packages/block-library/src/image/deprecated.js
@@ -59,7 +59,6 @@ const blockAttributes = {
},
linkDestination: {
type: 'string',
- default: 'none',
},
linkTarget: {
type: 'string',
diff --git a/packages/block-library/src/image/edit.js b/packages/block-library/src/image/edit.js
index 50181e67ee83f3..5d5478b4db3394 100644
--- a/packages/block-library/src/image/edit.js
+++ b/packages/block-library/src/image/edit.js
@@ -21,6 +21,8 @@ import { useEffect, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { image as icon } from '@wordpress/icons';
+/* global wp */
+
/**
* Internal dependencies
*/
@@ -30,8 +32,10 @@ import Image from './image';
* Module constants
*/
import {
- LINK_DESTINATION_MEDIA,
LINK_DESTINATION_ATTACHMENT,
+ LINK_DESTINATION_CUSTOM,
+ LINK_DESTINATION_MEDIA,
+ LINK_DESTINATION_NONE,
ALLOWED_MEDIA_TYPES,
DEFAULT_SIZE_SLUG,
} from './constants';
@@ -83,7 +87,6 @@ export function ImageEdit( {
caption,
align,
id,
- linkDestination,
width,
height,
sizeSlug,
@@ -152,21 +155,49 @@ export function ImageEdit( {
additionalAttributes = { url };
}
- // Check if the image is linked to it's media.
- if ( linkDestination === LINK_DESTINATION_MEDIA ) {
- // Update the media link.
- mediaAttributes.href = media.url;
+ // Check if default link setting should be used.
+ let linkDestination = attributes.linkDestination;
+ if ( ! linkDestination ) {
+ // Use the WordPress option to determine the proper default.
+ // The constants used in Gutenberg do not match WP options so a little more complicated than ideal.
+ // TODO: fix this in a follow up PR, requires updating media-text and ui component.
+ switch (
+ wp?.media?.view?.settings?.defaultProps?.link ||
+ LINK_DESTINATION_NONE
+ ) {
+ case 'file':
+ case LINK_DESTINATION_MEDIA:
+ linkDestination = LINK_DESTINATION_MEDIA;
+ break;
+ case 'post':
+ case LINK_DESTINATION_ATTACHMENT:
+ linkDestination = LINK_DESTINATION_ATTACHMENT;
+ break;
+ case LINK_DESTINATION_CUSTOM:
+ linkDestination = LINK_DESTINATION_CUSTOM;
+ break;
+ case LINK_DESTINATION_NONE:
+ linkDestination = LINK_DESTINATION_NONE;
+ break;
+ }
}
- // Check if the image is linked to the attachment page.
- if ( linkDestination === LINK_DESTINATION_ATTACHMENT ) {
- // Update the media link.
- mediaAttributes.href = media.link;
+ // Check if the image is linked to it's media.
+ let href;
+ switch ( linkDestination ) {
+ case LINK_DESTINATION_MEDIA:
+ href = media.url;
+ break;
+ case LINK_DESTINATION_ATTACHMENT:
+ href = media.link;
+ break;
}
+ mediaAttributes.href = href;
setAttributes( {
...mediaAttributes,
...additionalAttributes,
+ linkDestination,
} );
}
diff --git a/packages/e2e-tests/fixtures/blocks/core__image.json b/packages/e2e-tests/fixtures/blocks/core__image.json
index 22c56bcf681315..1650156649dadf 100644
--- a/packages/e2e-tests/fixtures/blocks/core__image.json
+++ b/packages/e2e-tests/fixtures/blocks/core__image.json
@@ -6,8 +6,7 @@
"attributes": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
"alt": "",
- "caption": "",
- "linkDestination": "none"
+ "caption": ""
},
"innerBlocks": [],
"originalContent": "