Verified Commit 9ea78bec authored by godotislate's avatar godotislate
Browse files

refactor: #3570839 Deprecate remaining underscore functions in media_library.module

By: nicxvan
By: phenaproxima
By: dcam
By: berdir
By: godotislate
parent 842c8541
Loading
Loading
Loading
Loading
Loading
+0 −6
Changes for core/.phpstan-baseline.php: 0 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -19549,12 +19549,6 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/media/tests/src/Unit/ResourceTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Function _media_library_views_form_media_library_after_build\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/media_library/media_library.module',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\media_library\\\\Ajax\\\\UpdateSelectionCommand\\:\\:render\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
+3 −2
Changes for core/modules/media_library/media_library.install: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
 */

use Drupal\media\Entity\MediaType;
use Drupal\media_library\MediaLibraryDisplayManager;

/**
 * Implements hook_install().
@@ -13,8 +14,8 @@
function media_library_install($is_syncing): void {
  if (!$is_syncing) {
    foreach (MediaType::loadMultiple() as $type) {
      _media_library_configure_form_display($type);
      _media_library_configure_view_display($type);
      MediaLibraryDisplayManager::configureFormDisplay($type);
      MediaLibraryDisplayManager::configureViewDisplay($type);
    }
  }
}
+0 −141
Changes for core/modules/media_library/media_library.module: 0 added lines, 141 removed lines.
Original line number Diff line number Diff line
<?php

/**
 * @file
 */

use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Form\FormStateInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
use Drupal\media\MediaTypeInterface;

/**
 * Form #after_build callback for media_library view's exposed filters form.
 */
function _media_library_views_form_media_library_after_build(array $form, FormStateInterface $form_state) {
  // Remove .form-actions from the view's exposed filter actions. This prevents
  // the "Apply filters" submit button from being moved into the dialog's
  // button area.
  // @see \Drupal\Core\Render\Element\Actions::processActions
  // @see Drupal.behaviors.dialog.prepareDialogButtons
  // @todo Remove this after
  //   https://www.drupal.org/project/drupal/issues/3089751 is fixed.
  if (($key = array_search('form-actions', $form['actions']['#attributes']['class'])) !== FALSE) {
    unset($form['actions']['#attributes']['class'][$key]);
  }
  return $form;
}

/**
 * Submit callback for media type form.
 */
function _media_library_media_type_form_submit(array &$form, FormStateInterface $form_state): void {
  $form_object = $form_state->getFormObject();
  if ($form_object->getOperation() === 'add') {
    $type = $form_object->getEntity();
    $form_display_created = _media_library_configure_form_display($type);
    $view_display_created = _media_library_configure_view_display($type);
    if ($form_display_created || $view_display_created) {
      \Drupal::messenger()->addStatus(t('Media Library form and view displays have been created for the %type media type.', [
        '%type' => $type->label(),
      ]));
    }
  }
}

/**
 * Ensures that the given media type has a media_library form display.
 *
 * @param \Drupal\media\MediaTypeInterface $type
 *   The media type to configure.
 *
 * @return bool
 *   Whether a form display has been created or not.
 *
 * @throws \Drupal\Core\Entity\EntityStorageException
 */
function _media_library_configure_form_display(MediaTypeInterface $type) {
  $display = EntityFormDisplay::load('media.' . $type->id() . '.media_library');

  if ($display) {
    return FALSE;
  }

  $values = [
    'targetEntityType' => 'media',
    'bundle' => $type->id(),
    'mode' => 'media_library',
    'status' => TRUE,
  ];
  $display = EntityFormDisplay::create($values);
  // Remove all default components.
  foreach (array_keys($display->getComponents()) as $name) {
    $display->removeComponent($name);
  }
  // Expose the name field when it is not mapped.
  if (!in_array('name', $type->getFieldMap(), TRUE)) {
    $display->setComponent('name', [
      'type' => 'string_textfield',
      'settings' => [
        'size' => 60,
      ],
    ]);
  }
  // If the source field is an image field, expose it so that users can set alt
  // and title text.
  $source_field = $type->getSource()->getSourceFieldDefinition($type);
  if ($source_field->isDisplayConfigurable('form') && is_a($source_field->getItemDefinition()->getClass(), ImageItem::class, TRUE)) {
    $type->getSource()->prepareFormDisplay($type, $display);
  }
  return (bool) $display->save();
}

