You are here

function fe_paths_file_process in File Entity Paths 7.2

Actually pre processes the files. Set a $file->fe_paths_processed flag on the file entity, then add to process queue. The file will be processed on drupal_register_shutdown.

See also

fe_paths_file_presave()

fe_paths_file_process()

1 string reference to 'fe_paths_file_process'
fe_paths_entity_presave in ./fe_paths.module
Implements hook_file_presave().

File

./fe_paths.module, line 205
Contains functions for the File Entity Paths module.

Code

function fe_paths_file_process($fid, $entity, $type, $field_name = NULL) {
  $file = file_load($fid);
  if ($file !== FALSE && fe_paths_file_process_available($file)) {
    $configs = fe_paths_config_load_multiple();
    fe_paths_add_global_settings_to_config($configs, $file);
    $entity_info = entity_get_info($type);
    $match = FALSE;

    // Variable to track, if we reach the correct config.
    $fe_paths_usage = fe_paths_usage($file->fid);
    $file_config = FALSE;
    $entity_key = $entity_info['entity keys']['id'];
    $entity_id = $entity->{$entity_key};
    $bundle_key = isset($entity_info['entity keys']['bundle']) ? $entity_info['entity keys']['bundle'] : 'type';

    // Like user, it hasn't bundle keys, but 'user' is a bundle.
    $bundle = isset($entity->{$bundle_key}) ? $entity->{$bundle_key} : $type;

    // If we have valid usage, we have some chance to quit immediatly.
    if ($fe_paths_usage) {
      $file_config = $configs[$fe_paths_usage->id];

      // We only have chances, when 'Override by other configuration' is
      // not checked in this configuration.
      if (!$file_config->data['other_config']) {

        // If "Never" is set, no more questions..
        if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_NEVER) {
          return;
        }

        // If 'Within the same entity type' overridden enabled, but the usage
        // and the processed entity's type is different, just leave.
        if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_WITHIN_ENTITY_TYPE && $fe_paths_usage->entity_type != $type) {
          return;
        }

        // If 'Within the same entity' is checked, and the usage and entity id
        // is different, leave again.
        if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_WITHIN_ENTITY && $fe_paths_usage->entity_id != $entity_id) {
          return;
        }

        // This stay here, if in the future bundle investigation is necessary.

        //if (fe_paths_have_valid_bundle($type)) {

        // If still here need to load it for further investigation.
        //  $usage_entity = entity_load_single($fe_paths_usage->entity_type, $fe_paths_usage->entity_id);

        //}
      }
    }

    // If we are here, start to work with all configs.
    foreach ($configs as $config) {

      // If we have match, no need to check more config, we have result.
      if ($match) {
        break;
      }

      // If $this_file_config is TRUE, means, the $config is the same, like
      // previously processed on this file.
      $this_file_config = $config == $file_config;
      $data = $config->data;

      // If override by other configuration is not allowed, and doesn't reach
      // own config, skip to the next config.
      if ($file_config && !$file_config->data['other_config'] && !$this_file_config) {
        continue;
      }

      // We reach the existing used config, no more config need to visit.
      // Step to the next config means, the file would be processed by a HEIGHER
      // weight configuration, than previously was processed.
      if ($this_file_config) {
        $match = TRUE;

        // Let's see, how can we escape here. We can check the conditions used
        // sooner, but can't escape because the config can be overridden by
        // lighter config.
        if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_NEVER) {
          return;
        }
        if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_WITHIN_ENTITY_TYPE && $fe_paths_usage->entity_type != $type) {
          return;
        }
        if ($file_config->data['override_options'] === FE_PATHS_OVERRIDE_WITHIN_ENTITY && $fe_paths_usage->entity_id != $entity_id) {
          return;
        }
      }

      // First, step to the next, if the file types of config doesn't match.
      if (isset($data['file_entity'][$file->type]) && $data['file_entity'][$file->type] !== $file->type) {
        continue;
      }

      // If the processed entity has different type then this config, or the
      // config settings is not global skip.
      if (isset($data['entity']) && $data['entity'] != $type && $data['entity'] != 'global') {
        continue;
      }

      // If bundle doesn't match, skip, but only if bundle is set.
      if (isset($data['bundle']) && !isset($data['bundle'][$bundle])) {
        continue;
      }

      // Only need field investigation, if field name was passed to this
      // function.
      if (!is_null($field_name)) {

        // If the field name doesn't exists, skip.
        if (!isset($data['bundle'][$bundle][$field_name])) {
          continue;
        }

        // If the field name isn't checked in config, skip
        if (!$data['bundle'][$bundle][$field_name]) {
          continue;
        }
      }

      //$fields = fe_paths_get_available_fields($type, $bundle);
      if (fe_paths_move_file($file, $config->path, $config->filename, $type, $data, $entity)) {
        $match = TRUE;

        // Store, which configuration with which entity moves the file.
        fe_paths_usage_add($file->fid, $type, $entity->{$entity_key}, $config->id);
      }
      else {
        $match = FALSE;
      }
    }
  }
}