ept_image-1.4.x-dev/ept_image.module

ept_image.module
<?php

/**
 * @file
 * EPT Call to Action (CTA) module file.
 */

use Drupal\media\Entity\Media;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;

/**
 * Implements hook_preprocess_paragraph().
 */
function ept_image_preprocess_paragraph(&$variables) {
  _ept_image_apply_image_style($variables);
  _ept_image_apply_lightbox_image_style($variables);
}

/**
 * Replace image style for image formatter on fly.
 */
function _ept_image_apply_image_style(&$variables) {
  if (empty($variables['paragraph']) ||
    $variables['paragraph']->bundle() !== 'ept_image') {
    return;
  }

  if (empty($variables['content']['field_ept_image'][0]['#theme'])) {
    return;
  }

  if ($variables['content']['field_ept_image'][0]['#theme'] != 'image_formatter') {
    return;
  }

  if (empty($variables['content']['field_ept_settings'][0]['#ept_settings']['image_style'])) {
    return;
  }

  if ($variables['content']['field_ept_settings'][0]['#ept_settings']['image_style'] == 'none') {
    return;
  }

  $variables['content']['field_ept_image'][0]['#image_style'] = $variables['content']['field_ept_settings'][0]['#ept_settings']['image_style'];
}

/**
 * Add GLightbox image URL with image style.
 */
function _ept_image_apply_lightbox_image_style(&$variables) {
  if (empty($variables['paragraph']) ||
    $variables['paragraph']->bundle() !== 'ept_image') {
    return;
  }

  if (empty($variables['content']['field_ept_settings'][0]['#ept_settings']['image_lightbox'])) {
    return;
  }

  $paragraph = $variables['paragraph'];

  if (empty($paragraph->field_ept_image->target_id)) {
    return;
  };

  $media = Media::load($paragraph->field_ept_image->target_id);
  if (empty($media)) {
    return;
  }

  /** @var \Drupal\media\MediaTypeInterface $media_type */
  $media_type = \Drupal::entityTypeManager()
    ->getStorage('media_type')
    ->load($media->bundle());

  $source_field_name = $media_type
    ->getSource()
    ->getSourceFieldDefinition($media_type)
    ->getName();

  $fid = $media->get($source_field_name)->target_id;
  $file = File::load($fid);
  if (empty($file)) {
    return;
  }

  $variables['show_lightbox'] = TRUE;
  if (empty($variables['content']['field_ept_settings'][0]['#ept_settings']['lightbox_image_style']) ||
    $variables['content']['field_ept_settings'][0]['#ept_settings']['lightbox_image_style'] == 'none') {
    $uri = $file->getFileUri();
    $variables['lightbox_url'] = \Drupal::service('file_url_generator')->generateAbsoluteString($uri);
  }
  else {
    $image_uri = $file->getFileUri();
    $style = ImageStyle::load($variables['content']['field_ept_settings'][0]['#ept_settings']['lightbox_image_style']);
    $variables['lightbox_url'] = $style->buildUrl($image_uri);
  }
}

/**
 * Implements hook_theme().
 */
function ept_image_theme($existing, $type, $theme, $path) {
  $theme_templates = [];

  // Register custom Paragraph field number.
  $theme_templates['field__paragraph__ept_image__field_ept_image'] = [
    'base hook' => 'field',
  ];

  return $theme_templates;
}

/**
 * Implements hook_theme_suggestions_hook_alter().
 */
function ept_image_theme_suggestions_field_alter(array &$suggestions, array $variables, $hook) {
  $field = $variables['element']['#field_name'];

  if ($field == 'field_ept_image') {
    $suggestions[] = 'field__paragraph__ept_image__field_ept_image';
  }
}

/**
 * Implements hook_theme_registry_alter().
 */
function ept_image_theme_registry_alter(&$theme_registry) {
  $ept_module = 'ept_image';
  $module_path = \Drupal::service('extension.list.module')->getPath($ept_module);

  $base_theme = 'field';
  $theme_registry['field__paragraph__ept_image__field_ept_image'] = [
    'path' => $module_path . '/templates',
    'template' => 'field--paragraph--ept-image--field-ept-image',
    'render element' => $theme_registry[$base_theme]['render element'],
    'base hook' => $base_theme,
    'type' => 'module',
    'theme path' => $module_path,
    'preprocess functions' => $theme_registry[$base_theme]['preprocess functions'],
    'initial preprocess' => 'template_preprocess_field',
  ];
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc