You are here

function bulk_media_upload_upload_form in Bulk Media Upload 7

Form for bulk media upload

1 string reference to 'bulk_media_upload_upload_form'
bulk_media_upload_menu in ./bulk_media_upload.module
Implements hook_menu().

File

./bulk_media_upload.upload.inc, line 11
Upload form and entity generation functions.

Code

function bulk_media_upload_upload_form($form, &$form_state) {
  global $language;
  $entity_type = variable_get('bulk_media_upload_entity_type');
  $entity_info = entity_get_info($entity_type);
  $bundle = variable_get('bulk_media_upload_bundle');
  $mediafield = variable_get('bulk_media_upload_mediafield');
  if (empty($entity_type) || empty($bundle) || empty($mediafield)) {
    drupal_set_message(t('You have not configured the Bulk Media Upload module. Go to the <a href="@admin-url">configuration page</a> to fix this.', array(
      '@admin-url' => url('admin/config/media/bulk_media_upload'),
    )), 'error');
    return $form;
  }
  $mediafield_info = field_info_instance($entity_type, $mediafield, $bundle);
  $form['import_information'] = array(
    '#markup' => '<p>' . t('Bundle used for generating the entities: <strong>@bundle_label (@bundle_name)</strong>', array(
      '@bundle_label' => $entity_info['bundles'][$bundle]['label'],
      '@bundle_name' => $bundle,
    )) . '</p>' . '<p>' . t('Mediafield used for uploading your media: <strong>@field_label (@field_name)</strong>', array(
      '@field_label' => $mediafield_info['label'],
      '@field_name' => $mediafield_info['field_name'],
    )) . '</p>',
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#default_value' => '[file:name]',
    '#size' => 60,
    '#maxlength' => 255,
    '#required' => TRUE,
  );
  $form['token_help'] = array(
    '#title' => t('Replacement patterns'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['token_help']['help'] = array(
    '#theme' => 'token_tree',
    '#token_types' => array(
      $entity_type,
      'file',
    ),
    '#global_types' => TRUE,
    '#click_insert' => TRUE,
  );
  $form['upload'] = array(
    '#type' => 'plupload',
    '#title' => t('Bulk Media Upload'),
    '#description' => t(''),
    '#required' => TRUE,
  );
  if (isset($mediafield_info['settings']['file_extensions'])) {
    $form['upload']['#upload_validators']['file_validate_extensions'][] = $mediafield_info['settings']['file_extensions'];
  }

  // Use the entity creation form to extract all assigned fields.
  $allowed_fields = array_diff_key(field_info_instances($entity_type, $bundle), array(
    $mediafield => NULL,
  ));

  // Create the new entity.
  $entity = entity_create($entity_type, array(
    $entity_info['entity keys']['bundle'] => $bundle,
  ));
  $entity_form = array();

  // And add these fields, if any, to a default value fieldset without the media field.
  field_attach_form($entity_type, $entity, $entity_form, $form_state);
  if ($additions = array_intersect_key($entity_form, $allowed_fields)) {
    $form['default_values'] = array(
      '#type' => 'fieldset',
      '#title' => t('Default Values'),
      '#tree' => TRUE,
    ) + $additions;
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Generate @entity_type entities', array(
      '@entity_type' => $entity_info['label'],
    )),
  );
  return $form;
}