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

Try to figure out which is the right LinkDestination value for an image #17401

Closed
wants to merge 3 commits into from

Conversation

jesusangel
Copy link

I want to always link the images in my posts to its source url. This way, users can click on the thumbnail and see the image full size. In Gutenberg's image block, the default setting is not to link the images, so I have to manually change the LinkDestination select for each image I insert in the posts.

Users can change the default value of LinkDestination with a plugin and some JavaScript like this:

   function modifyImageLinkDestinationDefault( settings, name ) {

       // Override core default value for LinkDestination and change it to media 
       if ( name == "core/image" ) {
            // Set image block default destination.
            settings.attributes.linkDestination.default = "media";
        }

        return settings;
    }

    // Add settings filter to block registration.
    wp.hooks.addFilter(
        "blocks.registerBlockType",
        "my-plugin/modify-image-link-destination-default",
        modifyImageLinkDestinationDefault
    );

The actual code of the Gutenberg's image block doesn't work well when it finds old posts with no LinkDestination attribute, as no attribute means default value, but the new default value isn't the same as the old one.

Description
I've added code to the componentDidMount method of the image block to figure out which is the right value for the LinkDestination attribute based on the innerHTML. This work is based on @SeanDS pull request #13797 and closes issue #13749.

How has this been tested?
I used the latest development release of Gutenberg (cloned at 313974f), and WordPress 5.1, using docker images. I used the plugin showed above to change the default link target to media. I added several images with different LinkDestination values and I edited again after disabling the plugin.

This change doesn't affect other areas of the code.

Types of changes
Bug fix (non-breaking change which fixes an issue)

Checklist:
[ X] My code is tested.
[ X] My code follows the WordPress code style.
[ X] My code follows the accessibility standards.
[ X] My code has proper inline documentation.
[ X] I've included developer documentation if appropriate.

@jesusangel jesusangel changed the title Fix/image block Try to figure out which is the right LinkDestination value for an image Sep 10, 2019
@jesusangel
Copy link
Author

After rebasing my changes on the latest commits I did a mistake while resolving conflicts. I need more time to test again my changes.

@vdrover
Copy link

vdrover commented Oct 13, 2019

Any progress?

@jesusangel
Copy link
Author

I'm sorry @vdrover, but I've been very busy. I'll try to fix the errors next week.

@bibiwan
Copy link

bibiwan commented Nov 9, 2019

I'm sorry @vdrover, but I've been very busy. I'll try to fix the errors next week.

Hi @jesusangel, when do you think you'll be able to fix the errors ? I'm very interested in this PR.

@SeanDS
Copy link
Contributor

SeanDS commented Dec 24, 2019

@jesusangel is the only thing stopping this from being reviewed the CI errors, or is there something else left over from the old PR that needs fixed in the code?

when the user has changed its default value. I change the
LinkDestination value so it matches the innerHTML:
<img ... /> => none
<a href="source_url"><img ... /></a> => media
<a href="attachment_url"><img ... /></a> => attachment
<a href="another_url"><img ... /> => custom
@thanh25896
Copy link

Why I always got "Parse error: syntax error, unexpected ',', expecting variable (T_VARIABLE)" when added your code to function.php?

@Soean
Copy link
Member

Soean commented Mar 25, 2020

Why I always got "Parse error: syntax error, unexpected ',', expecting variable (T_VARIABLE)" when added your code to function.php?

It's not PHP code, you have to put it inside a JavaScript (.js) file.

@noisysocks
Copy link
Member

Somewhat related to this is #10173 which is that the image and gallery blocks should respect the image_default_link_type filter in WordPress.

@vigilantspb
Copy link

// Add settings filter to block registration.
wp.hooks.addFilter(
    "blocks.registerBlockType",
    "my-plugin/modify-image-link-destination-default",
    modifyImageLinkDestinationDefault
);

excuse me, where do I have to put this code?

@jesusangel jesusangel requested a review from ajlende as a code owner October 1, 2020 09:40
@ajlende
Copy link
Contributor

ajlende commented Oct 7, 2020

This seems to be resolved by #25578.

The default setting can now be changed by editing the image_default_link_type setting in options.php to 'post' (for LINK_DESTINATION_MEDIA), 'file' (for LINK_DESTINATION_ATTACHMENT), or 'none' (for LINK_DESTINATION_NONE).

There is an unfortunate mismtatch between what Gutenberg thinks the option should be and what the historic option values are. The classic editor and previous use file to represent Media File, and post to represent "Attachment Page". The Gutenberg constants used are media and attachment.

@ajlende ajlende closed this Oct 7, 2020
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

Successfully merging this pull request may close these issues.

9 participants