Problem/Motivation

Follow-up #2904514: Crop API causes an error when adding a media bundle This issue exist to port #2904514: Crop API causes an error when adding a media bundle changes and purpose a cleanup of specific code for media_entity.

Some part of code need to be cleaned...

Proposed resolution

Purpose a patch for 1.x branch to maintain compatibility with 8.4 and usage of media entity AND media (core).

Purpose another clean more brutal for media entity to prepare transition with full media (core) usage.

Comments

woprrr created an issue. See original summary.

woprrr’s picture

Status: Active » Needs review
StatusFileSize
new6.74 KB
new5.44 KB

Here two patches, for 1.x branch (Rétro compatible with Media Entity) and 2.x fully compatible with Media.

The last submitted patch, 3: cleanup_of_code-1-x-2918441-2.patch, failed testing. View results

phenaproxima’s picture

Title: Cleanup of code specific media entity for media » Make Crop API compatible with core Media
Status: Needs review » Needs work
Issue tags: +Media Initiative

I reviewed the 1.x patch and found several issues:

  1. +++ b/crop.module
    @@ -38,22 +40,45 @@ function template_preprocess_crop_crop_summary(&$variables) {
    +function crop_form_media_type_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    

    &$form should be type hinted as an array.

  2. +++ b/crop.module
    @@ -38,22 +40,45 @@ function template_preprocess_crop_crop_summary(&$variables) {
    + * Adds crop configuration fields to media bundle form.
    

    Should be "media type form".

  3. +++ b/crop.module
    @@ -38,22 +40,45 @@ function template_preprocess_crop_crop_summary(&$variables) {
    +function _crop_media_provider_form(&$form, FormStateInterface $form_state) {
    

    &$form should have the array type hint.

  4. +++ b/crop.module
    @@ -38,22 +40,45 @@ function template_preprocess_crop_crop_summary(&$variables) {
    +  } else {
    +    $form['#entity_builders'][] = 'crop_media_bundle_form_builder';
    +  }
    

    Nit: The else { should be on a new line.

  5. +++ b/crop.module
    @@ -94,6 +120,17 @@ function crop_media_bundle_form_builder($entity_type, MediaBundleInterface $bund
    +function crop_media_type_form_builder($entity_type, MediaTypeInterface $bundle, &$form, FormStateInterface $form_state) {
    

    &$form should be type hinted as an array.

  6. +++ b/src/Plugin/Crop/EntityProvider/Media.php
    @@ -1,23 +1,24 @@
    + *   description = @Translation("Provides crop integration for media.")
    

    s/media/Media

  7. +++ b/src/Plugin/Crop/EntityProvider/Media.php
    @@ -59,8 +60,16 @@ class MediaEntity extends EntityProviderBase implements ContainerFactoryPluginIn
    +      $bundle = $this->entityTypeManager->getStorage('media_type')->load($entity->bundle());
    

    Wait, what? Config entities don't have bundles. Why are we loading $entity->bundle() if $entity is a media type?

  8. +++ b/src/Plugin/Crop/EntityProvider/Media.php
    @@ -59,8 +60,16 @@ class MediaEntity extends EntityProviderBase implements ContainerFactoryPluginIn
    +    } else {
    

    else { should be on its own line.

woprrr’s picture

Status: Needs work » Needs review
StatusFileSize
new11.01 KB
new8.06 KB

Here you are 1.x fixes and some coding standards fixes by the way...

Status: Needs review » Needs work

The last submitted patch, 6: cleanup_of_code-1-x-2918441-6.patch, failed testing. View results

woprrr’s picture

Status: Needs work » Needs review
+++ b/src/Plugin/Crop/EntityProvider/Media.php
@@ -59,9 +59,12 @@ class MediaEntity extends EntityProviderBase implements ContainerFactoryPluginIn
-    /** @var \Drupal\media_entity\MediaBundleInterface $bundle */
-    $bundle = $this->entityTypeManager->getStorage('media_bundle')->load($entity->bundle());
-    $image_field = $bundle->getThirdPartySetting('crop', 'image_field');
+
+    $bundle_entity_type = $entity->getEntityType()->getBundleEntityType();
+    /** @var \Drupal\Core\Config\Entity\ConfigEntityBase $entity_type */
+    $entity_type = $this->entityTypeManager->getStorage($bundle_entity_type)->load($entity->bundle());
+
+    $image_field = $entity_type->getThirdPartySetting('crop', 'image_field');

This way is better and reduce general complexity.

As discussed with @slashrsm

In that provider the idea was that you could have crops that are tied to different entities. This way you could say for node N crop like this and for node M like this but then we realized that the image style system can't do that so it was a good idea I guess but can't be used right now.

I maintain that provider and permit this method can retrieve $entity_type from ContentEntityType. We can't retrieve directly the Entity Type loaded directly we need to use EntityManager to do that.

woprrr’s picture

Typo/Nit/coding standards fixes backported onto 2-x patch and change backported for \Drupal\crop\Plugin\Crop\EntityProvider\Media::uri

To test providers here small code to past in devel console :

$plugin_manager = \Drupal::service('plugin.manager.crop.entity_provider');
$plugin_media = $plugin_manager->createInstance('media');
$media_entity = \Drupal::entityTypeManager()->getStorage('media')->load(1);
dpm($plugin_media->uri($media_entity));
woprrr’s picture

StatusFileSize
new10.37 KB
new688 bytes

Backport of https://www.drupal.org/node/2808719#comment-12314321 change for 2.x branch to assume empty value for crop configuration form.

woprrr’s picture

Re-roll of patch for 1.x branch and small addition to display element in correct form element group.
Small addition of form element group for 2.x patch.

Everything look's good now :).

Edit : @phenaproxima : I have juste one doubt about move submodule into crop basis for Media cropProvider "modules/crop_media_entity/src/Plugin/Crop/EntityProvider/MediaEntity.php" If users have this module enabled we need to add update_n to uninstall "crop_media_entity" on update no ?

Status: Needs review » Needs work

The last submitted patch, 11: cleanup_of_code-1-x-2918441-11.patch, failed testing. View results

woprrr’s picture

Status: Needs work » Needs review
woprrr’s picture

Re-roll patches head of last release (1.x) / beta-1 (2.x)

phenaproxima’s picture

+++ b/crop.module
@@ -40,23 +42,52 @@ function template_preprocess_crop_crop_summary(&$variables) {
+  if ($entity_type instanceof MediaType) {

I don't think you can directly use Media's classes unless you have a hard dependency on core Media...no?

woprrr’s picture

@phenaproxima :O I'm surprised I didn't see this change in patches ! This look like a custom check I have added to debug in my test install but does not appear here :O We are right this doesn't applied at all But with patches apply I didn't see

/**
 * Prepares variables for crop_crop summary template.
 *
 * Default template: crop-crop-summary.twig.html.
 */
function template_preprocess_crop_crop_summary(&$variables) {
  if (!empty($variables['data']['crop_type'])) {
    $type = \Drupal::entityTypeManager()->getStorage('crop_type')->load($variables['data']['crop_type']);
    $variables['data']['crop_type'] = $type->label();
  }
}
woprrr’s picture

This check only exist on _crop_media_provider_form() function and that's only fired if we use Media entity of Media in core Both module implement same form_alter() and we need to be sure what entity builder you should process. If current media are MediaType then First condition are OK else we are in Media entity context.

This form_alter does not create a strong dependency to media entity / media core but this is only a way to avoid code duplications in 1.x branch. We need to permit using Media Entity Or Media core.

Perhaps this opportunity to bring both lives to you and you prefer a more radical approach? In this case the addition of a conflict in the composer.json would not hurt to empower users to the fact that 1.x === Media entity 2.x === Media in core?

balsama’s picture

StatusFileSize
new10.91 KB

The patch in #14 won't apply to the D.O packaged version of 8.x-2.x-beta1 because it patches the crop.info.yml file, and file differs in the packaged version since D.O adds some stuff. So if you want to use the patch in a project, you need to HEAD of 8.x-2.x which isn't modified by D.O.

Here's a patch that's identical to #14, but assumes the info file has been modified by D.O.

This is not relevant to the actual issue at hand. Do not test.

woprrr’s picture

Status: Needs review » Reviewed & tested by the community

Hi @balsama,

Now Lightning seems using this patch during 6 days without problems :) That's sound good to this patch to be merged in 2.x !

The patch in #14 won't apply to the D.O packaged version of 8.x-2.x-beta1 because it patches the crop.info.yml file, and file differs in the packaged version since D.O adds some stuff. So if you want to use the patch in a project, you need to HEAD of 8.x-2.x which isn't modified by D.O.

As we have discussed on Slack, new Image Widget Crop requirements does work to use dev branch and apply patch normally now.

I switch that RTBC if anyone have objections :)

  • woprrr committed bb8caee on 8.x-1.x
    Issue #2918441 by woprrr, balsama, phenaproxima: Make Crop API...

  • woprrr committed d8066e1 on 8.x-2.x
    Issue #2918441 by woprrr, balsama, phenaproxima: Make Crop API...
woprrr’s picture

Status: Reviewed & tested by the community » Fixed

Merged Thanks all :) good job @balsama I will create PR onto Lightning as we have seen in slack.

abaier’s picture

Since crop_media_entity was removed from the modules here, we got an update issue and get warnings:

Dependency issue when upgrading from 1.3.0 to 1.4.0

lukus’s picture

I'm seeing the same warning as @ABaier.

Status: Fixed » Closed (fixed)

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