/**
 * Ensures that the given media type has a media_library view display.
 *
 * @param \Drupal\media\MediaTypeInterface $type
 *   The media type to configure.
 *
 * @return bool
 *   Whether a view display has been created or not.
 *
 * @throws \Drupal\Core\Entity\EntityStorageException
 */
function _media_library_configure_view_display(MediaTypeInterface $type) {
  $display = EntityViewDisplay::load('media.' . $type->id() . '.media_library');

  if ($display) {
    return FALSE;
  }

  $values = [
    'targetEntityType' => 'media',
    'bundle' => $type->id(),
    'mode' => 'media_library',
    'status' => TRUE,
  ];
  $display = EntityViewDisplay::create($values);
  // Remove all default components.
  foreach (array_keys($display->getComponents()) as $name) {
    $display->removeComponent($name);
  }

  // @todo Remove dependency on 'medium' and 'thumbnail' image styles from
  //   media and media library modules.
  //   https://www.drupal.org/project/drupal/issues/3030437
  $image_style = ImageStyle::load('medium');

  // Expose the thumbnail component. If the medium image style doesn't exist,
  // use the fallback 'media_library' image style.
  $display->setComponent('thumbnail', [
    'type' => 'image',
    'label' => 'hidden',
    'settings' => [
      'image_style' => $image_style ? $image_style->id() : 'media_library',
      'image_link' => '',
    ],
  ]);
  return (bool) $display->save();
}
+42 −3
Changes for core/modules/media_library/src/Hook/MediaLibraryHooks.php: 42 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Render\Element;
use Drupal\media_library\MediaLibraryDisplayManager;

/**
 * Hook implementations for media_library.
@@ -183,14 +184,14 @@ public function formAlter(array &$form, FormStateInterface $form_state, $form_id
    // Add a process callback to ensure that the media library view's exposed
    // filters submit button is not moved to the modal dialog's button area.
    if ($form_id === 'views_exposed_form' && str_starts_with($form['#id'], 'views-exposed-form-media-library-widget')) {
      $form['#after_build'][] = '_media_library_views_form_media_library_after_build';
      $form['#after_build'][] = static::class . ':viewsFormAfterBuild';
    }
    // Configures media_library displays when a type is submitted.
    if ($form_state->getFormObject() instanceof MediaTypeForm) {
      $form['actions']['submit']['#submit'][] = '_media_library_media_type_form_submit';
      $form['actions']['submit']['#submit'][] = static::class . ':mediaTypeFormSubmit';
      // @see field_ui_form_alter()
      if (isset($form['actions']['save_continue'])) {
        $form['actions']['save_continue']['#submit'][] = '_media_library_media_type_form_submit';
        $form['actions']['save_continue']['#submit'][] = static::class . ':mediaTypeFormSubmit';
      }
    }
  }
@@ -262,4 +263,42 @@ public function imageStyleAccess(EntityInterface $entity, $operation, AccountInt
    return AccessResult::neutral();
  }

  /**
   * Form #after_build callback for media_library view's exposed filters form.
   *
   * @internal
   */
  public function viewsFormAfterBuild(array $form, FormStateInterface $form_state): array {
    // Remove .form-actions from the view's exposed filter actions. This
    // prevents the "Apply filters" submit button from being moved into the
    // dialog's button area.
    // @see \Drupal\Core\Render\Element\Actions::processActions
    // @see Drupal.behaviors.dialog.prepareDialogButtons
    // @todo Remove this after
    //   https://www.drupal.org/project/drupal/issues/3089751 is fixed.
    if (($key = array_search('form-actions', $form['actions']['#attributes']['class'])) !== FALSE) {
      unset($form['actions']['#attributes']['class'][$key]);
    }
    return $form;
  }

  /**
   * Submit callback for media type form.
   *
   * @internal
   */
  public function mediaTypeFormSubmit(array &$form, FormStateInterface $form_state): void {
    $form_object = $form_state->getFormObject();
    if ($form_object->getOperation() === 'add') {
      $type = $form_object->getEntity();
      $form_display_created = MediaLibraryDisplayManager::configureFormDisplay($type);
      $view_display_created = MediaLibraryDisplayManager::configureViewDisplay($type);
      if ($form_display_created || $view_display_created) {
        \Drupal::messenger()->addStatus($this->t('Media Library form and view displays have been created for the %type media type.', [
          '%type' => $type->label(),
        ]));
      }
    }
  }

}
+115 −0
Changes for core/modules/media_library/src/MediaLibraryDisplayManager.php: 115 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\media_library;

