view_mode_crop-1.0.x-dev/view_mode_crop.module
view_mode_crop.module
<?php /** * @file * View mode crop module file. */ use Drupal\view_mode_crop\Plugin\Field\FieldFormatter\ViewModeCropEntityReferenceEntityFormatter; use Drupal\view_mode_crop\Plugin\Field\FieldWidget\ViewModeCropMediaLibraryWidget; use Drupal\view_mode_crop\Plugin\Field\FieldWidget\ViewModeCropImageWidget; use Drupal\view_mode_crop\CropImageHelper; use Drupal\Core\Entity\EntityInterface; use Drupal\view_mode_crop\Plugin\Field\FieldType\ViewModeCropEntityReferenceItem; use Drupal\Core\StreamWrapper\StreamWrapperManager; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\view_mode_crop\Plugin\Field\FieldType\ViewModeCropImageItem; /** * Implements hook_field_info_alter(). */ function view_mode_crop_field_info_alter(&$info) { if (isset($info['image'])) { $info['image']['class'] = ViewModeCropImageItem::class; } if (isset($info['entity_reference'])) { $info['entity_reference']['class'] = ViewModeCropEntityReferenceItem::class; } } /** * Implements hook_field_widget_info_alter(). */ function view_mode_crop_field_widget_info_alter(array &$info) { if (isset($info['image_image'])) { $info['image_image']['class'] = ViewModeCropImageWidget::class; } if (isset($info['media_library_widget'])) { $info['media_library_widget']['class'] = ViewModeCropMediaLibraryWidget::class; } } /** * Implements hook_field_formatter_info_alter(). */ function view_mode_crop_field_formatter_info_alter(array &$info) { if (isset($info['entity_reference_entity_view'])) { $info['entity_reference_entity_view']['class'] = ViewModeCropEntityReferenceEntityFormatter::class; $info['entity_reference_entity_view']['provider'] = 'view_mode_crop'; } } /** * Implements hook_theme(). */ function view_mode_crop_theme($existing, $type, $theme, $path) { return [ 'view_mode_crop_wrapper' => [ 'render element' => 'element', ], ]; } /** * Prepares variables for the cropper modal dialog. * * Default template: view-mode-crop-wrapper.html.twig. * * @param array $variables * An associative array containing: * - element: An associative array containing the properties of the element. * Properties used: #menu, #content. */ function template_preprocess_view_mode_crop_wrapper(array &$variables) { $variables['content'] = &$variables['element']['content']; $variables['attributes']['class'][] = 'view-mode-crop-wrapper'; } /** * Implements hook_file_download(). */ function view_mode_crop_file_download($uri) { $uri = str_replace('private://crop/', 'crop-private://', $uri); $target = StreamWrapperManager::getTarget($uri); $parsed = CropImageHelper::parsePathOrUri($target); if ($parsed === NULL) { return; } $storage = Drupal::entityTypeManager()->getStorage($parsed['entity_type_id']); $entity = $storage->load($parsed['entity_id']); if (!$entity instanceof ContentEntityInterface) { throw new \RuntimeException('Not a Content entity'); } /** * @var \Drupal\view_mode_crop\Plugin\Field\FieldType\ViewModeCropImageItem $image_item */ $image_item = $entity->get($parsed['field_name'])->get($parsed['delta']); if (!$image_item->entity->access('download')) { return -1; } /** * @var \Drupal\file\FileInterface $file */ $file = $image_item->entity; return [ // Send headers describing the image's size, and MIME-type. 'Content-Type' => $file->getMimeType(), 'Content-Length' => $file->getSize(), // By not explicitly setting them here, this uses normal Drupal // Expires, Cache-Control and ETag headers to prevent proxy or // browser caching of private images. ]; } /** * Implements hook_entity_delete(). */ function view_mode_crop_entity_delete(EntityInterface $entity) { $db = Drupal::database(); $db->delete('view_mode_crop') ->condition('entity_type', $entity->getEntityTypeId()) ->condition('entity_id', $entity->id()) ->execute(); CropImageHelper::deleteCroppedImagesForEntity($entity); } /** * Implements hook_entity_update(). */ function view_mode_crop_entity_update(EntityInterface $entity) { CropImageHelper::deleteCroppedImagesForEntity($entity); } /** * Implements hook_preprocess_HOOK(). */ function view_mode_crop_preprocess_form_element(&$variables) { if (isset($variables['element']['#attributes']['data-view-mode-crop-triggering-element-name'])) { $variables['attributes']['class'][] = 'view-mode-crop-state-container'; } }