You are here

function scald_image_scald_add_form_fill in Scald: Media Management made easy 7

Implements hook_scald_add_form_fill().

File

modules/providers/scald_image/scald_image.module, line 83
Scald Image is a Scald Atom Provider for images.

Code

function scald_image_scald_add_form_fill(&$atoms, $form, $form_state) {
  foreach ($atoms as $delta => $atom) {
    if (is_array($form_state['values']['file']) && module_exists('plupload')) {
      module_load_include('inc', 'scald', 'includes/scald.plupload');
      $destination = $form['file']['#upload_location'] . '/' . $form_state['values']['file'][$delta]['name'];
      $file = scald_plupload_save_file($form_state['values']['file'][$delta]['tmppath'], $destination);
    }
    else {
      $file = file_load($form_state['values']['file']);
    }
    $atom->title = $file->filename;
    $atom->base_id = $file->fid;
    $variable_names = array(
      'scald_authors' => 'scald_author_vocabulary',
      'scald_tags' => 'scald_tags_vocabulary',
    );

    // Hacky, because variable and field name do not really match.
    foreach ($variable_names as $field_name => $variable_name) {
      $langcode = field_language('scald_atom', $atom, $field_name);
      if (empty($form_state['values'][$field_name])) {
        continue;
      }

      // Borrowed from taxonomy_autocomplete_validate().
      $typed_terms = drupal_explode_tags($form_state['values'][$field_name]);
      $vocabulary = taxonomy_vocabulary_machine_name_load(variable_get($variable_name, $field_name));
      foreach ($typed_terms as $typed_term) {
        if ($possibilities = taxonomy_term_load_multiple(array(), array(
          'name' => trim($typed_term),
          'vid' => $vocabulary->vid,
        ))) {
          $term = array_pop($possibilities);
        }
        else {
          $term = (object) array(
            'vid' => $vocabulary->vid,
            'name' => $typed_term,
            'vocabulary_machine_name' => $vocabulary->machine_name,
          );
          taxonomy_term_save($term);
        }
        $atom->{$field_name}[$langcode][] = array(
          'tid' => $term->tid,
        );
      }
    }
    $langcode = field_language('scald_atom', $atom, 'scald_thumbnail');
    $atom->scald_thumbnail[$langcode][0] = (array) $file;
  }
}