Problem/Motivation

The `isApplicable()` method in the `LiteYoutubeFormatter` class loads the entire `MediaType` entity solely to check the source. This approach may be inefficient and could be optimized to improve performance.

Steps to reproduce

  1. Analyze the `isApplicable()` method in the `LiteYoutubeFormatter` class.
  2. Observe that the `MediaType::load()` method is used to fetch the full `MediaType` entity.
  3. Note that only the source property of the `MediaType` is used.

Proposed resolution

  • Refactor the `isApplicable()` method to avoid loading the full `MediaType` entity.
  • Fetch only the required data or use a more lightweight approach to determine the media type's source.

Remaining tasks

  • Investigate alternate ways to retrieve the media source without loading the full entity. Refactor the `isApplicable()` method.
  • Test the refactored code to ensure the behavior remains consistent.

User interface changes

None expected, as this change pertains to backend performance optimization.

API changes

None expected.

Data model changes

None.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

apereira23 created an issue. See original summary.

ruby232’s picture

Assigned: Unassigned » pponcedeleona

pponcedeleona made their first commit to this issue’s fork.

jleon1110’s picture

After reviewing this issue and analyzing the implementation of the isApplicable() method, I noticed that there is a redundant block of code that performs an entity query to check if the media type exists:

$source_plugin_id = $media_type_storage
  ->getQuery()
  ->condition('id', $media_type_id)
  ->accessCheck(FALSE)
  ->execute();

if (empty($source_plugin_id)) {
  return FALSE;
}

I think this part of the code is unnecessary, as the media type is already validated later using \Drupal::config('media.type.' . $media_type_id). Additionally, the result of this query is not used anywhere else in the method.

jleon1110’s picture

When this block is remove:

$source_plugin_id = $media_type_storage
  ->getQuery()
  ->condition('id', $media_type_id)
  ->accessCheck(FALSE)
  ->execute();

if (empty($source_plugin_id)) {
  return FALSE;
}

I think the solution works fine and can be merged.

jleon1110’s picture

The block was removed I think this solution can by merged.

ruby232’s picture

Status: Active » Fixed
ruby232’s picture

Status: Fixed » Closed (fixed)