Commit dbea54af authored by Bart Vanderstukken's avatar Bart Vanderstukken Committed by Bart Vanderstukken
Browse files

Issue #2679619 by Sneakyvv: File usage not working for nested paragraph items

parent 664b036b
Loading
Loading
Loading
Loading
+80 −31
Original line number Diff line number Diff line
@@ -54,38 +54,10 @@ function paragraph_panes_edit_form_submit($form, &$form_state) {
    $field_name = $field['field_name'];
    $field_value = _paragraph_panes_sanitize_values($form_state['values'][$field_name][LANGUAGE_NONE]);

    // Handle files usage.
    // Handle file usage.
    $field_info = field_info_field($field_name);
    if (isset($field_info['type']) && in_array($field_info['type'], array('file', 'image'))) {
      // Add usage for new values.
      foreach ($field_value as $item) {
        if (isset($item['fid'])) {
          $file = (object) $item;
          file_usage_add($file, 'paragraph_panes', 'paragraph_pane', 1);
        }
      }

      // Collect fids from old values.
      $fids = array();
      if (isset($form_state['conf'][$field_name])) {
        foreach ($form_state['conf'][$field_name] as $item) {
          if (isset($item['fid'])) {
            $fids[] = $item['fid'];
          }
        }
      }

      // Delete usage for collected fids.
      if (!empty($fids)) {
        $files = file_load_multiple($fids);
        foreach ($files as $file) {
          // Decrease file usage by one.
          file_usage_delete($file, 'paragraph_panes', 'paragraph_pane', 1);
          // Delete file if no longer used anywhere.
          file_delete($file);
        }
      }
    }
    $old_values = isset($form_state['conf'][$field_name]) ? $form_state['conf'][$field_name] : array();
    _paragraph_panes_file_usage_recursive($field_info, $field_value, $old_values);

    $form_state['conf'][$field_name] = $field_value;
  }
@@ -162,6 +134,83 @@ function _paragraph_panes_sanitize_values(array $field_values) {
  return $clean;
}

/**
 * @param $field_info
 *   The field info for the $field_value.
 * @param $field_value
 *   The current values for the field, from the form.
 * @param $old_values
 *   The old values for the field, from the pane configuration.
 */
function _paragraph_panes_file_usage_recursive($field_info, $field_value, $old_values) {
  // Handle nested paragraph items.
  if ($field_info['type'] == 'paragraphs') {
    foreach ($field_value as $key => $subfields) {
      foreach ($subfields as $subfield_name => $subfield_value) {
        if (!is_array($subfield_value)) {
          continue;
        }
        $subfield_info = field_info_field($subfield_name);
        $old_sub_values = isset($old_values[$key][$subfield_name][LANGUAGE_NONE])? $old_values[$key][$subfield_name][LANGUAGE_NONE] : array();
        _paragraph_panes_file_usage_recursive($subfield_info, $subfield_value[LANGUAGE_NONE], $old_sub_values);
      }
    }
  }
  else {
    _paragraph_panes_file_usage($field_info, $field_value, $old_values);
  }
}

/**
 * Handles the file_usage records.
 *
 * Adds a file_usage record, or increases its count if it already exist,
 * for all files & images uploaded into a paragaph pane.
 * Then it decreases the file_usage count for all "old" values, i.e. the ones
 * in the current pane config.
 * Finally it tries to delete the old files, which only happens if file count is 0 now.
 *
 * @param array $field_info
 *   The field info for the $field_value.
 * @param array $field_value
 *   The current values for the field, from the form.
 * @param array $old_values
 *   The old values for the field, from the pane configuration.
 */
function _paragraph_panes_file_usage($field_info, $field_value, $old_values) {
  if (isset($field_info['type'])) {
    // Handle files & images.
    if (in_array($field_info['type'], array('file', 'image'))) {
      // Add usage for new values.
      foreach ($field_value as $item) {
        if (isset($item['fid'])) {
          $file = (object) $item;
          file_usage_add($file, 'paragraph_panes', 'paragraph_pane', 1);
        }
      }

      // Collect fids from old values.
      $fids = array();
      foreach ($old_values as $item) {
        if (isset($item['fid'])) {
          $fids[] = $item['fid'];
        }
      }

      // Delete usage for collected fids.
      if (!empty($fids)) {
        $files = file_load_multiple($fids);
        foreach ($files as $file) {
          // Decrease file usage by one.
          file_usage_delete($file, 'paragraph_panes', 'paragraph_pane', 1);
          // Delete file if no longer used anywhere.
          file_delete($file);
        }
      }
    }
  }
}

/**
 * Unserializes the enitities back again.
 *