You are here

public function Update400::convertMediaReferenceFieldsToMediaLibrary in Lightning Media 8.4

Optionally changes entity browser field widgets to media library widgets.

@update

Parameters

\Symfony\Component\Console\Style\StyleInterface $io: The I/O style.

File

src/Update/Update400.php, line 59

Class

Update400
Contains optional updates targeting Lightning Media 4.0.0.

Namespace

Drupal\lightning_media\Update

Code

public function convertMediaReferenceFieldsToMediaLibrary(StyleInterface $io) {
  $storage = $this->entityTypeManager
    ->getStorage('entity_form_display');

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
  foreach ($storage
    ->loadMultiple() as $display) {
    $changed = FALSE;
    foreach ($display
      ->getComponents() as $name => $component) {
      if ($component['type'] === 'entity_browser_entity_reference' && $component['settings']['entity_browser'] === 'media_browser') {
        $question = $this
          ->getQuestionForMediaReferenceField($display, $name);
        if ($io
          ->confirm($question)) {
          $display
            ->setComponent($name, [
            'type' => 'media_library_widget',
          ]);
          $changed = TRUE;
        }
      }
      if ($changed) {
        $storage
          ->save($display);
      }
    }
  }
}