You are here

lightning_media_image.module in Lightning Media 8

Support for image media assets in Lightning.

File

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

/**
 * @file
 * Support for image media assets in Lightning.
 */
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_browser\Entity\EntityBrowser;
use Drupal\image\Entity\ImageStyle;
use Drupal\lightning_core\ConfigHelper as Config;
use Drupal\lightning_core\OverrideHelper as Override;
use Drupal\lightning_media_image\Plugin\media\Source\Image;
use Drupal\views\ViewEntityInterface;

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function lightning_media_image_view_insert(ViewEntityInterface $view) {
  if (\Drupal::isConfigSyncing()) {
    return;
  }
  elseif (drupal_installation_attempted() && $view
    ->id() == 'media') {
    $display_id = $view
      ->addDisplay('entity_browser', 'Image Browser');
    $display =& $view
      ->getDisplay($display_id);
    $display = array_merge($display, unserialize('a:2:{s:15:"display_options";a:12:{s:17:"display_extenders";a:0:{}s:5:"style";a:2:{s:4:"type";s:4:"grid";s:7:"options";a:8:{s:8:"grouping";a:0:{}s:7:"columns";i:4;s:15:"automatic_width";b:1;s:9:"alignment";s:10:"horizontal";s:17:"col_class_default";b:1;s:16:"col_class_custom";s:0:"";s:17:"row_class_default";b:1;s:16:"row_class_custom";s:0:"";}}s:8:"defaults";a:9:{s:5:"style";b:0;s:3:"row";b:0;s:6:"fields";b:0;s:7:"filters";b:0;s:13:"filter_groups";b:0;s:5:"empty";b:0;s:5:"pager";b:0;s:9:"css_class";b:0;s:13:"relationships";b:0;}s:3:"row";a:2:{s:4:"type";s:6:"fields";s:7:"options";a:0:{}}s:6:"fields";a:2:{s:20:"thumbnail__target_id";a:37:{s:2:"id";s:20:"thumbnail__target_id";s:5:"table";s:16:"media_field_data";s:5:"field";s:20:"thumbnail__target_id";s:12:"relationship";s:4:"none";s:10:"group_type";s:5:"group";s:11:"admin_label";s:0:"";s:5:"label";s:0:"";s:7:"exclude";b:0;s:5:"alter";a:26:{s:10:"alter_text";b:0;s:4:"text";s:0:"";s:9:"make_link";b:0;s:4:"path";s:0:"";s:8:"absolute";b:0;s:8:"external";b:0;s:14:"replace_spaces";b:0;s:9:"path_case";s:4:"none";s:15:"trim_whitespace";b:0;s:3:"alt";s:0:"";s:3:"rel";s:0:"";s:10:"link_class";s:0:"";s:6:"prefix";s:0:"";s:6:"suffix";s:0:"";s:6:"target";s:0:"";s:5:"nl2br";b:0;s:10:"max_length";i:0;s:13:"word_boundary";b:1;s:8:"ellipsis";b:1;s:9:"more_link";b:0;s:14:"more_link_text";s:0:"";s:14:"more_link_path";s:0:"";s:10:"strip_tags";b:0;s:4:"trim";b:0;s:13:"preserve_tags";s:0:"";s:4:"html";b:0;}s:12:"element_type";s:0:"";s:13:"element_class";s:0:"";s:18:"element_label_type";s:0:"";s:19:"element_label_class";s:0:"";s:19:"element_label_colon";b:1;s:20:"element_wrapper_type";s:0:"";s:21:"element_wrapper_class";s:0:"";s:23:"element_default_classes";b:1;s:5:"empty";s:0:"";s:10:"hide_empty";b:0;s:10:"empty_zero";b:0;s:16:"hide_alter_empty";b:1;s:17:"click_sort_column";s:9:"target_id";s:4:"type";s:5:"image";s:8:"settings";a:2:{s:11:"image_style";s:6:"medium";s:10:"image_link";s:0:"";}s:12:"group_column";s:0:"";s:13:"group_columns";a:0:{}s:10:"group_rows";b:1;s:11:"delta_limit";i:0;s:12:"delta_offset";i:0;s:14:"delta_reversed";b:0;s:16:"delta_first_last";b:0;s:10:"multi_type";s:9:"separator";s:9:"separator";s:2:", ";s:17:"field_api_classes";b:0;s:11:"entity_type";s:5:"media";s:12:"entity_field";s:9:"thumbnail";s:9:"plugin_id";s:5:"field";}s:21:"entity_browser_select";a:23:{s:2:"id";s:21:"entity_browser_select";s:5:"table";s:12:"file_managed";s:5:"field";s:21:"entity_browser_select";s:12:"relationship";s:15:"image_target_id";s:10:"group_type";s:5:"group";s:11:"admin_label";s:0:"";s:5:"label";s:0:"";s:7:"exclude";b:0;s:5:"alter";a:26:{s:10:"alter_text";b:0;s:4:"text";s:0:"";s:9:"make_link";b:0;s:4:"path";s:0:"";s:8:"absolute";b:0;s:8:"external";b:0;s:14:"replace_spaces";b:0;s:9:"path_case";s:4:"none";s:15:"trim_whitespace";b:0;s:3:"alt";s:0:"";s:3:"rel";s:0:"";s:10:"link_class";s:0:"";s:6:"prefix";s:0:"";s:6:"suffix";s:0:"";s:6:"target";s:0:"";s:5:"nl2br";b:0;s:10:"max_length";i:0;s:13:"word_boundary";b:1;s:8:"ellipsis";b:1;s:9:"more_link";b:0;s:14:"more_link_text";s:0:"";s:14:"more_link_path";s:0:"";s:10:"strip_tags";b:0;s:4:"trim";b:0;s:13:"preserve_tags";s:0:"";s:4:"html";b:0;}s:12:"element_type";s:0:"";s:13:"element_class";s:0:"";s:18:"element_label_type";s:0:"";s:19:"element_label_class";s:0:"";s:19:"element_label_colon";b:0;s:20:"element_wrapper_type";s:0:"";s:21:"element_wrapper_class";s:15:"visually-hidden";s:23:"element_default_classes";b:0;s:5:"empty";s:0:"";s:10:"hide_empty";b:0;s:10:"empty_zero";b:0;s:16:"hide_alter_empty";b:1;s:11:"entity_type";s:4:"file";s:9:"plugin_id";s:21:"entity_browser_select";}}s:7:"filters";a:4:{s:6:"status";a:16:{s:2:"id";s:6:"status";s:5:"table";s:16:"media_field_data";s:5:"field";s:6:"status";s:12:"relationship";s:4:"none";s:10:"group_type";s:5:"group";s:11:"admin_label";s:0:"";s:8:"operator";s:1:"=";s:5:"value";b:1;s:5:"group";i:1;s:7:"exposed";b:0;s:6:"expose";a:10:{s:11:"operator_id";s:0:"";s:5:"label";s:4:"True";s:11:"description";N;s:12:"use_operator";b:0;s:8:"operator";s:9:"status_op";s:10:"identifier";s:6:"status";s:8:"required";b:1;s:8:"remember";b:0;s:8:"multiple";b:0;s:14:"remember_roles";a:1:{s:13:"authenticated";s:13:"authenticated";}}s:10:"is_grouped";b:1;s:10:"group_info";a:10:{s:5:"label";s:17:"Publishing status";s:11:"description";s:0:"";s:10:"identifier";s:6:"status";s:8:"optional";b:1;s:6:"widget";s:6:"select";s:8:"multiple";b:0;s:8:"remember";b:0;s:13:"default_group";s:3:"All";s:22:"default_group_multiple";a:0:{}s:11:"group_items";a:2:{i:1;a:3:{s:5:"title";s:9:"Published";s:8:"operator";s:1:"=";s:5:"value";s:1:"1";}i:2;a:3:{s:5:"title";s:11:"Unpublished";s:8:"operator";s:1:"=";s:5:"value";s:1:"0";}}}s:9:"plugin_id";s:7:"boolean";s:11:"entity_type";s:5:"media";s:12:"entity_field";s:6:"status";}s:4:"name";a:16:{s:2:"id";s:4:"name";s:5:"table";s:16:"media_field_data";s:5:"field";s:4:"name";s:12:"relationship";s:4:"none";s:10:"group_type";s:5:"group";s:11:"admin_label";s:0:"";s:8:"operator";s:8:"contains";s:5:"value";s:0:"";s:5:"group";i:1;s:7:"exposed";b:1;s:6:"expose";a:10:{s:11:"operator_id";s:7:"name_op";s:5:"label";s:8:"Keywords";s:11:"description";s:0:"";s:12:"use_operator";b:0;s:8:"operator";s:7:"name_op";s:10:"identifier";s:8:"keywords";s:8:"required";b:0;s:8:"remember";b:0;s:8:"multiple";b:0;s:14:"remember_roles";a:10:{s:13:"authenticated";s:13:"authenticated";s:9:"anonymous";s:1:"0";s:13:"administrator";s:1:"0";s:12:"page_creator";s:1:"0";s:14:"layout_manager";s:1:"0";s:13:"page_reviewer";s:1:"0";s:20:"landing_page_creator";s:1:"0";s:21:"landing_page_reviewer";s:1:"0";s:13:"media_creator";s:1:"0";s:13:"media_manager";s:1:"0";}}s:10:"is_grouped";b:0;s:10:"group_info";a:10:{s:5:"label";s:0:"";s:11:"description";s:0:"";s:10:"identifier";s:0:"";s:8:"optional";b:1;s:6:"widget";s:6:"select";s:8:"multiple";b:0;s:8:"remember";b:0;s:13:"default_group";s:3:"All";s:22:"default_group_multiple";a:0:{}s:11:"group_items";a:0:{}}s:11:"entity_type";s:5:"media";s:12:"entity_field";s:4:"name";s:9:"plugin_id";s:6:"string";}s:6:"bundle";a:16:{s:2:"id";s:6:"bundle";s:5:"table";s:16:"media_field_data";s:5:"field";s:6:"bundle";s:12:"relationship";s:4:"none";s:10:"group_type";s:5:"group";s:11:"admin_label";s:0:"";s:8:"operator";s:2:"in";s:5:"value";a:1:{s:5:"image";s:5:"image";}s:5:"group";i:1;s:7:"exposed";b:0;s:6:"expose";a:11:{s:11:"operator_id";s:9:"bundle_op";s:5:"label";s:4:"Type";s:11:"description";s:0:"";s:12:"use_operator";b:0;s:8:"operator";s:9:"bundle_op";s:10:"identifier";s:4:"type";s:8:"required";b:0;s:8:"remember";b:0;s:8:"multiple";b:0;s:14:"remember_roles";a:10:{s:13:"authenticated";s:13:"authenticated";s:9:"anonymous";s:1:"0";s:13:"administrator";s:1:"0";s:12:"page_creator";s:1:"0";s:14:"layout_manager";s:1:"0";s:13:"page_reviewer";s:1:"0";s:20:"landing_page_creator";s:1:"0";s:21:"landing_page_reviewer";s:1:"0";s:13:"media_creator";s:1:"0";s:13:"media_manager";s:1:"0";}s:6:"reduce";b:0;}s:10:"is_grouped";b:0;s:10:"group_info";a:10:{s:5:"label";s:0:"";s:11:"description";s:0:"";s:10:"identifier";s:0:"";s:8:"optional";b:1;s:6:"widget";s:6:"select";s:8:"multiple";b:0;s:8:"remember";b:0;s:13:"default_group";s:3:"All";s:22:"default_group_multiple";a:0:{}s:11:"group_items";a:0:{}}s:11:"entity_type";s:5:"media";s:12:"entity_field";s:6:"bundle";s:9:"plugin_id";s:6:"bundle";}s:28:"field_media_in_library_value";a:14:{s:2:"id";s:28:"field_media_in_library_value";s:5:"table";s:29:"media__field_media_in_library";s:5:"field";s:28:"field_media_in_library_value";s:12:"relationship";s:4:"none";s:10:"group_type";s:5:"group";s:11:"admin_label";s:0:"";s:8:"operator";s:1:"=";s:5:"value";b:1;s:5:"group";i:1;s:7:"exposed";b:0;s:6:"expose";a:10:{s:11:"operator_id";s:0:"";s:5:"label";s:0:"";s:11:"description";s:0:"";s:12:"use_operator";b:0;s:8:"operator";s:0:"";s:10:"identifier";s:0:"";s:8:"required";b:0;s:8:"remember";b:0;s:8:"multiple";b:0;s:14:"remember_roles";a:1:{s:13:"authenticated";s:13:"authenticated";}}s:10:"is_grouped";b:0;s:10:"group_info";a:10:{s:5:"label";s:0:"";s:11:"description";s:0:"";s:10:"identifier";s:0:"";s:8:"optional";b:1;s:6:"widget";s:6:"select";s:8:"multiple";b:0;s:8:"remember";b:0;s:13:"default_group";s:3:"All";s:22:"default_group_multiple";a:0:{}s:11:"group_items";a:0:{}}s:9:"plugin_id";s:7:"boolean";}}s:13:"filter_groups";a:2:{s:8:"operator";s:3:"AND";s:6:"groups";a:1:{i:1;s:3:"AND";}}s:5:"empty";a:1:{s:16:"area_text_custom";a:10:{s:2:"id";s:16:"area_text_custom";s:5:"table";s:5:"views";s:5:"field";s:16:"area_text_custom";s:12:"relationship";s:4:"none";s:10:"group_type";s:5:"group";s:11:"admin_label";s:0:"";s:5:"empty";b:1;s:8:"tokenize";b:0;s:7:"content";s:31:"There are no images to display.";s:9:"plugin_id";s:11:"text_custom";}}s:5:"pager";a:2:{s:4:"type";s:15:"infinite_scroll";s:7:"options";a:7:{s:14:"items_per_page";i:12;s:6:"offset";i:0;s:2:"id";i:0;s:11:"total_pages";N;s:4:"tags";a:2:{s:8:"previous";s:12:"‹ Previous";s:4:"next";s:8:"Next ›";}s:6:"expose";a:7:{s:14:"items_per_page";b:0;s:20:"items_per_page_label";s:14:"Items per page";s:22:"items_per_page_options";s:13:"5, 10, 25, 50";s:26:"items_per_page_options_all";b:0;s:32:"items_per_page_options_all_label";s:7:"- All -";s:6:"offset";b:0;s:12:"offset_label";s:6:"Offset";}s:21:"views_infinite_scroll";a:2:{s:11:"button_text";s:9:"Load More";s:26:"automatically_load_content";b:0;}}}s:19:"display_description";s:0:"";s:9:"css_class";s:8:"eb-media";s:13:"relationships";a:1:{s:15:"image_target_id";a:8:{s:2:"id";s:15:"image_target_id";s:5:"table";s:12:"media__image";s:5:"field";s:15:"image_target_id";s:12:"relationship";s:4:"none";s:10:"group_type";s:5:"group";s:11:"admin_label";s:5:"Image";s:8:"required";b:1;s:9:"plugin_id";s:8:"standard";}}}s:14:"cache_metadata";a:3:{s:7:"max-age";i:-1;s:8:"contexts";a:5:{i:0;s:26:"languages:language_content";i:1;s:28:"languages:language_interface";i:2;s:3:"url";i:3;s:14:"url.query_args";i:4;s:16:"user.permissions";}s:4:"tags";a:0:{}}}'));

    // This line is needed because Views is insane.
    $view
      ->getExecutable()->current_display = NULL;
    $view
      ->save();

    /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
    $browser = EntityBrowser::load('image_browser');
    $browser
      ->addWidget([
      'id' => 'view',
      'label' => 'Library',
      'weight' => -10,
      'settings' => [
        'view' => 'media',
        'view_display' => $display_id,
        'submit_text' => 'Select',
        'auto_select' => FALSE,
      ],
    ]);
    $browser
      ->save();
  }
}

