Pexels image media doesn't have a mapped name/description

Created on 17 November 2022, almost 2 years ago
Updated 19 July 2023, about 1 year ago

Problem/Motivation

When adding an image from Pexels to the external media, its name is the same as the file name.
When using the solution proposed below, it solves the situation for Unsplash, but in Pexels it still does not work.

Steps to reproduce

  • Step 1: Create external media entity with Unsplash media source.
  • Step 2: Add this media type as an entity reference to any content type.
  • Step 3: Add a media on the content type and search for an image on Pexels via the media library interface.
  • Step4: Select one image from the search results & click import.
  • Step 5: Check the name in the media library interface after the import, the name field will be populated with file name.

Proposed resolution

After investigating a bit, I realized that this is due to the fact that the Pexels API doesn't have a unique field for "description" like Unsplash does.
This way the source media in Pexels is always returning an empty string for the name field. Referenced Code

Taking this into account, my solution to solve this problem is to use the "alt" field as a name/description and only if it is not available, leave an empty string.

return new ExternalMedia(
  $data['id'],
  $data['src']['original'],
  $data['src']['original'] . '?w=800&h=600&fit=crop',
  (-) '',
  (+) $data['alt'] ?? '',
  $data['alt'] ?? '',
  $data['photographer'] ?? '',
  $data['photographer_url'] ?? '',
);

Note: for this solution to work it is necessary to implement the correction of issue: 🐛 External Media title field is mapped to the file url Closed: won't fix

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇵🇹Portugal lolgm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024