Problem/Motivation

The [img] shortcode takes a src parameter which can contain either an absolute or a relative URL. This can cause an issues in the following circumstances:

  • The site is moved to a different directory.
  • The image URL is changed (image updated).

In addition, we are unable to take advantage of image styles.

Proposed resolution

I propose adding the following parameters:

  • mid - Generate the absolute file URL based on the supplied media id and use this URL in the src attribute of the img tag.
  • imagestyle - Add the ability to supply an image style. If both mid and imagestyle are supplied, the URL of the appropriate style will be set as the src attribute of the img tag.

Remaining tasks

Test/Review.

User interface changes

At present, I don't think there are any user interface implications.

API changes

N/A.

Data model changes

N/A.

Comments

sthomps5 created an issue. See original summary.

sthomps5’s picture

sthomps5’s picture

Title: Add extra attrbutes to the [img] shortcode. Proposed: mid (media id) and style » Add fid (file id) and style attributes to the [img] shortcode
Issue summary: View changes
sthomps5’s picture

sthomps5’s picture

Status: Active » Needs review
sthomps5’s picture

Issue summary: View changes
sthomps5’s picture

Title: Add fid (file id) and style attributes to the [img] shortcode » Add mid (media id) and style attributes to the [img] shortcode
Issue summary: View changes

mid (media id) is a more appropriate attribute name than file id (fid). It is more convenient to retrieve the media id within Drupal.

sthomps5’s picture

Created a new patch after realising that media id was being used to create a file entity. We now convert the media id to a file entity before proceeding to retrieve URLs.

sthomps5’s picture

Noting here that the media library has a required field of 'alt' attribute. If pulling a file in using mid, it makes sense to use the alt attribute of the media file itself if an attribute isn't supplied as an override.

sthomps5’s picture

Testing reveals that setting an image style causes an error. This issue is resolved in this patch.

sthomps5’s picture

Code refactor, and logic that pulls in the alt attribute if the media id is supplied and no alt attribute is given in the shortcode.

This enables us to write shorter shortcodes such as:

[img mid="1" style="medium" /]

...and still have the alt attribute added as this is a required field on the media object with an id of 1.

sthomps5’s picture

Renaming the style attribute to imagestyle to avoid confusion with the style attribute used in other shortcodes that add inline style.

sthomps5’s picture

Issue summary: View changes
sthomps5’s picture

Title: Add mid (media id) and style attributes to the [img] shortcode » Add mid (media id) and imagestyle attributes to the [img] shortcode

  • sthomps5 committed 83df21d on 8.x-1.x
    Issue #2966395 by sthomps5: Add mid (media id) and imagestyle attributes...
sthomps5’s picture

Status: Needs review » Fixed
sthomps5’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.