/**
 * Implements hook_field_widget_info_alter().
 */
function lightning_media_image_field_widget_info_alter(array &$info) {
  if (isset($info['image_widget_crop'])) {

    // We cannot import the class with a use statement, because it will blow up
    // if Image Widget Crop is not installed.
    Override::pluginClass($info['image_widget_crop'], \Drupal\lightning_media_image\Plugin\Field\FieldWidget\ImageCropWidget::class);
  }
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function lightning_media_image_crop_type_insert($crop_type) {

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

    // Create a dedicated image style to display images using this crop type.

    /** @var \Drupal\crop\CropTypeInterface $crop_type */
    $image_style = ImageStyle::create([
      'name' => 'crop_' . $crop_type
        ->id(),
      'label' => t('Cropped: @crop_type', [
        '@crop_type' => $crop_type
          ->label(),
      ]),
    ]);
    $image_style
      ->addImageEffect([
      'id' => 'crop_crop',
      'data' => [
        'crop_type' => $crop_type
          ->id(),
      ],
    ]);
    $image_style
      ->save();
  }
}

/**
 * Implements hook_modules_installed().
 */
function lightning_media_image_modules_installed(array $modules) {

  // Don't do anything during config sync.
  if (\Drupal::isConfigSyncing()) {
    return;
  }
  if (in_array('lightning_roles', $modules)) {
    $permissions = [
      'access image_browser entity browser pages',
    ];

    // Media creators and managers can always access the image browser.
    user_role_grant_permissions('media_creator', $permissions);
    user_role_grant_permissions('media_manager', $permissions);

    // Any content creator can as well.
    \Drupal::service('lightning.content_roles')
      ->grantPermissions('creator', $permissions);
  }
  if (in_array('image_widget_crop', $modules)) {
    Config::forModule('lightning_media_image')
      ->optional()
      ->getEntity('crop_type', 'freeform')
      ->save();
  }
  if (in_array('lightning_dev', $modules)) {
    $GLOBALS['install_state'] = [];
    $view = entity_load('view', 'media');
    lightning_media_image_view_insert($view);
    unset($GLOBALS['install_state']);
  }
}

/**
 * Implements hook_media_source_info_alter().
 */
function lightning_media_image_media_source_info_alter(array &$sources) {
  $sources['image']['input_match']['field_types'] = [
    'image',
  ];
  Override::pluginClass($sources['image'], Image::class);

  // When a media item that uses the Image plugin is embedded in a text editor,
  // prefer the media_image display plugin.
  // @see \Drupal\lightning_media\Form\EntityEmbedDialog::buildEmbedStep()
  $sources['image']['entity_embed_display'] = 'media_image';
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function lightning_media_image_form_entity_browser_image_browser_form_alter(array &$form, FormStateInterface $form_state) {
  $form['#attached']['library'][] = 'lightning_media/browser.styling';
}

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
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',
    ]);
  }
}