You are here

lightning_media_image.install in Lightning Media 8.4

Contains install and update routines for Lightning Media Image.

File

modules/lightning_media_image/lightning_media_image.install
View source
<?php

/**
 * @file
 * Contains install and update routines for Lightning Media Image.
 */
use Drupal\lightning_core\ConfigHelper as Config;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

/**
 * Implements hook_install().
 */
function lightning_media_image_install() {

  // Don't do anything during config sync.
  if (\Drupal::isConfigSyncing()) {
    return;
  }
  $module_exists = [
    \Drupal::moduleHandler(),
    'moduleExists',
  ];

  // Grants image browser access to the creator content role and the
  // media_creator and media_manager roles.
  if ($module_exists('lightning_roles')) {
    lightning_media_image_modules_installed([
      'lightning_roles',
    ]);
  }
  if ($module_exists('image_widget_crop')) {

    // Use the cropping widgets for every form display of the Image media type.
    $form_displays = \Drupal::entityTypeManager()
      ->getStorage('entity_form_display')
      ->loadByProperties([
      'targetEntityType' => 'media',
      'bundle' => 'image',
    ]);

    /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
    foreach ($form_displays as $form_display) {
      $component = $form_display
        ->getComponent('field_media_image');
      if ($component && $component['type'] == 'image_image') {
        $component['type'] = 'image_widget_crop';
        $component['settings']['crop_list'] = [
          'freeform',
        ];
        $component['settings']['show_crop_area'] = TRUE;
        $form_display
          ->setComponent('field_media_image', $component)
          ->save();
      }
    }

    // Try to use a local copy of Cropper over the CDN-hosted fallback.
    $cropper_path = Drupal::service('library.libraries_directory_file_finder')
      ->find('cropper/dist');
    if ($cropper_path) {
      Drupal::configFactory()
        ->getEditable('image_widget_crop.settings')
        ->set('settings.library_url', "{$cropper_path}/cropper.min.js")
        ->set('settings.css_url', "{$cropper_path}/cropper.min.css")
        ->save();
    }
  }
}

/**
 * Creates the media_browser form display.
 */
function lightning_media_image_update_8001() {
  Config::forModule('lightning_media_image')
    ->optional()
    ->getEntity('entity_form_display', 'media.image.media_browser')
    ->save();
}

/**
 * Installs the image_browser entity browser.
 */
function lightning_media_image_update_8002() {

  // A widget validation service was added to Entity Browser after alpha6, and
  // it will almost certainly be instantiated by the widget plugins during the
  // creation of a new entity browser.
  try {
    \Drupal::service('plugin.manager.entity_browser.widget_validation');
  } catch (ServiceNotFoundException $e) {

    // Rebuild the container to ensure the widget validation service exists.
    lightning_core_rebuild_container();
  }
  Config::forModule('lightning_media_image')
    ->optional()
    ->getEntity('entity_browser', 'image_browser')
    ->save();
}

/**
 * Removed in Lightning 8.x-2.21.
 *
 * Formerly created the image browser display of the media view.
 */
function lightning_media_image_update_8003() {
}

/**
 * Removed in Lightning 8.x-2.10.
 *
 * Formerly added the 'access image_browser entity browser pages' permission to
 * the media_creator and media_manager roles, as well as the creator content
 * role.
 */
function lightning_media_image_update_8004() {
}

/**
 * Removes the file link(s) and Remove button from the media_browser form.
 */
function lightning_media_image_update_8005() {
  $display = Drupal::service('entity_display.repository')
    ->getFormDisplay('media', 'image', 'media_browser');
  $component = $display
    ->getComponent('image');
  if ($component && $component['type'] == 'image_image') {
    $component['third_party_settings']['lightning_media'] = [
      'file_links' => FALSE,
      'remove_button' => FALSE,
    ];
    $display
      ->setComponent('image', $component)
      ->save();
  }
}

/**
 * Creates the thumbnail display for images.
 */
function lightning_media_image_update_8006() {
  Config::forModule('lightning_media_image')
    ->optional()
    ->getEntity('entity_view_display', 'media.image.thumbnail')
    ->save();
}

/**
 * Creates the media_library form display for images.
 */
function lightning_media_image_update_8007() {
  Config::forModule('lightning_media_image')
    ->optional()
    ->getEntity('entity_form_display', 'media.image.media_library')
    ->save();
}

/**
 * Implements hook_update_dependencies().
 */
function lightning_media_image_update_dependencies() {
  return [
    'lightning_media_image' => [
      8003 => [
        // 8003 modifies the image_browser entity browser, which is created by
        // Lightning Media Image 8002.
        'lightning_media_image' => 8002,
      ],
      // 8006 depends on the thumbnail view mode, which is created by
      // lightning_media 8015.
      8006 => [
        'lightning_media' => 8015,
      ],
    ],
  ];
}

Functions

Namesort descending Description
lightning_media_image_install Implements hook_install().
lightning_media_image_update_8001 Creates the media_browser form display.
lightning_media_image_update_8002 Installs the image_browser entity browser.
lightning_media_image_update_8003 Removed in Lightning 8.x-2.21.
lightning_media_image_update_8004 Removed in Lightning 8.x-2.10.
lightning_media_image_update_8005 Removes the file link(s) and Remove button from the media_browser form.
lightning_media_image_update_8006 Creates the thumbnail display for images.
lightning_media_image_update_8007 Creates the media_library form display for images.
lightning_media_image_update_dependencies Implements hook_update_dependencies().