You are here

function lightning_media_image_entity_form_display_presave in Lightning Media 8.3

Same name and namespace in other branches
  1. 8.4 modules/lightning_media_image/lightning_media_image.module \lightning_media_image_entity_form_display_presave()
  2. 8 modules/lightning_media_image/lightning_media_image.module \lightning_media_image_entity_form_display_presave()
  3. 8.2 modules/lightning_media_image/lightning_media_image.module \lightning_media_image_entity_form_display_presave()

Implements hook_ENTITY_TYPE_presave().

File

modules/lightning_media_image/lightning_media_image.module, line 149
Support for image media assets in Lightning.

Code

function lightning_media_image_entity_form_display_presave(EntityFormDisplayInterface $display) {

  // Don't do anything during config sync.
  if (\Drupal::isConfigSyncing()) {
    return;
  }

  // Since the image browser integrates with the media library, it doesn't make
  // sense to use the image browser when creating or editing an image media
  // entity.
  if ($display
    ->getTargetEntityTypeId() == 'media' && $display
    ->getTargetBundle() == 'image') {
    return;
  }
  $filter = function (FieldStorageDefinitionInterface $field) {
    return $field
      ->getType() == 'image';
  };
  $new_components = \Drupal::service('lightning.display_helper')
    ->getNewFields($display, $filter);
  foreach ($new_components as $key => $component) {
    $display
      ->setComponent($key, [
      'type' => 'entity_browser_file',
      'weight' => $component['weight'],
      'settings' => [
        'entity_browser' => 'image_browser',
        'field_widget_edit' => TRUE,
        'field_widget_remove' => TRUE,
        'view_mode' => 'default',
        'preview_image_style' => 'thumbnail',
        'open' => TRUE,
      ],
      'region' => 'content',
    ]);
  }
}