You are here

public function Image::prepareMediaSourceFieldInstanceRow in Media Migration 8

Overrides MediaDealerBase::prepareMediaSourceFieldInstanceRow

File

src/Plugin/media_migration/file_entity/Image.php, line 130

Class

Image
Image media migration plugin for local image media entities.

Namespace

Drupal\media_migration\Plugin\media_migration\file_entity

Code

public function prepareMediaSourceFieldInstanceRow(Row $row, Connection $connection) : void {
  parent::prepareMediaSourceFieldInstanceRow($row, $connection);

  // Alt and title properties of an image field always exist, regardless
  // of whether they are accessible and editable on the entity edit form or
  // not.
  // But we also want to make sure that if either the alt or title property
  // was editable on the source site, then it will be editable also on the
  // destination site.
  // For first, we try to determine which properties should be enabled (and
  // thus accessible and editable on UI) based on the source media image
  // entities' alt and title fields' content. We assume that if there is at
  // least a single value for an image's title property in the source
  // database, then we have to change visibility of the title property to
  // TRUE, so that it can be edited on the destination site as well.
  $alt_title_config = $this
    ->getImageAltTitleSettingsFromPropertyFieldContent($connection);

  // All image field content is also migrated into a media entity. If any of
  // the alt or title properties shouldn't be necessarily shown (and be
  // editable) on the entity edit form based on the source media entity's
  // alt or title field value, we still have to check the configuration of
  // the source site's image fields.
  // If any of the preexisting image field was configured to show the alt or
  // the title property, then we will make their input field visible.
  $props = array_map(function (string $property) {
    return "{$property}_field";
  }, array_keys(static::PROPERTY_FIELD_NAME_MAP));
  if (!empty(array_diff($props, array_keys($alt_title_config)))) {
    $alt_title_config += $this
      ->getSettingsFromImageFields($connection);
  }

  // Get the 'required' settings from the image fields we found.
  $this
    ->mergePropertyRequiredSettingsFromImageFields($alt_title_config, $connection);

  // Add the discovered and the default configuration.
  $additional_properties = $alt_title_config + [
    'alt_field' => TRUE,
    'alt_field_required' => TRUE,
    'title_field' => FALSE,
    'title_field_required' => FALSE,
  ];
  $settings = $additional_properties + ($row
    ->getSourceProperty('settings') ?? []);
  $row
    ->setSourceProperty('settings', $settings);
}