You are here

function insert_insert_variables in Insert 8.2

Implements hook_insert_variables().

File

./insert.module, line 488

Code

function insert_insert_variables($insertType, array &$element, $styleName, array &$vars) {
  if (!in_array($insertType, [
    INSERT_TYPE_FILE,
    INSERT_TYPE_IMAGE,
  ])) {
    return [];
  }

  /** @var \Drupal\file\Entity\File $file */
  $file = $element['#insert'][$insertType]['file'];
  if ($file === NULL) {
    return [
      FALSE,
    ];
  }
  $config = \Drupal::config('insert.config');
  $imageInfo = pathinfo($file
    ->getFileUri());
  $extension = isset($imageInfo['extension']) ? $imageInfo['extension'] : NULL;
  if ($styleName === 'audio' && !in_array($extension, $config
    ->get('file_extensions.audio')) || $styleName === 'video' && !in_array($extension, $config
    ->get('file_extensions.video'))) {
    return [
      FALSE,
    ];
  }
  $settings = $element['#insert']['settings'];
  if ($styleName === 'insert__auto') {
    $vars['insert__auto'] = NULL;
    if (in_array($extension, $config
      ->get('file_extensions.video'))) {
      $vars['insert__auto'] = 'video';
    }
    elseif (in_array($extension, $config
      ->get('file_extensions.audio'))) {
      $vars['insert__auto'] = 'audio';
    }
    elseif (InsertUtility::isImage($file) && $config
      ->get('file_field_images_enabled') || $insertType === INSERT_TYPE_IMAGE) {
      $vars['insert__auto'] = $settings['auto_image_style'];
    }
  }
  $styleNameSegments = explode('__', $styleName, 2);
  if (isset($styleNameSegments[1])) {
    $styleName = $styleNameSegments[1];
  }
  $vars += [
    'class' => join(' ', $config
      ->get('css_classes.' . $insertType)),
    'file' => $file,
    'entity_type' => $file
      ->getEntityTypeId(),
    'mime_type' => $file
      ->getMimeType(),
    'uuid' => $file
      ->uuid(),
    'url' => file_create_url($file
      ->getFileUri()),
  ];
  if (!$config
    ->get('absolute')) {
    $parsedUrl = parse_url($vars['url']);
    $vars['url'] = $parsedUrl['path'];
    if (!empty($parsedUrl['query'])) {
      $vars['url'] .= '?' . $parsedUrl['query'];
    }
  }
  if ($insertType === INSERT_TYPE_IMAGE) {
    $vars['url_original'] = $vars['url'];
    $vars['url_link'] = NULL;
    $vars['uuid'] = 'insert-' . $styleName . '-' . $vars['uuid'];
    $styleUrl = InsertUtility::buildDerivativeUrl($vars['file'], isset($vars['insert__auto']) ? $vars['insert__auto'] : $styleName, $config
      ->get('absolute'));
    if ($styleUrl !== NULL) {
      $vars['url'] = $styleUrl;
      if ($settings['link_image']) {
        $vars['url_link'] = InsertUtility::buildDerivativeUrl($vars['file'], $settings['link_image'], $config
          ->get('absolute'));
      }
    }
  }
  return [];
}