You are here

function insert_insert_process in Insert 8.2

Implements hook_insert_process().

File

./insert.module, line 428

Code

function insert_insert_process(&$insertType, array &$element) {
  if ($insertType !== INSERT_TYPE_FILE && $insertType !== INSERT_TYPE_IMAGE) {
    return [];
  }

  // Prevent displaying the Insert button when the file/image is empty ("Add a
  // new file" row).
  if (count($element['#value']['fids']) === 0) {
    return FALSE;
  }
  $settings = $element['#insert']['settings'];
  if (!count(array_filter($settings['styles']))) {

    // Insert is disabled (no styles selected in the widget settings).
    return FALSE;
  }
  $item = $element['#value'];
  if (!isset($item['fids']) || count($item['fids']) === 0) {
    return FALSE;
  }

  /** @var \Drupal\file\Entity\File $file */
  $file = File::load($item['fids'][0]);
  $config = \Drupal::config('insert.config');
  $insertType = INSERT_TYPE_FILE;
  if ($element['#insert']['type'] === INSERT_TYPE_IMAGE || InsertUtility::isImage($file) && $config
    ->get('file_field_images_enabled')) {
    $insertType = INSERT_TYPE_IMAGE;
  }
  $element['#insert'][$insertType]['file'] = $file;
  $element['#insert']['settings']['fid'] = $item['fids'][0];
  $element['#insert']['id'] = $file
    ->uuid();
  $element['insert']['#attributes']['data-uuid'] = $file
    ->uuid();
  $element['insert']['insert_filename'] = [
    '#type' => 'hidden',
    '#value' => $file
      ->getFilename(),
    '#id' => $element['#insert']['id'] . '-insert-filename',
    '#name' => $element['#name'] . '[insert_filename]',
    '#attributes' => [
      'class' => [
        'insert-filename',
      ],
    ],
  ];
  $element['#attached']['library'][] = 'insert/insert';
  return [];
}