use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
use Drupal\media\MediaTypeInterface;

/**
 * The media library form and view display setup.
 *
 * @internal
 */
class MediaLibraryDisplayManager {

  /**
   * Ensures that the given media type has a media_library form display.
   *
   * @param \Drupal\media\MediaTypeInterface $type
   *   The media type to configure.
   *
   * @return bool
   *   Whether a form display has been created or not.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public static function configureFormDisplay(MediaTypeInterface $type): bool {
    $display = EntityFormDisplay::load('media.' . $type->id() . '.media_library');

    if ($display) {
      return FALSE;
    }

    $values = [
      'targetEntityType' => 'media',
      'bundle' => $type->id(),
      'mode' => 'media_library',
      'status' => TRUE,
    ];
    $display = EntityFormDisplay::create($values);
    // Remove all default components.
    foreach (array_keys($display->getComponents()) as $name) {
      $display->removeComponent($name);
    }
    // Expose the name field when it is not mapped.
    if (!in_array('name', $type->getFieldMap(), TRUE)) {
      $display->setComponent('name', [
        'type' => 'string_textfield',
        'settings' => [
          'size' => 60,
        ],
      ]);
    }
    // If the source field is an image field, expose it so that users can set
    // alt and title text.
    $source_field = $type->getSource()->getSourceFieldDefinition($type);
    if ($source_field->isDisplayConfigurable('form') && is_a($source_field->getItemDefinition()->getClass(), ImageItem::class, TRUE)) {
      $type->getSource()->prepareFormDisplay($type, $display);
    }
    return (bool) $display->save();
  }

  /**
   * Ensures that the given media type has a media_library view display.
   *
   * @param \Drupal\media\MediaTypeInterface $type
   *   The media type to configure.
   *
   * @return bool
   *   Whether a view display has been created or not.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public static function configureViewDisplay(MediaTypeInterface $type): bool {
    $display = EntityViewDisplay::load('media.' . $type->id() . '.media_library');

    if ($display) {
      return FALSE;
    }

    $values = [
      'targetEntityType' => 'media',
      'bundle' => $type->id(),
      'mode' => 'media_library',
      'status' => TRUE,
    ];
    $display = EntityViewDisplay::create($values);
    // Remove all default components.
    foreach (array_keys($display->getComponents()) as $name) {
      $display->removeComponent($name);
    }

    // @todo Remove dependency on 'medium' and 'thumbnail' image styles from
    //   media and media library modules.
    //   https://www.drupal.org/project/drupal/issues/3030437
    $image_style = ImageStyle::load('medium');

    // Expose the thumbnail component. If the medium image style doesn't exist,
    // use the fallback 'media_library' image style.
    $display->setComponent('thumbnail', [
      'type' => 'image',
      'label' => 'hidden',
      'settings' => [
        'image_style' => $image_style ? $image_style->id() : 'media_library',
        'image_link' => '',
      ],
    ]);
    return (bool) $display->save();
  }

}